For an int array, write a counting sort algorithm that sorts the array elements.
Given an int array a and the size of the array n, return the sorted array.
Test examples:
[1,2,3,5,2,3],6
[1,2,2,3,3,5]
classCountingsort { Public: int* Countingsort (intAintN) {//Write code here intmin=a[0],max=a[0]; for(intI=1; i<n;i++) { if(a[i]<min) min=A[i]; if(A[i]>max) max=A[i]; } intk=max-min+1; int* b=New intK ();//initialized to 0 for(intI=0; i<n;i++) B[a[i]-min]++; intidx=0; for(inti=min;i<=max;i++) for(intj=0; j<b[i-min];j++) A[idx++]=i; Delete[]b; returnA; }};
For an int array, write a cardinal sort algorithm that sorts the array elements.
Given an int array a and the size of the array n, return the sorted array. The guaranteed element is less than or equal to 2000.
Test examples:
[1,2,3,5,2,3],6
[1,2,2,3,3,5]
2nd Chapter Sort | The 10th section counts the sorting exercises