Basic Structure:
typedefstructarray_list {//an upgraded version of an array intlength; int*values;} array_list;voidSwap_values (array_list** ppal,intindexa,intIndexb) {//swaps the specified subscript value for an upgraded array inttemp = (*ppal)values[indexa]; (*ppal)->values[indexa] = (*ppal)values[indexb]; (*ppal)->values[indexb] =temp;}voidInit_array_list (array_list** ppal,intLength) {//initializing an upgraded version of an array(*ppal) = (array_list*)malloc(sizeof(array_list)); (*ppal)->length =length; (*ppal)->values = (int*)malloc(sizeof(int) * (*ppal)length); Init_values (ppal);}voidInit_values (array_list** Ppal) {//assigns a random value to an array of stored numbers inside the upgraded array inti =0; for(i =0; I < (*ppal)->length; i++) { (*ppal)->values[i] =Rand (); }}
first, Bubble Sort:
void bubble_sort (array_list* pal) { int i=0, j=0; for (i=0; i<pal->length;i++) { for (j=pal->length-1;j>i; j--) { if(pal->values[j] < pal->values[j-1]) { swap_ VALUES (&pal,j,j-1);}}}
second, Choose the Sort:
void selection_sort (array_list* pal) { int i=0, j=0, min=0 ; for (i=0; i<pal->length;i++) { = i; for (j=i+1; j<pal->length;j++) { if(pal->values[j] < pal-> Values[min]) { = j; } } Swap_values (&pal,min,i);} }
three, Insert Sort:
voidInsertion_sort (array_list*Pal) { intI=0, j=0, temp=0; for(i=1; i<pal->length;i++){ if(pal->values[i] < pal->values[i-1]) {temp= pal->values[i]; for(j=i-1;p al->values[j] > temp;j--) {pal->values[j+1] = pal->values[j]; } pal->values[j+1] =temp; } }}
Iv. sort of Hill:
voidShell_sort (array_list*Pal) { inti =0, j =0, k =0, increment =0, temp =0; for(increment = pal->length/2; Increment >0; Increment/=2) { for(i =0; I < increment; i++) { for(j = increment + i; J < pal->length; J + =Increment) { if(pal->values[j] < pal->values[j-increment]) {temp= pal->values[j]; for(k = j-increment; k >=0&& pal->values[k] > temp; k-=Increment) {pal->values[k + increment] = pal->values[k]; } pal->values[k + increment] =temp; } } } }}
five, Heap Sort:
Note: when using heap sorting, the array to be sorted is 1 larger than the number of data to be sorted because the No. 0 position of the array in the heap sort does not work and cannot be used to store Data.
voidHeap_sort (array_list*Pal) { inti =1; for(i = pal->length/2; I >=1; i--) {//Notice the loop condition Here. Not i>=0 .Heap_adjustion (&pal, i, pal->length-1); } for(i = pal->length-1; i >1; i--) {//Notice the cyclic condition here, not i>0 .Swap_values (&pal,1, i); Heap_adjustion (&pal,1I1); }} voidHeap_adjustion (array_list** ppal,intStartintEnd) { inti =0, temp =0; Temp= (*ppal)values[start]; for(i =2* start; I <= end; I *=2) { /*Note that the relationship between the left and right nodes can be judged under the condition of I<end.*/ if(i < end && (*ppal)->values[i] < (*ppal)->values[i +1]) i++; if((*ppal)->values[i] <= Temp) break; (*ppal)->values[start] = (*ppal)values[i]; Start=i; } (*ppal)->values[start] =temp;}
six, Merge Sort:
seven, Quick Sort:
eight, the base sort:
Data Structure--eight Big Sort