Skip to content

Commit a7a087d

Browse files
authored
Merge pull request TheAlgorithms#669 from ani03sha/Development
Added Counting Sort and Cycle Sort
2 parents f65fc4d + c22449a commit a7a087d

File tree

4 files changed

+202
-0
lines changed

4 files changed

+202
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package src.main.java.com.sorts;
2+
3+
public class CountingSort {
4+
5+
/**
6+
* This method sorts the array using counting sort technique
7+
*
8+
* @param arr The array to be sorted
9+
* @return arr Sorted array
10+
*/
11+
public Integer[] sort(Integer[] arr) {
12+
13+
// Finding the maximum element in the array
14+
int max = arr[0];
15+
for (int i = 1; i < arr.length; i++) {
16+
max = Math.max(max, arr[i]);
17+
}
18+
19+
// Finding the minimum element in the array
20+
int min = arr[0];
21+
for (int i = 1; i < arr.length; i++) {
22+
min = Math.min(min, arr[i]);
23+
}
24+
25+
// Creating the count array - This method will store the count of each element in the unsorted array
26+
int[] count = new int[max - min + 1];
27+
28+
// This loop will store the count of each element in the array
29+
for (Integer integer : arr) {
30+
count[integer - min]++;
31+
}
32+
33+
// This loop will replace the ith index of the count array with the sum of values at the ith and (i-1) index
34+
for (int i = 1; i < count.length; i++) {
35+
36+
count[i] = count[i] + count[i - 1];
37+
}
38+
39+
// This array will store the sorted array
40+
Integer[] places = new Integer[arr.length];
41+
42+
// This loop will put the ith element at is correct position in the places array
43+
for (int i = 0; i < places.length; i++) {
44+
45+
// Getting the value of the index - the value at the oount array index will be replaced by the value
46+
// in the original array
47+
int index = arr[i];
48+
places[count[index - min] - 1] = index;
49+
count[index - min]--;
50+
}
51+
52+
// Copy the places array back to the original array - which will be sorted
53+
System.arraycopy(places, 0, arr, 0, arr.length);
54+
55+
return arr;
56+
}
57+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package src.main.java.com.sorts;
2+
3+
public class CycleSort {
4+
5+
/**
6+
* @param arr Array to be sorted
7+
* @param <T> Generic type
8+
* @return arr Sorted array
9+
*/
10+
public <T extends Comparable<T>> T[] sort(T[] arr) {
11+
12+
int n = arr.length;
13+
14+
// Counter for the number of memory writes
15+
int count = 0;
16+
17+
// Traverse array and put the elements on their respective right places
18+
for (int i = 0; i < n - 2; i++) {
19+
20+
// Initialize item as the starting point
21+
T item = arr[i];
22+
23+
// Find the position where we want to put the item
24+
// Basically we count all the smaller elements to the right of the item
25+
int position = i;
26+
27+
for (int j = i + 1; j < n; j++) {
28+
if (arr[j].compareTo(item) < 0) {
29+
position++;
30+
}
31+
}
32+
33+
// Check if the element is already at the correct position...
34+
if (position == i) {
35+
36+
// .. then we do not have to do anything
37+
continue;
38+
}
39+
40+
// Ignore duplicate elements
41+
while (item == arr[position]) {
42+
position++;
43+
}
44+
45+
// Put the elements at its right position
46+
if (position != i) {
47+
48+
// Swap
49+
T temp = item;
50+
item = arr[position];
51+
arr[position] = temp;
52+
53+
count++;
54+
}
55+
56+
// Rotate remaining cycle
57+
while (position != i) {
58+
position = i;
59+
60+
// Find the position where we put the element
61+
for (int j = i + 1; j < n; j++) {
62+
if (arr[j].compareTo(item) < 0) {
63+
position++;
64+
}
65+
}
66+
67+
// Ignore duplicate elements
68+
while (item == arr[position]) {
69+
position++;
70+
}
71+
72+
// Put the element to its correct position
73+
if (item != arr[position]) {
74+
T temp = arr[position];
75+
arr[position] = item;
76+
item = temp;
77+
78+
count++;
79+
}
80+
}
81+
}
82+
83+
System.out.println("Number of memory writes :: " + count);
84+
85+
return arr;
86+
}
87+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package src.test.java.com.sorts;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import src.main.java.com.sorts.CountingSort;
6+
7+
public class CountingSortTest {
8+
9+
@Test
10+
public void testCountingSort() {
11+
12+
CountingSort countingSort = new CountingSort();
13+
14+
// Unsorted integer array
15+
Integer[] unsorted = new Integer[]{1, 4, 1, 2, 7, 5, 2};
16+
17+
// Sorted integer array
18+
Integer[] sorted = new Integer[]{1, 1, 2, 2, 4, 5, 7};
19+
20+
// Comparing the two integer arrays
21+
Assert.assertArrayEquals(sorted, countingSort.sort(unsorted));
22+
}
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package src.test.java.com.sorts;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import src.main.java.com.sorts.CycleSort;
6+
7+
public class CycleSortTest {
8+
9+
@Test
10+
public void cycleSortIntegerTest() {
11+
12+
CycleSort cycleSort = new CycleSort();
13+
14+
// Test case for integers
15+
Integer[] unsortedInt = new Integer[]{5, 1, 7, 0, 2, 9, 6, 3, 4, 8};
16+
Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
17+
Assert.assertArrayEquals(sortedInt, cycleSort.sort(unsortedInt));
18+
19+
// Test case for floating point numbers
20+
Float[] unsortedFloat = new Float[]{6.7f, 21.1f, 0.9f, -3.2f, 5.9f, -21.3f};
21+
Float[] sortedFloat = new Float[]{-21.3f, -3.2f, 0.9f, 5.9f, 6.7f, 21.1f};
22+
Assert.assertArrayEquals(sortedFloat, cycleSort.sort(unsortedFloat));
23+
24+
// Test case for characters
25+
Character[] unsortedChar = new Character[]{'c', 'a', 'b', 'A', 'C', 'B'};
26+
Character[] sortedChar = new Character[]{'A', 'B', 'C', 'a', 'b', 'c'};
27+
Assert.assertArrayEquals(sortedChar, cycleSort.sort(unsortedChar));
28+
29+
// Test case for Strings
30+
String[] unsortedStr = new String[]{"Edward", "Linus", "David", "Alan", "Dennis", "Robert", "Ken"};
31+
String[] sortedStr = new String[]{"Alan", "David", "Dennis", "Edward", "Ken", "Linus", "Robert"};
32+
Assert.assertArrayEquals(sortedStr, cycleSort.sort(unsortedStr));
33+
34+
}
35+
}

0 commit comments

Comments
 (0)