1counting-SORT2 (a,k)2 Let C[0..K] is a new array3 fori =0 to K4c[i]=05 fori = 1To A.length6C[a[i]] = c[a[i]]+17 fori = 1to K8C[i] = C[i] + c[i-1]9 fori = a.length Downto 1Ten whileI < (j =C[a[i]) One Exchagnge A[i] with A[j] AC[A[J]] = c[a[j]]-1
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <memory.h>void radix2 (int* arr, int len, int k) {k++;int* Count_arr = (int*) malloc (sizeof (int) *k), for (int i = 0; i < K; i++) {count_arr[i] = 0;} for (int i = 0; i < len; i++) {count_arr[arr[i]]++;} for (int i = 1; i < K; i++) {Count_arr[i] + = count_arr[i-1];} for (int i = len-1; I >= 0; i--) {//a[i+1 ... len-1] element is the element that has determined the final position of elements//find Arr[i] The final position of the element is J, if i<j, then swap (A[i],a[j]),// Continue to find the final position of the new Arr[i] element until I>=jint j = count_arr[arr[i] "-1;while (i < (j = Count_arr[arr[i]]-1)) {int temp = Arr[j];arr [j] = arr[i];arr[i] = temp;count_arr[arr[j]]--;}} Free (Count_arr);} int rand_int (int max) {return (rand ()% max);} void Main () {int a[100] = {0};int max = 20;srand (Time (0)), for (int i = 0; i <; i++) {A[i] = Rand_int (max);} Radix2 (A, n, max); for (int i = 0; i <; i++) {printf ("%d", A[i]);} GetChar ();}
Introduction to Algorithms third Edition study questions 8-2.E