#include <stdio.h>#include<stdlib.h>#include<Windows.h>//Direct Insert SortvoidInsertsort (intArry[],intN) { intI, J; intTemp//Temp Variable for(i =1; I < n; i++) {Temp=Arry[i]; for(j = i-1; J >=0; j--) { if(Temp >Arry[j]) Break; Elsearry[j+1] =Arry[j]; } arry[j+1] =temp; }}//Direct Select SortvoidSelectsort (intArry[],intN) { intI, J; inttemp; for(i =0; I < n1; i++) {Temp=i; for(j = i +1; J < N; J + +) { if(Arry[j] <arry[temp]) Temp=J; } if(temp!=i) arry[temp]^= Arry[i] ^= arry[temp] ^= arry[i];//implements ARRY[TEMP] and Arry[i] Value Exchange }}//Bubble SortvoidBubblesort (intArry[],intN) { intI, j, K; for(i =0; I < n1; i++) { for(j = i; J < N; j + +) { if(Arry[j-1]>Arry[j]) arry[j-1] ^= Arry[j] ^= arry[j-1] ^= Arry[j];//implements ARRY[TEMP] and Arry[i] Value Exchange } }}//Hill sort First class,voidShellSort1 (intArry[],intN) { intI, J, K, Gap,temp; for(GAP = n/2; Gap >0; Gap/=2) { for(k =0; K < Gap; k++)//same group first row { for(i = k+gap; i < n; i + =Gap) {Temp=Arry[i]; for(j = i-gap; J >=0; J-=Gap) { if(Temp >Arry[j]) Break; Elsearry[j+ Gap] =Arry[j]; } arry[j+ Gap] =temp; } } }}//Hill sort Second ClassvoidShellSort2 (intArry[],intN) { intI, J, K, Gap, temp; for(GAP = n/2; Gap >0; Gap/=2) { for(i = gap; i < n; i++)//sort all groups together{Temp=Arry[i]; for(j = i-gap; J >=0; J-=Gap) { if(Temp >Arry[j]) Break; Elsearry[j+ Gap] =Arry[j]; } arry[j+ Gap] =temp; } }}//Hill sort, using gap grouping after bubble sortvoidSHELLSORT3 (intArry[],intN) { intI, J, K, Gap, temp; for(GAP = n/2; Gap >0; Gap/=2) { for(k =0; K < Gap; k++) { for(i = k; i < n-gap; i + =Gap) { for(j = k+gap; J <n; J + =Gap) { if(arry[j-gap]>Arry[j]) arry[j-gap] ^= Arry[j] ^= Arry[j-gap] ^=Arry[j]; } } } }}//Quick Sort, recursive callvoidQuickSort (intArry[],intSintt) { intI, J; if(S <t) {i=s; J= t+1; while(1) { Do{i++; } while(! (Arry[s] <= Arry[i] | | I >=t)); Do{J--; } while(! (Arry[s] >= Arry[j] | | J <=s)); if(I <j) Arry[i]^= Arry[j] ^= Arry[i] ^=Arry[j]; Else Break; } if(arry[j]!=Arry[s]) arry[j]^= Arry[s] ^= Arry[j] ^=Arry[s]; QuickSort (Arry, S, J-1); QuickSort (Arry, J+1, T); }}intMain () {intarry1[ the] = { +, the, -,Ten,3, -, at, the,5, -,7, -, +,2, A}; intarry2[ the] = { +, the, -,Ten,3, -, at, the,5, -,7, -, +,2, A }; intarry3[ the] = { +, the, -,Ten,3, -, at, the,5, -,7, -, +,2, A }; intarry4[ the] = { +, the, -,Ten,3, -, at, the,5, -,7, -, +,2, A }; intarry5[ the] = { +, the, -,Ten,3, -, at, the,5, -,7, -, +,2, A }; intarry6[ the] = { +, the, -,Ten,3, -, at, the,5, -,7, -, +,2, A }; intarry7[ the] = { +, the, -,Ten,3, -, at, the,5, -,7, -, +,2, A }; //Seven ways to choose one, but with different efficiency//General: Quick Sort > Direct Insert > Direct select = Bubble//Hill sort depends on the situation. Generally better than direct selection sorting//Insertsort (Arry1, 15); //Selectsort (Arry2, 15); //Bubblesort (Arry3, 15); //ShellSort1 (Arry4, 15); //ShellSort2 (Arry5, 15); //ShellSort2 (Arry6, 15); //QuickSort (arry7,0,14); inti =0; for(i =0; I < the; i++) {printf ("%d", Arry7[i]); } System ("Pause");}
Several common sorting methods (C language Implementation)