Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5c1faf6

Browse files
add basicCountingSortWithNegative
1 parent e9ce3a8 commit 5c1faf6

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

‎src/main/java/ir/sk/algorithm/others/Sort.java‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ public static void treeSort(int[] array) {
508508
*/
509509
@TimeComplexity("O(n + k) + O(n) = O(2n + k) = O(n + k) where n is the number of elements in input array and k is the range of input.")
510510
@SpaceComplexity("nO(n + k)")
511-
public static void countSort(int[] input) {
511+
public static void basicCountSort(int[] input) {
512512
int max = Arrays.stream(input).max().getAsInt();
513513

514514
// create buckets
@@ -527,6 +527,26 @@ public static void countSort(int[] input) {
527527
}
528528
}
529529

530+
@TimeComplexity("O(n + k) + O(n) = O(2n + k) = O(n + k) where n is the number of elements in input array and k is the range of input.")
531+
@SpaceComplexity("nO(n + k)")
532+
public static void basicCountingSortWithNegative(int[] array) {
533+
int min = Arrays.stream(array).min().getAsInt();
534+
int max = Arrays.stream(array).max().getAsInt();
535+
536+
int range = max - min + 1;
537+
int[] counter = new int[range];
538+
539+
for (int i = 0 ; i < array.length; i++) {
540+
counter[array[i] - min]++;
541+
}
542+
543+
int index = 0;
544+
for (int i = 0; i < counter.length; i++) {
545+
for (int j = 0; j < counter[i]; j++) {
546+
array[index++] = i + min;
547+
}
548+
}
549+
}
530550

531551
/**
532552
* Counting sort which takes negative numbers as well

‎src/test/java/ir/sk/algorithm/others/SortTest.java‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void treeSort() {
140140
@Test
141141
public void countingSort() {
142142
long start = System.currentTimeMillis();
143-
Sort.countSort(actual);
143+
Sort.basicCountSort(actual);
144144
long end = System.currentTimeMillis();
145145
System.out.println("Logic countingSort took " + (end - start) + " MilliSeconds");
146146
assertArrayEquals(expected, actual);
@@ -157,6 +157,17 @@ public void countingSortWithNegative() {
157157
assertArrayEquals(expected, arr);
158158
}
159159

160+
@Test
161+
public void basicCountingSort() {
162+
int[] arr = new int[]{9, 2, 1, 5, 5, 6, -2};
163+
int[] expected = new int[]{-2, 1, 2, 5, 5, 6, 9};
164+
long start = System.currentTimeMillis();
165+
Sort.basicCountingSortWithNegative(arr);
166+
long end = System.currentTimeMillis();
167+
System.out.println("Logic countingSortWithNegative took " + (end - start) + " MilliSeconds");
168+
assertArrayEquals(expected, arr);
169+
}
170+
160171
@Test
161172
public void quickSort() {
162173
long start = System.currentTimeMillis();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /