欢迎来到天天文库
浏览记录
ID:38628813
大小:98.50 KB
页数:17页
时间:2019-06-16
《排序算法经典集锦(C、C#)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、名称:直接插入排序[稳定的排序]思想:每次从无序表中取出第一个元素,把它插入到有序表的合适位置,使有序表仍然有序时间复杂度:0(n^2)实现语言:C语言编译环境:TC3.0#include/*标准输入输出头文件*/#include/*标准库头文件*/voidInsert_Sort(int*arr,intlen){ inti,j,temp; for(i=1;i=0&&temp<*(a
2、rr+j);j--)*(arr+j+1)=*(arr+j);//后移 *(arr+j+1)=temp; //插入到正确位置} }intmain(){inti,array[10];randomize();//初始化随机数发生器,该函数定义在库文件中for(i=0;i<10;i++)array[i]=rand()%100;//rand:伪随机数发生器,获取0到99的随机数Insert_Sort(array,10);printf("Theresultis:");for(i=0;i<10;i++)pr
3、intf("%d",array[i]);return0;}名称:直接插入排序实现语言:C#语言编译环境:VS2010usingSystem;usingSystem.Collections.Generic;classProgram{staticvoidMain(string[]args){Randomrand=newRandom();int[]array=newint[10];for(inti=0;i<10;i++)array[i]=rand.Next(100);Insertion_Sort(array);Console
4、.WriteLine("随机生成10个数(0~99)并排序输出为:");foreach(intiteminarray){Console.Write(item);Console.Write("");}Console.ReadKey();}staticvoidInsertion_Sort(int[]arr){inti,j,temp;for(i=arr.GetLowerBound(0)+1;i=0&&arr[j]>tem
5、p;j--)arr[j+1]=arr[j];arr[j+1]=temp;}}}名称:希尔排序(缩小增量排序)[不稳定的排序]思想:将整个待排序记录序列分割成为若干个子序列分别进行直接插入排序,待整个序列中的记录“基本有序“时,再对全体记录进行一次直接插入排序时间复杂度:0(nlogn)~0(n^2)实现语言:C语言编译环境:TC3.0#include#includevoidShell_Sort(int*arr,intlen){inti,j,h,temp;for(h=len/2;h>
6、0;h/=2)/*控制增量*/{for(i=h;i=0&&temp<*(arr+j);j-=h)*(arr+j+h)=*(arr+j);*(arr+j+h)=temp;}}}intmain(){inti,array[10];randomize();for(i=0;i<10;i++)array[i]=rand()%100;Shell_Sort(array,10);printf("Theresultis:");for(
7、i=0;i<10;i++){printf("%d",array[i]);}return0;}名称:希尔排序(缩小增量排序)实现语言:C#语言编译环境:VS2010usingSystem;usingSystem.Collections.Generic;classProgram{staticvoidMain(string[]args){Randomrand=newRandom();int[]array=newint[10];for(inti=0;i<10;i++)array[i]=rand.Next(100);Shell_
8、Sort(array);Console.WriteLine("随机生成10个数(0~99)并排序输出为:");foreach(intiteminarray){Console.Write(item);Console.Write("");}Console.ReadKey();}staticvoidShell_Sort(int[]arr
此文档下载收益归作者所有