1 #include <stdio.h> 2 #include <stdlib.h> 3 4 void PopupSort(int *s, int length) 5 { 6 int i, j, swap; 7 8 for(i = 0; i < length - 1; i++) 9 { 10 for(j = 0; j < 9 - i; j++) 11 { 12 if(s[j] > s[j + 1]) 13 { 14 swap = s[j]; 15 s[j] = s[j + 1]; 16 s[j + 1] = swap; 17 } 18 } 19 } 20 } 21 22 void SelectionSort(int *s, int length) 23 { 24 int i, j, min, min_index; 25 26 for(i = 0; i < length - 1; i++) 27 { 28 min = s[i]; 29 min_index = i; 30 31 for(j = i + 1; j < length; j++) 32 { 33 if(min > s[j]) 34 { 35 min = s[j]; 36 min_index = j; 37 } 38 } 39 40 s[min_index] = s[i]; 41 s[i] = min; 42 } 43 } 44 45 void InsertSort(int *s, int length) 46 { 47 int i, j, k, insert, temp; 48 49 for(i = 1; i < length; i++) 50 { 51 for(j = 0; j < i; j++) 52 { 53 if(s[j] > s[i]) 54 { 55 insert = s[i]; 56 for(k = i; k >= j + 1; k--) 57 { 58 s[k] = s[k - 1]; 59 } 60 s[j] = insert; 61 } 62 } 63 } 64 } 65 66 67 int main() 68 { 69 int s1[] = {7, 9, 8, 6, 4, 5, 2, 3, 1}; 70 PopupSort(s1, sizeof(s1) / sizeof(s1[0])); 71 72 int s2[] = {7, 9, 8, 6, 4, 5, 2, 3, 1}; 73 74 SelectionSort(s2, sizeof(s2) / sizeof(s2[0])); 75 76 int s3[] = {7, 9, 8, 6, 4, 5, 2, 3, 1}; 77 InsertSort(s3, sizeof(s3) / sizeof(s3[0])); 78 79 system("pause"); 80 return 0; 81 }