先贴上代码,以后再慢慢分析。
1 #include <time.h> 2 3 #define MAXSIZE 6000 4 typedef struct List{ 5 int r[MAXSIZE]; 6 int length; 7 }SqList; 8 9 void swap(SqList* L, int i, int j) 10 { 11 int temp = L->r[i]; 12 L->r[i] = L->r[j]; 13 L->r[j] = temp; 14 } 15 16 void SampleSelectSort(SqList* L) 17 { 18 int i,j,min; 19 for(i=0;i<L->length;i++){ 20 min = i; 21 for(j=i+1;j<=L->length;j++) 22 { 23 if(L->r[min] > L->r[j]) 24 min = j; 25 } 26 if(i!=min){ 27 swap(L,i,min); 28 } 29 } 30 } 31 32 void MergeSort(SqList* L) 33 { 34 MSort(L->r,L->r,0,L->length-1); 35 } 36 void MSort(int SR[], int TR1[],int s, int t) 37 { 38 int m; 39 int TR2[MAXSIZE]; 40 if(s == t) 41 TR1[s] = SR[s]; 42 else{ 43 m = (s+t)/2; 44 MSort(SR,TR2,s,m); 45 MSort(SR,TR2,m+1,t); 46 Merge(TR2,TR1,s,m,t); 47 } 48 } 49 50 void Merge(int SR[], int TR[], int s, int m, int n) 51 { 52 int i,j; 53 for(i=s,j=m+1;i<=m && j<=n;s++){ 54 if(SR[i]<SR[j]) 55 TR[s] = SR[i++]; 56 else 57 TR[s] = SR[j++]; 58 } 59 int l; 60 if(i<=m){ 61 for(l=i;l<=m;l++) 62 TR[s++] = SR[l]; 63 } 64 65 if(j<=n){ 66 for(l=j;l<=n;l++) 67 TR[s++] = SR[l]; 68 } 69 } 70 void InsertSort(SqList* L) 71 { 72 int i,j; 73 int flag; 74 for(i=1;i<L->length;i++) 75 { 76 if(L->r[i]<L->r[i-1]){ 77 flag = L->r[i]; 78 for(j=i-1;L->r[j]>flag;j--){ 79 L->r[j+1] = L->r[j]; 80 } 81 L->r[j+1] = flag; 82 } 83 } 84 } 85 86 void HeapAdjust(SqList* L, int s, int n) 87 { 88 int temp,j,i; 89 temp = L->r[s]; 90 for(j=2*s;j<n;j*=2){ 91 if(j<n && L->r[j] > L->r[j+1]) 92 j++; 93 if(temp <= L->r[j]) 94 break; 95 L->r[s] = L->r[j]; 96 s = j; 97 } 98 L->r[s] = temp; 99 } 100 101 void HeapSort(SqList * L) 102 { 103 int i; 104 for(i=(L->length-1)/2-1;i>0;i--) 105 { 106 HeapAdjust(L,i,L->length-1); 107 } 108 for(i=0;i<L->length;i++){ 109 printf("%d ",L->r[i]); 110 } 111 printf("\n"); 112 for(i=L->length;i>2;i--){ 113 swap(L,1,i-1); 114 HeapAdjust(L,1,i-2); 115 } 116 } 117 void ShellSort(SqList* L) 118 { 119 int i,j; 120 int step = L->length; 121 int flag; 122 do{ 123 step = step/3 + 1; 124 for(i=step+1;i<L->length;i++){ 125 if(L->r[i]<L->r[i-step]){ 126 flag = L->r[i]; 127 for(j=i-step;((j>0)&&(L->r[j]>flag)); j-=step){ 128 L->r[j+step] = L->r[j]; 129 } 130 L->r[j+step] = flag; 131 } 132 } 133 }while(step>1); 134 } 135 136 void QuickSort(SqList*L) 137 { 138 QuickSortPart(L->r,0,L->length-1); 139 } 140 141 void QuickSortPart(int array[], int low, int high) 142 { 143 if(low < high){ 144 int i = QuickArraySort(array,low,high); 145 QuickSortPart(array,low,i-1); 146 QuickSortPart(array,i+1,high); 147 } 148 149 } 150 151 int QuickArraySort(int array[],int low, int high) 152 { 153 int orig = array[low]; 154 while(low<high){ 155 while(low<high && array[high]>orig) 156 high--; 157 if(low<high){ 158 array[low] = array[high]; 159 low++; 160 } 161 while(low<high && array[low]<orig) 162 low++; 163 if(low<high){ 164 array[high] = array[low]; 165 high--; 166 } 167 } 168 array[low] = orig; 169 return low; 170 } 171 172 #define random(x) (rand()%x) 173 174 int main() 175 { 176 SqList* L; 177 int i; 178 srand(time(NULL)); 179 int a[MAXSIZE]; 180 for(i = 0; i < MAXSIZE; i++) 181 a[i] = random(2147483647); 182 183 L = (SqList *)malloc(sizeof(struct List)); 184 L->length = MAXSIZE; 185 memcpy(L->r,a,MAXSIZE*sizeof(int)); 186 187 MergeSort(L); 188 189 for(i=0;i<100;i++){ 190 printf("%d ",L->r[i]); 191 } 192 printf("\n"); 193 return 0; 194 }
时间: 2024-10-10 00:38:17