Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Counting sort in Java #2129

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions sorting/counting_sort/countingSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
public class countingSort{
public static void main(String[] args) {
int[] unsorted = {0,1,94,1,68,28,8,2,5,8,2,8,2,5,6,6,7,9,9,9,5,2,2,34};

System.out.println("Array before:");
for(int i = 0; i < unsorted.length; i++) System.out.print(unsorted[i] + ", ");

unsorted = countingSort(unsorted);

System.out.println("\n\nArray after: ");
for(int i = 0; i < unsorted.length; i++) System.out.print(unsorted[i] + ", ");

}

public static int[] countingSort(int[] arr) {
int[] toReturn = new int[arr.length];

int min = arr[0];
int max = arr[0];

for(int i = 1; i < arr.length; i++) {
if(arr[i] < min) min = arr[i];
if(arr[i] > max) max = arr[i];
}

// Where we'll be storing the counts of our values.
int[] count = new int[max - min + 1];

// First loop populating our counts.
for(int i = 0; i < arr.length; i++) count[arr[i] - min]++;

// Calculating the number of times each value occurs.
count[0]--;
for(int i = 1; i < count.length; i++) count[i] = count[i] + count[i-1];

// Determining where we should put the value and how many times it should go there.
for(int i = toReturn.length - 1; i >= 0; i--) {
int index = count[arr[i] - min]--;
toReturn[index] = arr[i];
}

return toReturn;
}
}