C语言排序算法

1 #include <stdio.h>
2
3 void swap(int *a,int *b)
4 {
5 int t;
6 t=*a; *a=*b; *b=t;
7 }
8
9 void selection_sort(int s[],int n){
10 int i,j;
11 int min ;
12 for(i=0;i<n;i++){
13 min=i;
14 for( j=i+1;j<n;j++){
15 if(s[j]<s[min]) min=j;
16 }
17 swap(&s[i],&s[min]);
18 }
19 }
20
21 void insertion_sort(int s[],int n){
22 int i,j;
23 for(i =1;i<n;i++){
24 j=i;
25 while((j>0)&&(s[j]<s[j-1])){
26 swap(&s[j],&s[j-1]);
27 j--;
28 }
29
30 }
31
32 }
33
34 int partition(int s[], int l, int h){
35 int i;
36 int p;
37 int firsthigh;
38
39 p=h;
40 firsthigh=l;
41 for(i=l;i<h;i++)
42 if(s[i]<s[p]){
43 swap(&s[i],&s[firsthigh]);
44 firsthigh++;
45
46 }
47 swap(&s[p],&s[firsthigh]);
48
49 return (firsthigh);
50
51 }
52
53 void quick_sort(int s[],int l,int h){
54 int p;
55 if((h-1)>0){
56 p=partition(s,l,h);
57 quick_sort(s,l,p-1);
58 quick_sort(s,p+1,h);
59 }
60
61 }
62
63
64 int main(void){
65 int s[10]={5,8,6,9,3,2,1,4,7,0};
66
67
68 //selection_sort(s,10);
69 insertion_sort(s,10);
70 //quick_sort(s,3,8);
71 for(int i=0;i<10;i++)
72 printf("%d\n",s[i]);
73
74 return 0;
75 }
原文地址:https://www.cnblogs.com/lfzark/p/2002706.html