Sorting is a common problem in life, and according to the main operation of the sorting process, we divide the inner sort into: inserting sort, exchanging sort, selecting sort and merging sort. These are relatively mature sorting algorithms, and the main purpose of our study of these sorting algorithms is to improve our ability to write algorithms by learning them to solve more complex and flexible application problems.
Bubble sort (Bubble sort)
Basic idea: 22 compare the keywords of adjacent records, if reversed, until there is no reverse order.
Algorithm implementation:
void bubblesort (sqlist *list) { int I, J;
int flag = TRUE; for (i=1; i<list->length && flag; i++) {
flag = flase; for (j=list->length-1; j>=i; j--) { if(List->r[j] > list->r[j+1]) { swap (list, J, J+1 );
flag = TRUE; / * If there is no data exchange, the description is orderly and avoids meaningless circular judgments */ }}}
Simple selection sort (Easy Selection sort)
basic idea: through the comparison between N-i sub-keywords, select the smallest record of the keyword from the n-i+1 record, and exchange it with the first record.
Algorithm implementation:
void selectsort (sqlist *list) { int i, J, Min; for (i=1; i<list->length; i++) { = i; for (j=i+1; j<=list->length; j + +) { if(List->r[min] > list->r[j]) = j; } if (I! = min) swap (list, I, min);} }
Direct Insert Sort (strairht insertion sort)
basic idea: inserting a record into an ordered table that is already ordered, thus obtaining a new ordered table with a 1 increase in the number of records.
Algorithm implementation:
voidInsertsort (SqList *list) { intI, J; for(i=2; i<=list->length; i++) { if(List->r[i] < list->r[i-1])/*need to insert*/{List->r[0] = list->r[i];/*Set Sentinel*/ for(j=i-1; list->r[j]>list->r[0]; j--) {List->r[j+1] = list->r[j];/*record and move back*/} list->r[j+1] = list->r[0];/*Insert to correct position*/ } }}
Hill sort (Shell sort)
Rationale: the records to be sorted into a number of sub-sequences, so that the number of sub-sequence records are less, and then the sequence of these sub-sequences are directly inserted into the order, when the whole sequence is basically ordered, and then a direct insertion of the whole record to sort. The segmentation strategy is to make a sub-sequence of the records of an "increment", so as to ensure that the results obtained in the sub-sequence are basically ordered rather than locally ordered.
For the incremental selection, it is still a mathematical problem, so far there is no one of the best incremental sequences. In addition, because the record is a jumping move, hill sort is not a stable sorting algorithm.
Quick Sorting ( quick Sort)
basic idea: to divide the records to be sorted into two separate parts through a sort of sequencing, in which some of the recorded keywords are smaller than those of the other, the two parts of the records can be sorted separately in order to achieve the order of the whole sequence.
Algorithm implementation:
voidQuickSort (SqList *list) {qsort (list,1, list->length);}voidQsort (SqList *list,intLowintHigh ) { intpivot; if(Low <High ) {Pivot= Partition (list, low, high);/*in Split, calculate the pivot value*/qsort (list, low, pivot-1);/*recursive ordering of low sub-tables*/qsort (list, pivot+1, high);/*recursive ordering of Gaozi*/ }}intPartition (SqList *list,intLowintHigh ) { intPivotKey; PivotKey= list->r[low];/*use the first record of a word table as a pivot value*/ while(Low < High)/*alternate to intermediate scan from both ends of the table*/ { while(LowPivotKey) High--; Swap (list, low, high); /*swap records that are smaller than the pivot value to the low end*/ while(Lowpivotkey) Low--; Swap (list, low, high); /*switching records larger than pivot values to high-end*/ } returnLow/*returns the location of the pivot value*/}
Quick Sort Optimization:
1. Optimize the selection of the pivot value: three.
2. Optimization of unnecessary exchanges
3. Optimizing the sorting scheme for fractional groups
4. Optimize recursive operations
Ordering of Algorithm Basics