Definition of sorting
The process of arranging a given number of data elements into an ordered sequence according to the specified data items is called sorting.
Inner sorting
The process of reading the data to be sorted into memory at one time and completing the sorting is called internal sorting.
External sorting
The process of reading the data to be sorted into memory for many times and completing the sorting is called external sorting.
Implementation of sequential storage of data elements
typedef struct entry{ //data elements KeyType key; //Sort keyword, KeyType should be comparable DataType data; //Data protects other data items in data elements }Entry; typedef struct list{ //Sequence table int n; //Amount of data elements to be sorted Entry D[MaxSize]; //Static array storage elements }List;
Simple selection sorting algorithm
The core idea of the algorithm is to find the data element with the smallest keyword in the sequence to be sorted, exchange its position with the first data element in the sequence to be sorted, remove it from the next sequence to be sorted, and repeat the process until there are only two data elements left in the sequence to be sorted in a certain sequence.
int FindMin(List list,int startIndex){ int i,minIndex=startIndex; for(i=startIndex+1;i<list.n;i++){ if(list.D[i].key<list.D[minIndex].key) minIndex=i; } return minIndex; } void Swap(Entry *D,int i,int j){//Exchange the position of two elements in the sequence table Entry temp; if(i==j){ return; } temp=*(D+i); *(D+i)=*(D+j); *(D+j)=temp; } void SelectSort(List *list){ int minIndex,startIndex=0; while(startIndex<list->n-1){ minIndex=FindMin(*list,startIndex); Swap(list->D,startIndex,minIndex); startIndex++; } }
Direct insertion sorting algorithm
The core of the sorting algorithm is: start from an ordered sequence containing only one element, and continuously insert the data elements to be sorted into the ordered sequence until the ordered sequence contains all the data elements to be sorted.
void InsertSort(List *list){ int i,j; Entry insertItem; for(i=1;i<list->n;i++){ insertItem=list->D[i]; for(j=i-1;j>=0;j--){ if(insertItem.key<list->D[j].key) { list->D[j+1]=list->D[j]; } else break; } list->D[j+1]=list->D[j]; } }
Bubble sorting
The core idea is: continuously exchange adjacent data elements in reverse order from front to back, and repeat the process until any adjacent elements are no longer arranged in reverse order.
typedef int BOOL; void BubbleSort(List *list) { int i,j;//i refers to the subscript of the last element of each sorting range for(i=list->n-1;i>0;i--) { BOOL isSwap=FALSE; for(j=0;j<i;j++) { if(list->D[j].key>list->D[j+1].key) { Swap(list->D,j,j+1); isSwap=TRUE; } } if(!isSwap) break; } }
Quick sorting algorithm
The core idea of the algorithm is:
(1) Select a split element in the sequence to be sorted, move all elements smaller or equal than the split element keyword in the sequence to be sorted to the left, and move all elements larger or equal than the split element keyword in the sequence to be sorted to the right of the split element;
(2) Then, all the elements on the left side of the split element are regarded as a sub sequence to be sorted, and the above process is repeated until these elements are completely ordered;
(3) Finally, all the elements on the right side of the split element are regarded as a subsequence to be sorted, and the above process is repeated until these elements are completely ordered.
//Sequence division int Patition(List *list,int low,int high){ int i=low,j=high+1; Entry pivot=list->D[low];//pivot is a split element do { do { i++; }while(i<high&&list->D[i].key<pivot.key); do{ j--; }while(list->D[j].key>pivot.key); if(i<j) { Swap(list->D,i,j); } }while(i<j); Swap(list->D,low,j) return j; } //Quick sorting algorithm void Quicksort(List *list,int low,int high)//Recursive function { int k; if(low<high) { k=Partition(list,low,high); QuickSort(list,low,high); QuickSort(list,k+1,high); } } void QuickSort(List *list) { QuickSort(list,0,list->n-1); }