Common sorting algorithms (comparison sort) and comparison

Source: Internet
Author: User
Tags assert

#include <iostream>using namespace std; #include <assert.h>  // Stability: Refers to two of the same number after the position of the change//bubble sort idea: The adjacent two data comparison exchange, the outer loop control number, the inner layer comparison   //void bubblesort (int *a, size_t  len)//{//assert (a);//for  (size_t i = 0; i < len - 1;  ++i)//{   //The adjacent location data, each order will be discharged with a maximum or minimum number//for  (size_t j = 0; j &NBSP;&LT;&NBSP;LEN&NBSP;-&NBSP;I&NBSP;-&NBSP;1;&NBSP;++J)//{//if  (a[j] > a[j + 1] )//{//swap (a[j], a[j + 1]);//}//}//}//}////cocktail ordering idea: That is, two-way bubble sort, a trip to the sort can find a maximum and the smallest element void  Cooktail_sort (int *arr, size_t size) {assert (arr);int tail = size - 1; int i, j ;for  (i = 0; i < tail; ++i) {for  (int j &NBSP;=&NBSP;TAIL;&NBSP;J&GT;I;&NBSP;--J) {if  (arr[j] < arr[j - 1]) {swap (arr[j], &NBSP;ARR[J&NBSP;-&NBSP;1]);}} ++i;for  (J&NBSP;=&NBSP;I;&NBSP;J&NBSP;&LT;&NBSP;TAIL;&NBSP;++J) {if  (arr[j]>arr[j + 1]) swap (Arr[j], &NBSP;ARR[J&NBSP;+&NBSP;1]);} --tail;}} Thought: Inserts the next data from the current position into an ordered block, and compares that number to the ordered data in the front. Each insertion of the previous data is ordered//void insertsort (int *a, size_t len)//Insert sort//{//assert (a);//for  ( Size_t i = 0; i < len-1; ++i)//When i=len-1, the location of TMP access is out of bounds//{//int end  = i;//int tmp = a[end + 1];//while  (end >= 0 & &AMP;&NBSP;A[END]&GT;TMP)//Last go in end=0 position than//{//a[end + 1] = a[end];//--end;//}//a[end  + 1] = tmp;//}//}//thought: Divides an array into two halves, then divides each half in half, recursively and so on, when only one data is divided, it can be considered that the group is already in order, and then merging adjacent groups, that is, the first recursive decomposition sequence, In the combined sequence Void mergesort (int *arr, int begin1, int end1, int begin2,  Int end2) {//assert (arr);//if  (begin1 >= end1 | | &NBSP;BEGIN2&NBSP;&GT;=&NBSP;END2)//return;//int one = end1 - begin1;//int two = end2 - begin2;//int *l = new int[one];//Open two arrays , one to save the first half, and one to save the latter half//int *r = new int[two];//int i = 0, j = 0 ;//for  (;  i < one; ++i)//{//l[i] = arr[begin1 + i];//}//for   (i=0; i < two; ++i)//{//r[i] = arr[begin2 + i];//}//int  index = begin1;//for  (I = 0, j = 0; index < end2 &&i<one&&j<two; ++index)//{//if  (L[i] <= r[j])//{//arr[index]  = L[i];//++i;//}//else //{//arr[index] = R[j];//++j;//}//}//if  (i <  one)//If a sub-order has been completed, the remaining remaining data is connected directly to the rear//{//for  (int k = i; k < one;  ++k)//arr[index++] = l[k];//}//else//{//for  (int k = j; k < TWO;&NBSP;++K)//arr[index++]&nbsP;= r[k];//}//delete[] l;//delete[] r;} Void _merge_sort (int *arr, int begin, int end)//{//assert (arr);//if  ( Begin + 1 < end)//{//int mid = begin +  ((End - begin  >> 1);//_merge_sort (Arr, begin, mid);//_merge_sort (arr, mid, end);// MergeSort (Arr, begin, mid, mid, end);////memcpy (src + begin, dst +  begin,  (End - begin) *sizeof (int))//}//else//return;//}//Two identical arrays, placing the source array sequentially into the target array void  MergeSort (Int *src,int *dst, int begin1,int end1,int begin2,int end2) { ASSERT (SRC&AMP;&AMP;DST); size_t index = begin1;//two arrays of the same size while  (begin1 <  End1 && begin2 < end2) {if  (src[begin1] < src[begin2]) {dst[ index++] = src[begin1++];} else{dst[index++] = src[begin2++];}} if  (BEGIN1&NBSP;&LT;&NBSP;END1) {while  (BEGIN1&NBSP;&LT;&NBSP;END1) {dst[index++] = src[begin1++];}} else{while  (Begin2 < end2) {dst[index++] = src[begin2++];}}} Void _merge_sort (int *src, int *dst, int begin, int end) {assert (src &NBSP;&AMP;&AMP;&NBSP;DST);if  (begin + 1 < end) {Int mid = begin  +  ((End - begin)  >> 1); _merge_sort (Src, dst, begin, mid); _ Merge_sort (Src, dst, mid , end); MergeSort (Src, dst, begin, mid, mid, end); memcpy (SRC&NBSP;+&NBSP;BEGIN,&NBSP;DST  + begin,  (End - begin) *sizeof (int));} Elsereturn;} Void _merge_sort (int* src, size_t size) {int* dst = new int[size];_ Merge_sort (src, dst, 0, size);d elete[] dst;} Thought: The idea of dividing and ruling, select a base, by a trip to sort the array will be sorted in two, where the cardinality before the data is smaller than it, the base of the data is larger than it, and then in the two parts of the data is fast row INt qsort (int *a, int left, int right)//Quick Sort {assert (a);if  (left >=  right) Return left;int key = a[right];int begin = left;int end  = right-1;while  (begin < end) {while  (begin < end & & a[begin] <= key) begin++;while  (begin < end && a[ End] > key) end--;if  (begin < end) swap (A[begin], a[end]);} if  (A[end] >= a[right]) swap (a[end], a[right]); return end;} Void quisort (int* a, int  left, int right)//Trenching method//{//assert (a);//if  ( Right <= left)//return;//int tmp = a[left];//int begin = left;//int  end = right;//while  (begin < end)//{//while  (begin < end &&a[end] >= tmp)//end--;//if  (BEGIN&NBSp;< end)//{//a[begin++] = a[end];//}//while  (begin < end&&a[begin ] <= tmp)//begin++;//if  (begin < end)//{//a[end--] = a[begin];//}//}/ /a[begin] = tmp;//quisort (a, left, begin - 1);//quisort (a, begin +  1, right);//}void quicksort (int *a, int left,int right) {assert (a);if  (Left < right) {Int mid = qsort (a, left, right); QuickSort (a, left, mid - 1); QuickSort (A, mid + 1, right);}} Thought: The first time to find the lowest element in the position of the subscript, exchange with the first element, then find the sub-small element subscript, and the second element to exchange, and so on//void selectsort (int* a, size_t  Len)//Select Sort//{//assert (a);//size_t min_index ;//for  (size_t i = 0; i  < len; ++i)//{//min_index = i;//for  (size_t j = i+1; j  &LT;&NBSP;LEN&NBSP;;&NBSP;++J)//{//if  (A[min_index]&NBSP;&GT;=&NBSP;A[J])//{//min_index = j;//find the lowest element in the subscript//}//}//swap (A[min_index], a[i]);// Let the smallest element in the first position//}//}//thought: The array is divided into several groups by an incremental gap, the subscript of each group is different from Gap, sorting all the elements in each group   //, and then using a smaller increment to do the above loop sorting, when the increment is reduced to 1 o'clock, The entire number to be sorted is divided into a single group, sorting complete Void shell_sort (int *a,size_t size) {assert (a); int gap = size  / 3 + 1;while  (1) {for  (int i = 0; i < size  - gap; ++i) {Int end = i;int tmp = a[end + gap];while   ((a[end] > tmp) &&end >= 0) {a[end+gap] = a[end];end -=  gap;} A[end + gap] = tmp;} if  (gap == 1) break;gap = gap / 3 + 1;//guarantee that the gap will be executed at the end of 1}}void  Testselectsort () {int a[10] = { 9, 1, 3, 4, 8, 6, 0, 2,  5, 0 };int len = sizeof (a)  / sizeof (a[0]); COUT&NBsp;<<  "Before:";for  (int i = 0; i < len; ++i) {cout  << a[i] <<  " ";} cout << endl; Shell_sort (A,len);//quicksort (a, 0, 9);//selectsort (a, 10);cout <<  "after:   ";for  (int i = 0; i < len; ++i) {Cout << a[i]  <<  " ";} Cout << endl;} Void testmergesort () {int a[10] = { 9, 1, 3, 4, 8, 6, 7,  2, 5, 0 };int len = sizeof (a)  / sizeof (a[0]);cout <<   "Before:";for  (int i = 0; i < len; ++i) {cout <<  a[i] <<  " ";} Cout << endl;//_merge_sort (A,0, len); _merge_sort (A, len);cout <<  " after:  ";for  (int i = 0; i <  len; ++i) {cout << a[i] <<  " ";} Cout << endl;} Heap sorting idea: First build a large heap or small heap, heap top element is the largest (minimum), let the heap top element and the last element Exchange, array length-1, and then adjust downward once, repeating the above loop//template<class t>//class  heap//{//public://heap (t* a, size_t size)//{//for  (size_t i = 0;  i < size; ++i)//{//_array.push_back (A[i]);//}//for  (int i =  (_ Array.size ()  - 2)  / 2; i >= 0;--i)//{//adjustdown (I,_array.size ());//}// }////void adjustdown (int root,int size)//{//size_t lchild = 2 * root  + 1;//while  (lchild < size)//{//if  (lchild + 1 <  Size)  && _array[lchild + 1] < _array[lchild])//{//lchild++;//}//if   (_array[lchild] < _array[root])//{//swap (_array[lchild], _array[root]);//root=lchild;/ /lchild = 2 *&nbsP;root + 1;//}//else//break;//}//}////void adjustup (Size_t child)//{//size_t root  =  (child - 1)  / 2;//while  (child > 0)//If root and child are size_t type, Will never be less than 0, so it cannot be used as a cyclic condition//{//if  (_array[child] < _array[root])//{//swap (_array[child], _ Array[root]);//child = root;//root =  (child - 1)  / 2;//}//else//break ;//}//}////void push_elem (const t&x)//{//_array.push_back (x);//adjustup (_array.size ()  -  1);//}////void pop_elem ()//{//swap (_array[0], _array[(_array.size ()  - 1]);//_ Array.pop_back ();//Exchange and delete the top element of the heap with the last element, then adjust the//adjustdown (0);//}////void heap_sort ()//{//int size  = _array.size ();//while  (size>0)//{//swap (_array[0], _array[size-1]);//cout < < _array[size - 1] <<  " ";////_array.pop_back ();//adjustdown (0,size-1) ;//--size;//}//}//void display ()//{//cout <<  "Heap is:";//for  (int i = 0; i < _ Array.size ();  ++i)//{//cout << _array[i] <<  " ";//}//cout < < endl;//}//protected://vector<t> _array;////};//

The application scenario and comparison of the algorithm:

Comparison sort: (1) Insert (direct insert, Hill Sort), (2) Select (select Sort, heap Sort), (3) Exchange (bubble sort, fast row) (4) Out sort (merge)

1) Complexity of Time:

Average performance is O (n^2): Insert, select, Bubble

Data size hour: Direct Insert sort better

When data size is large: bubble sort time is the most expensive

Average performance is O (NLGN): heap, fast, merge

When data size is large: applicable heap sequencing (example: Find the smallest first 100 numbers in 10 million numbers)

Data scale hour: Fast sorting is better when small to a certain interval using insert sort

Hill sort average time complexity of O (n^1.3)

Stability refers to whether the position changes after the two identical numbers are sorted and is stable if no changes are in place

2). Stability Analysis:

Stability: bubbling, inserting, merging

Instability: Selection, hill, heap, Quick row




Common sorting algorithms (comparison sort) and comparison

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.