排序原理及算法复杂度计算见“经典排序算法及时间复杂度计算-c语言-动画演示”,链接:https://blog.csdn.net/yq272393925/article/details/89132353。
以下内容为c语言实现干货,在linux平台下编译运行并查看结果。
0.编译及运行脚本(build.sh)
执行 sh build.sh ,输入排序数据数量,即可运行查看结果
#!/bin/bash
clean=$1
if [ "$clean" = "clean" ]
then
rm -rf *.txt
rm -rf a.out
exit
fi
if [ "$1" = "-g" ]
then
gcc -g sort.c utils.c -o mysort
exit
fi
gcc sort.c utils.c -o mysort
./mysort
md5sum *txt
1.util.h
#include <stdio.h>
#define ASC 0
#define DESC 1
#define DATA_SCOPE 100
void data2file(int *data,int len, char *filename);
void gendata(int n, int *data);
void bubble_sort(int *data, int len,int typeflag);
void select_sort(int *data, int len,int typeflag);
void insert_sort(int *data, int len,int typeflag);
void insert_sort_good(int *data, int len,int typeflag);
2.utils.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "util.h"
void swap(int *a, int *b)
{
int tmp = -1;
tmp = *a;
*a = *b;
*b =tmp;
}
void bubble_sort(int *data, int len,int typeflag)
{
int i = 0;
int j = 0;
int tmp = -1;
for(i = 0; i < len; i++)
{
for(j = 0; j < len - i - 1; j++)
{
if(typeflag==ASC && data[j] > data[j+1])
swap(data+j,data+j+1);
if(typeflag==DESC && data[j] < data[j+1])
swap(&data[j],&data[j+1]);
}
}
}
void select_sort(int *data, int len,int typeflag)
{
int i = 0;
int j = 0;
int idx = -1;
for(i = 0; i < len -1 ; i++){
idx = i+1 ;
for(j = i ; j < len ; j++){
if(typeflag==ASC && data[idx] > data[j])
idx = j;
if(typeflag==DESC && data[idx] < data[j])
idx = j;
}
if(idx != i)
swap(data+i,data+idx);
}
}
void insert_sort(int *data, int len,int typeflag)
{
int i = 0;
int j = 0;
int k = 0;
int tmpmax = -1;
int idx = -1;
for(i=0; i<len-1; i++){
idx = -1;
for(j=0; j<i+1 ;j++){
if(typeflag == ASC && data[i+1] < data[j] ){
idx = j;
break ;
}
if(typeflag == DESC && data[i+1] > data[j] ){
idx = j;
break ;
}
}
if(idx == -1)
continue ;
else{
tmpmax = data[i+1];
for(k = i+1; k>idx;k--){
data[k]=data[k-1];
}
data[idx] = tmpmax;
}
}
}
void insert_sort_good(int *data, int len,int typeflag)
{
int i = 0;
int preindex = 0;
int k = 0;
int current = -1;
int idx = -1;
for(i=0; i<len-1; i++){
idx = -1;
current = data[i+1];
preindex = i;
while(preindex >=0){
if( (typeflag == ASC && current < data[preindex] ) ||
(typeflag == DESC && current > data[preindex] ) )
{
data[preindex+1] = data[preindex];
preindex--;
continue;
}
break;
}
data[preindex+1] = current;
}
}
void shell_sort(int *data, int len, int typeflag)
{
int current = -1;
int preindex = -1;
int i = 0,j = 0;
int gap = 0;
gap = len/2;
while(gap > 0){
for(i = gap; i<len; i++){
current = data[i];
preindex = i-gap;
while(preindex >=0) {
if( (typeflag == ASC && current < data[preindex] ) ||
(typeflag == DESC && current > data[preindex] ) )
{
data[preindex+gap] = data[preindex];
preindex = preindex-gap;
continue;
}
break;
}
data[preindex+gap] = current;
}
gap = gap/2;
}
}
void data2file(int *data,int len, char *filename)
{
FILE *fp = NULL;
int i = 0;
if((fp=fopen(filename,"w"))==NULL)
{
printf("file cannot open \n");
exit(0);
//头文件#include <stdlib.h>
//exit结束程序,一般0为正常推出,其它数字为异常,其对应的错误可以自己指定。
}
fprintf(fp,"%d\n",len);
for(i = 0; i < len; i++)
{
if((i+1)%10 == 0 )
fprintf(fp,"%6d \n",data[i]);
else
fprintf(fp,"%6d ",data[i]);
}
if(fclose(fp)!=0)
printf("file cannot be closed \n");
else
printf("file is now closed \n");
}
void gendata(int n, int *data)
{
int i = 0;
srand((unsigned)time(NULL));
for(i=0;i<n;i++)
data[i] = rand()%DATA_SCOPE;
}
3.sort.c
#include <stdio.h>
#include "util.h"
void main()
{
int count = 0;
int n = 0;
int *org = 0x0;
int *sort = 0x0;
printf("本程序随机生成n个整型数据至data.txt中,请选择如下排序算法,对数据进行排序\n");
printf("\n");
printf("----------------------------------------------------------\n");
printf("\n");
printf("随机产生n个整数(0-100000)至data.txt文件\n");
printf("\n");
printf("数据格式首行单个数字,表示数据中整数的个数\n");
printf("\n");
printf("其余行每行10个整数\n");
printf("----------------------------------------------------------\n");
printf("\n");
printf("输入随机数数量:%d\n",n);
scanf("%d",&n);
//1.生成原始数据
org = (int *)malloc(n * sizeof(int));
memset(org, 0, n * sizeof(int));
gendata(n, org);
data2file(org,n,"org.txt");
sort = (int *)malloc(n * sizeof(int));
memset(sort, 0, n * sizeof(int));
//2.冒泡排序
memcpy(sort,org,n * sizeof(int));
bubble_sort(sort, n, ASC);
data2file(sort,n,"bubble_sort.txt");
//3.选择排序
memcpy(sort,org,n * sizeof(int));
select_sort(sort, n, ASC);
data2file(sort,n,"select_sort.txt");
//4.插入排序,两步骤
memcpy(sort,org,n * sizeof(int));
insert_sort(sort, n, ASC);
data2file(sort,n,"insert_sort.txt");
//5.插入排序,while循环
memcpy(sort,org,n * sizeof(int));
insert_sort_good(sort, n, ASC);
data2file(sort,n,"insert_sort_good.txt");
//6.希尔排序,while循环
memcpy(sort,org,n * sizeof(int));
shell_sort(sort, n, ASC);
data2file(sort,n,"shell_sort.txt");
free(org);
org = 0;
free(sort);
sort = 0;
}