赞
踩
用户选择数据规模分为三种10、1000、10000.选择后随机生成相关规模的数据保存在文件data_wait.txt中,每随机生成一组数据,就会覆盖前一组随机数据,便于读取排序,然后调用排序算法函数进行排序,将已排序数据序列保存于文件data_ordlely.txt中,该文件中保存多次执行结果。
该代码还有许多可修改完善之处,仅供读者参考。
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #define MaxScale 10001 int choice_Scale; //数据规模 void InsertSort(int num[], int n) { //直接插入排序,从小到大 ,下标1开始有效 int i,j; for(i = 2; i <= n; i++) { if(num[i] < num[i-1]) { num[0] = num[i]; for(j = i-1; num[j] > num[0]; j--) { num[j+1] = num[j]; } } } } void ShellSort(int num[], int n) { //希尔排序,增量选择为2的k次方减1,从小到大,起始下标1 int k, s,d,i,j; for(k = log(n)/log(2); k >= 1; k--) { d = pow(2,k)-1; // printf("\nd = %5d,k = %d\n",d,k); for(s = 1; s <= d; s++) { for(i = s+d; i <= n; i+=d) { if(num[i] < num[i-d]) { num[0] = num[i]; for(j = i-d; j > 0 && num[j] > num[0]; j-=d) { num[j+d] = num[j]; } num[j+d] = num[0]; } } } } } void BubbleSort(int num[], int n) { //冒泡排序,从小到大,起始下标1 int i,j, t , f = 1; for(i = 1; i <= n; i++) { for(j = 1; j < n-i; j ++) { if(num[j] >num[j+1]) { t = num[j]; num[j] = num[j+1]; num[j+1] = t; f = 0; } } if(f) { break; } } } //移动low、high进行记录交换 int AdjustAarry(int num[], int low, int high) { int i = low,j = high; num[0] = num[low]; //将子表的第一个记录作为枢轴记录; while(i<j) { while(i<j && num[j] > num[0]) { j--; } if(i<j) { num[i] = num[j]; //将比枢轴记录小的移到左侧; i++; } while(i<j && num[i] < num[0]) { i++; } if(i<j) { num[j] = num[i]; //将比枢轴记录大的移到右侧 j--; } } num[i] = num[0]; //枢轴记录放在合适位置 return i; } void QuickSort(int num[], int low, int high) { //快速排序,从小到大,起始下标1 int m; if(low < high) { m = AdjustAarry(num, low, high); QuickSort(num, low, m-1); //递归处理左右子表; QuickSort(num, m+1, high); } } void SelectSort(int num[], int n) {//选择排序 int i,j, t; for(i = 1; i < n ; i++) { t = i; //t指向此趟排序中关键字最小的记录 for(j = i+1; j <= n; j++) { if(num[t] > num[j]) { t = j; } } if(i != t) { //交换num[0]与num[i] num[0] = num[i]; num[i] = num[t]; num[t] = num[0]; } } } void AdjustHeap(int num[], int start, int end) { int dad = start, son = 2*dad; /*如果 num[dad] < num[2*dad],交换num[dad]和num[son]。交换后,判断num[son]为根的子树是否为堆, 若不是,则重复上述过程,将以num[son]为根的子树调整为堆,直至到叶子结点 */ while(son <= end) { if(son+1 <= end && num[son] < num[son+1]) { son++; } if(num[dad] < num[son]) { num[0] = num[dad]; num[dad] = num[son]; num[son] = num[0]; dad = son; son = 2*dad; } else { //如果num[dad] >= num[son],说明以r[2s]为根的子树已经是堆,不做调整 return; } } } void HeapSort(int num[], int n) {//堆排序 int i = n; while(i > 0) { AdjustHeap(num, i, n); //建初堆 i--; } for(i = 1; i < n; i++) { num[0] = num[1]; num[1] = num[n+1-i]; num[n+1-i] = num[0]; AdjustHeap(num,1, n-i); } } //归并两组有序数。从小的到大 void merge(int num[], int low, int mid, int high) { int i = low, j = mid+1, *pt = NULL, t = 0; pt = (int *)malloc(sizeof(int) * (high - low +1)); while(i <= mid && j <= high) { //将num中记录由小到大地并入pt中 if(num[i] > num[j]) { pt[t] = num[j]; j++; } else { pt[t] = num[i]; i++; } t++; } while(i <= mid) pt[t++] = num[i++]; //将剩余的num[i..mid]复制到pt中 while(j <= high) pt[t++] = num[j++]; //将剩余的num[j.high]复制到pt中 t = 0; i = low; while(i <= high) { num[i++] = pt[t++]; //将pt中的值存进num } free(pt); } //归并排序,从小到大,递归 void MergeSort(int num[], int low, int high) { int mid; if(low < high) { mid = (low+ high) / 2; //将当前序列一分为二,求出分裂点mid MergeSort(num, low,mid); MergeSort(num, mid+1, high); merge(num, low, mid, high); } } void RadixSort(int num[], int n) { // 基数排序,从小到大,下标从1开始 int max = num[1], i,j,k, count = 0, *temp[10]; for(i = 2; i <= n; i++) { //找最大数 if(max < num[i]) { max = num[i]; } } //printf("%d",max); while(max) { //确定最大位数 count++; max = max/10; } //printf("%d",count); for(i = 0; i < 10; i++) { temp[i] = (int *)malloc(sizeof(int) * (n+1)); temp[i][0] = 0; } max = 1; while(count--) { for(i = 1; i <= n; i++) { //分桶 temp[num[i]/max%10][0]++; temp[num[i]/max%10][temp[num[i]/max%10][0]] = num[i]; } k = 1; for(i = 0; i < 10; i++) { //合并 for(j = 1; j <= temp[i][0]; j++) { num[k++] = temp[i][j]; } temp[i][0] = 0; } // for(i = 1; i <= n; i++){//找最大数 // printf("%5d", num[i]); // } max *= 10; } for(i = 0; i < 10; i++) { free(temp[i]); } } void createdata(int n) { FILE *fp; int data, i; fp = fopen("data_wait.txt","w"); fprintf(fp,"%d\n",n); srand((unsigned)time(NULL)); for(i = 0; i < n; i++) { data = rand(); fprintf(fp,"%d\t",data); if(!((i+1) % 10)) fprintf(fp,"\n"); } fprintf(fp,"\n"); fclose(fp); } void readdata(int a[], int n){ FILE *fp; int fn, i; fp = fopen("data_wait.txt","r"); fscanf(fp,"%d",&fn); if(fn > n){ fn = n; } for(i = 1; i <= fn; i++){ fscanf(fp,"%d",a+i); } fclose(fp); } //写入文件 void writtedata(int a[],int n) { FILE *fp; int i; fp = fopen("data_ordlely.txt","a"); fprintf(fp,"已排序数据如下:\n"); fprintf(fp,"%d\n",n); for(i = 1; i <= n; i++){ fprintf(fp,"%d\t",a[i]); if(!((i+1 )% 10)) fprintf(fp,"\n"); } fprintf(fp,"\n"); fclose(fp); } //选择数据规模 int Scalechoice() { int n; printf("请选择数据规模:\n"); printf("\t1---10\n"); printf("\t2---1000\n"); printf("\t3---10000\n"); scanf("%d",&n); if(n == 1) return 10; else if(n == 2) return 1000; else if(n == 3) return 10000; } int main(int argc, char *argv[]) { int count , data[MaxScale]; char op; count = Scalechoice(); //选取数据规模 createdata(count); //随机产生数据 readdata(data, MaxScale); InsertSort(data,count); readdata(data, MaxScale); ShellSort(data,count); readdata(data, MaxScale); BubbleSort(data,count); readdata(data, MaxScale); QuickSort(data,1,count); readdata(data, MaxScale); SelectSort(data,count); readdata(data, MaxScale); HeapSort(data,count); readdata(data, MaxScale); MergeSort(data,1,count); readdata(data, MaxScale); RadixSort(data,count); writtedata(data,count); //所排序结果打印于文件中 return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。