整数数据去重和排序的神秘技巧,适用于数据最大值不大的情况(比如数据是0-1000的随机数)

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 
 6 // 输入参数:数据数组指针,数组长度
 7 int sort(int *a,int n)
 8 {
 9     int t[1001]={0};
10     int i;
11     for (i=0;i<n;i++){
12         t[a[i]] = 1;  // t[a[i]] += 1;  // 用于计数
13     }
14     for (i=0;i<1024;i++){
15         if(t[i])
16         printf("%d
",i);
17     }
18     return 0;
19  
20 }

核心思路就是将数据的值作为一个临时数组t的索引,索引对应的值为非0,最后输出所有非空的索引即可。

原文地址:https://www.cnblogs.com/gsp1004/p/11079126.html