Sort.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream>using namespace std;template <typename t>void print (t* &arr, int len) {for (int i=0;i<len;i++) {cout<<* (arr+i) << ';} Cout<<endl;} Simple Select Sort/*1.i=[0,len-1), j=[i+1,len-1] array The element I value is compared to J element value, which is greater than the element value of interchange I and J 2.j+=1 repeats the first step until J = = Len3.i+=1 repeats the first step until i = = Len-1*/templ Ate <typename t>void select_sort (t* &array,int len) {if (NULL = = array) {cout<< "#ERROR" <<__file__& lt;< "line:" <<__LINE__<< "null point error" <<endl; Return } for (int i=0, i<len-1; i++) for (int j=i+1;j<len;j++) if (Array[i] > Array[j]) swap (array[i],array[j]);} Bubble sort/*1.bubble Adjacent 2 elements compare, large then swap each time to make the maximum value to the top */template <typename t>void bubble_sort (t* &array,int len) {if ( NULL = = array) {cout<< ' #ERROR ' <<__FILE__<< ' line: ' <<__LINE__<< ' null point ERROR ' < <endl; Return } bool Flag; for (int i=0, i<len-1; i++) {flag = true; for (int j=0;j<len-i-1;j++) if (Array[j] > array[j+1]) {flag = false; Swap (array[j],array[j+1]);} if (flag) break; }}//Insert Sort/*1.i belongs to [1,len] from the I position forward to judge J belongs to [0,i) then the latter element is less than the previous one is replaced, greater than or equal to exit the first loop */template <typename t>void Insert_sort ( t* &array,int len) {if (NULL = = array) {cout<< "#ERROR" <<__FILE__<< "line:" <<__line__<& lt; "Null point error" <<endl; Return } for (int i=1, i<len; i++) {for (int. j=i;j;j--) {if (Array[j] >= array[j-1]) break; swap (array[j],array[j-1]);} }}//Merge Sort/* Use the idea of dichotomy to get the data back to the left and right 2 sides of the list a,b1. Chain List A A and a chain list to get a new ordered list */template <typename t>void merge (t* &array,int Left,int Mid,int right,t* temp) {int i = left, int j = mid+1; int k = 0; while (i<=mid && j<=right) {if (AR Ray[i] > Array[j]) temp[k++] = array[j++]; else temp[k++] = array[i++]; }//Append last while (I <= mid) temp[k++] = array[i++]; while (j<=right) temp[k++] = array[j++]; Temporary variable copy to element group k = 0; while (left<=right)//did not pay attention to write K<=right debugging for half a day. Data becomes large number consider whether to assign a value out of bounds array[left++] = temp[k++]; }template <typename t>void merger_sort (t* &array,int left,int right,t* temp) {if (left < right) {int mid = ( Left + right)/2; Merger_sort<t> (array,left,mid,temp);//Sort left sub-sequence merger_sort<t> (array,mid+1,right,temp);//Sort Right sub-sequence merge <T> (ARRAY,LEFT,MID,RIGHT,TEMP)///around 2-side merge}}template <typename t>void merger_sort (t* &array,int len) { if (null = = array) {cout<< ' #ERROR ' <<__FILE__<< ' line: ' <<__LINE__<< ' null point ERROR ' <<endl; Return } t* temp = new T[len]; Merger_sort<t> (array,0,len-1,temp); if (NULL! = temp) {delete[] temp; temp = NULL;}} Quick Row//(a) dig pit pits 1. Take the first element as the baseline X initial value prefix i = left suffix j = right2. Suffix to find J element value < X;i value of J element values 3. prefix to find I element value > X;j Assignment I element value repeat 2nd, 3 steps (ii) Division 1. Left [left,i] Execution (i) 2. Right (I,right] Execution (a) recursive execution 1,2*/template <typename t>int dig_fil (t* &array,int left,int right) {T X = array[left], int i = left, int j = right, while (I<J) {while (J>i && array[j] >= X) J--; if (j>i)//Only if the suffix is greater than the prefix to assign a value otherwise the array is out of bounds {array[i] = array[j]; i++;} while (I<j && array[i] < X) i++; if (i<j) {array[j] = array[i]; j--;}} Array[i] = X; return i;} Template <typename t>void quick_sort (t* &array,int left,int right) {if (left < right) {int mid = dig_fil< T> (Array,left,right); Quick_sort (array,left,mid-1); Quick_sort (Array,mid+1,right); }}template <typename t>void quick_sort (t* &array,int len) {if (NULL = = array) {cout<< "#ERROR" <<__f ile__<< "line:" <<__LINE__<< "null point error" <<endl; Return } quick_sort (array,0,len-1);} Heap Sort/* heap ordering takes full binary tree 1. Build the largest heap from all non-leaf nodes 2. Swap heap top with the last element 3. For all [0,len-1) perform 1th, 2 steps */template <typename t>void Adjust (t* &array,int Sign,int len) {T temp = array[sign];//each cycle updates the parent node to the root of the full binary tree maximum heap for (int i = sign * 2 + 1; i < len; i = i * 2 + 1) {if (i + 1 < len && array[i + 1] > Array[i]) i++;//determine child node greater than parent node if (Array[i] > Temp) {array[sig N] = array[i]; sign = i; }} Array[sign] = temp;} Template<typename t>void heap_sort (t* &array,int len) {//1. Build the initial large top heap from all non-leaf nodes for (int i = LEN/2-1; I >= 0; i--) {Adjust (array, I, Len),}//for (int i = len-1; i; i--) {//2. Swap the maximum heap and the last element of the relative swap (array[i],array[0]);//3. Re-sizing the heap Structural adjust (array, 0, i); }}int _tmain (int argc, _tchar* argv[]) {int a[] = {1,5,4,9,0,2,3,6,8,7}; int* pa = A;//select_sort<int> (pa,10);//b Ubble_sort<int> (pa,10); Insert_sort<int> (pa,10); Merger_sort<int> (pa,10); Quick_sort<int> (pa,10); Heap_sort (pa,10); Print<int> (pa,10); GetChar (); return 0;}
C + + 6 sort algorithm source code