Seven common sorting algorithms and seven common sorting algorithms

Source: Internet
Author: User

Seven common sorting algorithms and seven common sorting algorithms

1: Bubble Sorting:

// BubbleSort. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <iostream> using namespace std;/* Bubble Sorting is a stable sorting time. The complexity is O (n ^ 2) */void Swap (int & a, int & B) {int temp = a; a = B; B = temp;} void BubbleSort (int a [], int n) {int I, j; for (I = 0; I <n; I ++) {for (j = 1; j <n-I; j ++) {if (a [J-1]> a [j]) {Swap (a [J-1], a [j]) ;}}} void PrintNum (int a [], int n) {for (int I = 0; I <n; I ++) {cout <a [I] <"" ;}} int _ tmain (int argc, _ TCHAR * argv []) {int a [] = {2, 4, 3, 6, 5, 1, 7, 8, 9, 33, 2, 4, 55}; int Num = sizeof (a)/sizeof (a [0]); cout <"Before sorting:" <endl; PrintNum (a, Num); BubbleSort (a, Num); cout <endl <"after sorting: "<endl; PrintNum (a, Num); getchar (); return 0 ;}


2: insert directly to sort:

// InsertSort. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <iostream> using namespace std;/* The insertion sorting is stable. The insertion sorting is O (n ^ 2); */void PrintNum (int a [], int n) {for (int I = 0; I <n; I ++) {cout <a [I] <"";}} void InsertSort (int a [], int n) {int I, j; for (I = 1; I <n; I ++) // from 1 a [0] automatic default is ordered {if (a [I] <a [I-1]) {int temp = a [I]; for (j = I-1; j> = 0 & a [j]> temp; j --) {a [j + 1] = a [j];} a [j + 1] = temp; // when a [j] <temp is encountered, a [j + 1] value: temp }}void InsertSort2 (int a [], int n) {int I, j; for (I = 1; I <n; I ++) {if (a [I] <a [I-1]) {int temp = a [I]; for (j = I-1; j> = 0 & a [j]> temp; j --) {a [j + 1] = a [j];} a [j + 1] = temp ;}} int _ tmain (int argc, _ TCHAR * argv []) {int a [] =, 7, 8, 9, 33, 2, 4, 55}; int Num = sizeof (a)/sizeof (a [0]); cout <"Before sorting:" <endl; printNum (a, Num); InsertSort2 (a, Num); cout <endl <"after sorting:" <endl; PrintNum (a, Num); getchar (); return 0 ;}


3: Hill sorting:

// ShellSort. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <iostream> using namespace std;/* Hill sorting: 1: the essence of hill sorting is that grouping is directly inserted to sort; 2: record is pressed to a certain increment group, sort each group using a direct insertion sorting algorithm. As the increment decreases gradually, each group contains more and more keywords. When the increment is reduced to 1, the entire file is grouped into a group, and the algorithm ends. 3: Hill sorting is not a stable sorting 4: the time complexity of hill sorting: slightly better than O (n ^ 2) 5: Hill sorting inserts and sorts elements according to different steps, when the elements at the beginning are unordered, The step size is the largest, so the number of elements inserted for sorting is very small and the speed is very fast. When the elements are basically ordered, the step size is very small, insert sorting is highly efficient for ordered sequences. Therefore, the time complexity of hill sorting is better than that of o (n ^ 2. */Void PrintNum (int a [], int n) {for (int I = 0; I <n; I ++) {cout <a [I] <";}} void ShellSort (int a [], int n) {int I; int gap; for (gap = n/2; gap> 0; gap/= 2) {for (I = gap; I <n; I ++) {if (a [I] <a [I-gap]) {int temp = a [I]; int j; for (j = I-gap; j> = 0 & a [j]> temp; j-= gap) {a [j + gap] = a [j];} a [j + gap] = temp ;}}} int _ tmain (int argc, _ TCHAR * argv []) {int a [] = {2, 4, 3, 6, 5, 1, 7, 8, 9, 33, 2, 4, 55}; int Num = sizeof (a)/sizeof (a [0]); cout <"Before sorting:" <endl; printNum (a, Num); ShellSort (a, Num); cout <endl <"after sorting:" <endl; PrintNum (a, Num); getchar (); return 0 ;}

4: Select sorting:

// SelectSort. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <iostream> using namespace std;/* directly select sorting and directly insert sorting. Data is divided into ordered and unordered areas, the difference is that direct insertion of sorting inserts the first element of the unordered area directly into the ordered area to form a larger ordered area, the Direct selection of sorting is to select the smallest element from the unordered area and directly place it to the final array a [0... n-1] 1: At the beginning, all arrays are unordered and a [0... n-1]. make I = 0; 2: In the unordered Zone a [I... in n-1], select the smallest element and exchange it with a [I. After the switch, a [0... i] It forms an ordered area 3: I ++ and repeats Step 2 to know That I = n-1. after sorting is completed, the selected sorting is not stable. For example, if 5 8 5 2 9 is exchanged between the first 5 and 2 after the first sorting, the relative position of the two 5 is changed. Time complexity is also O (n ^ 2), but it is better than Bubble Sorting in general */void PrintNum (int a [], int n) {for (int I = 0; I <n; I ++) {cout <a [I] <"" ;}} void Swap (int & a, int & B) {int temp =; a = B; B = temp;} void SelectSort (int a [], int n) {int I, j, nMin; for (int I = 0; I <n; I ++) {nMin = I; for (int j = I + 1; j <n; j ++) {if (a [j] <a [nMin]) {nMin = j ;}} Swap (a [nMin], a [I]) ;}} int _ tmain (int argc, _ TCHAR * argv []) {int a [] = {2, 4, 3, 6, 5, 1, 7, 8, 9, 33, 2, 4, 55}; int Num = sizeof (a)/sizeof (a [0]); cout <"Before sorting:" <endl; PrintNum (a, Num); SelectSort (a, Num); cout <endl <"after sorting: "<endl; PrintNum (a, Num); getchar (); return 0 ;}


5: Merge Sorting:

// MergeSort. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <iostream> using namespace std;/* merge and sort: time complexity: O (NlogN) it is a stable sorting and merging algorithm established on the merge operation. This algorithm is a very typical application of Divide and Conquer. Merges ordered subsequences to obtain a fully ordered sequence. That is, first orders each subsequence, and then orders the subsequence segments. If two ordered tables are merged into an ordered table, it is called a two-way merge. The merging process is: Compare the sizes of a [I] And a [j]. If a [I] is less than or equal to a [j], copy the element a [I] In the first ordered table to r [k] and Add 1 to I and k respectively; otherwise, copy the element a [j] In the second ordered table to r [k], and Add 1 to j and k respectively. In this way, the loop continues until one of the ordered tables is completed, then copy the remaining elements in another ordered table to the Unit from subscript k to subscript t in r. The Merge Sorting Algorithm is implemented recursively. We first sort the subintervals [s, t] to the midpoint, then sort the left subintervals, and then sort the right subintervals, finally, merge the left and right intervals into an ordered interval [s, t] with one merge operation. */Void PrintNum (int a [], int n) {for (int I = 0; I <n; I ++) {cout <a [I] <";}} void mergeArray (int a [], int first, int mid, int last, int temp []) {int I = first; int j = mid + 1; int m = mid; int n = last; int k = 0; while (I <= m & j <= n) {if (a [I] <= a [j]) {temp [k ++] = a [I ++];} else {temp [k ++] = a [j ++];} while (I <= m) {temp [k ++] = a [I ++];} while (j <= n) {temp [k ++] = a [j ++] ;}for (I = 0; I <k; I ++) {a [first + I] = temp [I] ;}} void mergeSort (int a [], int first, int last, int temp []) {if (first <last) {int mid = (first + last)/2; mergeSort (a, first, mid, temp); // sort mergeSort (, mid + 1, last, temp); // sort mergeArray (a, first, mid, last, temp) On the right ); // merge two ordered columns} bool MergeSort (int a [], int n) {int * p = new int [n]; mergeSort (a, 0, n-1, p); delete [] p; return true;} int _ tmain (int argc, _ TCHAR * argv []) {int a [] = {2, 4, 3, 6, 5, 1, 7, 8, 9, 33, 2, 4, 55}; int Num = sizeof (a)/sizeof (a [0]); cout <"Before sorting:" <endl; printNum (a, Num); MergeSort (a, Num); cout <endl <"after sorting:" <endl; PrintNum (a, Num); getchar (); return 0 ;}


6. Quick sorting:

// QuickSort. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <iostream> using namespace std;/* idea of fast sorting which is more efficient and fast in several sorting methods that are the same as O (NLogN: number of traps + division method 1: first take a number from the array as the reference number, for example, a [0] 2: partition process, place all the numbers greater than this number to the right of it, and all the numbers smaller than or equal to it to the left of it. 3: Repeat the second step on the left and right intervals, until the fast sorting of a number in each interval is not a stable sorting, which is also the biggest disadvantage compared with the Merge Sorting. Example: 3 2 4 5 6 2 7 The first step is to find the position of 2, 2, and 3 smaller than a [0] from the right to the right. Then the relative positions of the two are changed, so it is not a stable sorting */void PrintNum (int a [], int n) {for (int I = 0; I <n; I ++) {cout <a [I] <";}} void QuickSort (int a [], int start, int end) {if (start> end) {return ;} int I = start; int j = end; int k = a [I]; // a [I] is the first pitfall while (I <j) {// enter a [I] while (I <j & a [j]> = k) {j --;} from the right to the left --;} if (I <j) {a [I] = a [j]; // enter a [j] in a [I, a [j] is a new pitfall} // from left to right find a number greater than k to fill in a [j] while (I <j & a [I] <k) {I ++;} if (I <j) {a [j] = a [I]; // enter a [I] in a [j, a [I] is a new pitfall} // when exiting, I = j, fill k in this pitfall a [I] = k; QuickSort (, I + 1, end); QuickSort (a, start, I-1);} int _ tmain (int argc, _ TCHAR * argv []) {int a [] = {2, 4, 3, 6, 5, 1, 7, 8, 9, 33, 2, 4, 55}; int Num = sizeof (a)/sizeof (a [0]); cout <"Before sorting: "<endl; PrintNum (a, Num); QuickSort (a, 0, Num-1); cout <endl <" sorted: "<endl; PrintNum (, num); getchar (); return 0 ;}


7: heap sorting:

// HeapSort. cpp: defines the entry point of the console application. // # Include "stdafx. h "# include <iostream> using namespace std;/* unstable: two numbers with the same size. After sorting, the final position is switched with the initial position. Fast sorting: 27 23 27 3 with the first 27 as the center of the sequence, then 27 is exchanged with the next 3 to Form 3 23 27. After sorting, but the last 27 was prior to the initial position 3 and 27 at the beginning of the sorting, so it was unstable. Heap sorting: for example, 3 27 36 27. If the heap top 3 is output first, 27 of the layer 3 (the last 27) runs to the heap top, and the heap is stable, continue to output the heap top, which is the 27 just now. This shows that the 27 following is prior to the 27 output at the second position, which is unstable. */Void PrintNum (int a [], int n) {for (int I = 0; I <n; I ++) {cout <a [I] <";}} void HeapAdjust (int a [], int start, int end) {int temp = a [start]; for (int I = 2 * start + 1; I <= end; I * = 2) {if (I <end & a [I] <a [I + 1]) // compare the left and right children with {++ I; // If the left child value is smaller than the right child value, ++ I, I is the subscript of a large record} else {// I is not processed} if (temp> a [I]) // comparison between the winner and the father in the left and right children {break; // if the maximum number of left and right children is smaller than that of the parent node, you do not need to handle it.} else {// upper the child node, then, the next round of filtering at the child node a [start] = a [I]; start = I ;}} a [start] = temp ;} void HeapSort (int a [], int n) {// first create a large top heap for (int I = n/2; I> = 0; -- I) {HeapAdjust (, i, n) ;}// sort for (int I = n-1; I> 0; -- I) {// exchange the last element with the first element int temp = a [I]; a [I] = a [0]; a [0] = temp; // then adjust the remaining unordered elements to HeapAdjust (a, 0, I-1);} int _ tmain (int argc, _ TCHAR * argv []) {int a [] = {2, 4, 3, 6, 5, 1, 7, 8, 9, 33, 2, 4, 55}; int Num = sizeof (a)/sizeof (a [0]); cout <"Before sorting:" <endl; PrintNum (a, Num); HeapSort (a, Num); cout <endl <"after sorting: "<endl; PrintNum (a, Num); getchar (); return 0 ;}



 

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.