this afternoon just finished counting the sorting, so write down the strike.
#include <iostream>using namespace Std;int main () {int a[10000], k, N, Max; Array A is an input array of int c[10000], b[10000]; Array B stores the sorted output. Array C provides temporary storage space cin >> n;for (int i=1; i<=n; i++) cin >> A[i];max = a[0]; Find the maximum value in array a for the value of K for (int i=1; i<=n; i++) {if (A[i] > Max) max =a[i];} K = max;for (int i=0; i<=k; i++) //Array color c for initialization {c[i] = 0;} for (int j=1; j<=n; J + +) {C[a[j]] = c[a[j]] +1; Find out the number of elements in array a}for (int i=1; i<=k; i++) {c[i] = c[i] +c[i-1]; Calculate the number of}for (int j = n; j>=1; j--) {b[c[a[j]] [= a[j] less than or equal to I; It is easy to preoccupied here. The explanation ———————— A[j] should be put in which position, answer: should be placed in C[a[j]] position c[a[j]] = C[a[j]]-1;} for (int i=1; i<=n; i++) {cout << b[i] << " ";} return 0;}
Also saw on the internet a well-written blog post, copy come and see it
#include <stdio.h> #include <stdlib.h> #include <time.h>/* run this program using the console Pauser or ad D your own getch, System ("pause") or input loop */void print_arry (int *arr,int n) {int i;for (i = 0; i<n; i++) {printf ("%d ", Arr[i]);} printf ("\ n");} void Count_sort (int *arr, int *sorted_arr, int n) {int *count_arr = (int *) malloc (sizeof (int) *); int i;//Initialize Count array for (i = 0; i<100; i++) Count_arr[i] = 0;//statistics I number of times for (i = 0;i<n;i++) count_arr[arr[i]]++;//to all counts accumulate for (i = 1; i<100; i++) Count_arr[i] + = Count_arr[i-1]; Inverse traversal of the source array (guaranteed stability), populated with the corresponding values in the count array into the first array for (i = n; i>0; i--) {sorted_arr[count_arr[arr[i-1]]-1] = arr[i-1];count_arr[ arr[i-1]]--;} Free (Count_arr);} int main () {int n,i;printf ("size of the array to be sorted n="), scanf ("%d", &n), int *arr = (int *) malloc (sizeof (int) * n); int *sorted_ar R = (int *) malloc (sizeof (int) * n); Srand (Time (0)); for (i = 0; i<n; i++) {Arr[i] = rand ()% 100;} printf ("Randomly generated array with numeric value of 0~99 ... \ n");p rintf ("Initialize array:");p Rint_arry (arr, n); Count_sort (arr, Sorted_arr, N);p rintf ("sorted array:"); Print_arry (Sorted_arr, n); return 0;system ("pause");}
Counting sorting algorithm