There are many kinds of sorting algorithms, this paper mainly introduces the basic sorting algorithm and implementation, and analyzes the complexity and stability.
一、0 (n2) algorithm
1. Insert Sort
Insert sort very well, the process of selecting a numeric value in an unordered array and inserting it into an ordered array is stable. The implementation code is as follows:
1Template <typename t>2 voidInsertionsort (Vector<t> &arr) {3 intI, J;4 for(i =1; I < arr.size (); i++) {5 inttemp =Arr[i];6 for(j = i-1; J >=0&& Arr[j] > temp; j--)7Arr[j +1] =Arr[j];8Arr[j +1] =temp;9}
Insertionsort
2. Bubble sort
The process of bubbling the sort is to iterate through the array, and each time you pick a value to compare forward, it is larger than the previous value, and the process itself is stable until it encounters a larger value or iterates through the array.
Template <typename t>voidBubblesort (Vector<t> &arr) { for(inti =0; I! = Arr.size (); i++){ for(size_t j =0; J < Arr.size ()-1-I.; J + +) { if(Arr[j] > arr[j +1]) {Arr[j]= Arr[j] ^ arr[j+1]; Arr[j+1] = Arr[j] ^ arr[j+1]; ARR[J]= arr[j+1] ^Arr[j]; } } }}
Bubblesort
二、0 (NLOGN) algorithm
1. Merge sort
Merge sort is a typical divide-and-conquer algorithm, which divides the original problem into disjoint sub-problem. The code is as follows:
1 voidMerge (vector<int> &arr,intLintMintR) {2vector<int>Left , right;3 intN1 = m-l;intN2 = R-m;4 for(inti =0; I <= N1; i++)5Left.push_back (Arr[l +i]);6Left.push_back (infinite);//Sentinel Value7 for(inti =0; I! = n2; i++)8Right.push_back (arr[m + i +1]);9 right.push_back (infinite);Ten intIndex1 =0;intIndex2 =0; One for(inti = l; I! = r +1; i++){ A if(Left[index1] <Right[index2]) -Arr[i] = left[index1++]; - Else theArr[i] = right[index2++]; - } - }; - + - voidMergeSort (vector<int> &arr,intLintR) { + A if(L <R) { at intm =int(L + R)/2); - mergesort (arr, L, m); -MergeSort (arr, M +1, R); - Merge (arr, L, M, R); - } -}
MergeSort
2. Heap Sequencing
The nature of the heap is to maintain a maximum heap (minimum heap), so his time complexity is associated with the nature of the largest heap, the Deletemax operation of a maximum heap is O (1), and each time after the Deletemax to maintain the nature of the maximum heap, so the time complexity of maintaining the heap is O (log n), which together is O (Nlogn). Code implementation is similar to building a heap, so don't repeat it.
3. Quick Sort
The quick platoon is also the thought of dividing, select a main element, divide the array into two parts, one part is smaller than the other part is greater than it, and then recursively two sub-arrays until the sort is complete. The selection of the quick row and the main element is related, if the selected result is exactly the order, then the time complexity will become O (N2).
The code is as follows:
1 intPartition (vector<int> &arr,intLeftintRight ) {2 inti = left-1;3 for(intj = left; J < Right; J + +){4 if(Arr[j] <=Arr[right])5Std::swap (arr[++i], arr[j]);6 }7Std::swap (Arr[i +1], arr[right]);8 returni +1;9 }Ten voidQuickSort (vector<int> &arr,intLeftintRight ) { One if(Left <Right ) { A intMID =Partition (arr, left, right); -QuickSort (arr, left, mid-1); -QuickSort (arr, mid+1, right); the } - -}
Three, linear algorithm (introduction later)
1. Base Order
2. Sort by Count
3, bucket sorting
A survey of sorting algorithms