We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- Sorting
- Counting Sort 2
- Discussions
Counting Sort 2
Counting Sort 2
+ 0 comments Java 8:
public static List<Integer> countingSort(List<Integer> arr) { Collections.sort(arr); return arr; }
+ 0 comments vector countingSort(vector arr) { sort(arr.begin(),arr.end()); return arr; }
+ 0 comments public static List<Integer> countingSort(List<Integer> arr) { int[] frequency = new int[100]; List<Integer> sorted = new ArrayList<>(); // count frequency for(int i = 0; i < arr.size(); i++) { frequency[arr.get(i)]++; } for(int i = 0; i < frequency.length; i++) { int count = frequency[i]; if(count == 0) { continue; } for(int k = 0; k < count; k++) { sorted.add(i); } } return sorted; }
+ 0 comments sort(arr.begin(),arr.end()); vector<int>count(100,0); for(int i=0;i<arr.size();++i) { count[arr[i]]++; } return arr;
+ 0 comments void merge_sort(vector<int> &arr, int left, int right, int middle){ int i=left,j=middle+1,temp_i=0; int temp[right-left+1]; while(i<=middle && j<=right){ if(arr[i]<arr[j]) temp[temp_i++]=arr[i++]; else temp[temp_i++]=arr[j++]; } while(i<=middle) temp[temp_i++]=arr[i++]; while(j<=right) temp[temp_i++]=arr[j++]; temp_i=0; for(i=left;i<=right;i++){ arr[i]=temp[temp_i++]; } } void merge(vector<int> &arr, int left, int right){ if(left<right){ int middle = (right+left)/2; merge(arr, left, middle); merge(arr, middle+1, right); merge_sort(arr,left,right,middle); } } vector<int> countingSort(vector<int> arr) { merge(arr,0,arr.size()-1); return arr; }
Merge sort ver, believe it's quick.
Load more conversations
Sort 339 Discussions, By:
Please Login in order to post a comment