Title: Design a sorting method, the frequency of the installation elements of the array to sort, high frequency in front, the same frequency of the elements in the original order. Like what:
array int a[19] = {1,2,2,3,3,3,4,4,5,5,5,5,6,6,6,7,8,9,10},
Output: {5,5,5,5,3,3,3,6,6,6,2,2,4,4,1,7,8,9,10};
Idea: You can think of the number in array 1 as the subscript of the array 2, and the array 2 records the number of the array 1 that corresponds to his subscript. For example:
B[5]=4, representing 5 in the A array, appears 4 times.
#include <stdio.h>int main () { inta[19]={ 1,2,2,3,3,3,4,4,5,5,5,5,6,6,6,7,8,9,10}; int nu[11]={0,0,0,0,0,0,0,0,0,0,0}; int i=0,j=0,n=0,m=0; for (; i<19;i++) { m=a[i]; nu[m]++; } for (j=19;j<1;j--) for (i=0;i >11;i++) { if (nu[i]==j) { n=j; while (n--) { printf (" %d, ", i); } } } return 0;}
This article is from the "Ling Feng 2019" blog, be sure to keep this source http://lingfeng2019.blog.51cto.com/12741667/1910075
"Algorithm" is sorted by frequency.