You are viewing a single comment's thread. Return to all comments →
static int[] countingSort(int[] arr) { int n = arr.length; int k = arr[0]; for(int i=1;i<n;i++) { if(arr[i]>k) { k = arr[i]; } } int count[] = new int[k+1]; for(int i=0;i<n;i++) { ++count[arr[i]]; } for(int i=1;i<k+1;i++) { count[i] = count[i]+count[i-1]; } int b[] = new int[n]; for(int i=n-1;i>=0;i--) { b[--count[arr[i]]] = arr[i]; } for(int i=0;i<n;i++) { arr[i] = b[i]; } return arr; }
Seems like cookies are disabled on this browser, please enable them to open this website
Counting Sort 2
You are viewing a single comment's thread. Return to all comments →