A summary of _c language based on C + + implementation of various internal sorting algorithms

Source: Internet
Author: User
Tags benchmark sorts

Lift sorting algorithm I believe that everyone is not unfamiliar, perhaps many people have memorized them, and even can write it at any time. Yes, these are the most basic algorithms. Here is a summary of the various internal sorting algorithms, including insertion sort (direct insertion sort, binary insertion sort, hill sort), exchange sort (bubble sort, quick sort), select sort (Simple select sort, heap sort), 2-Path merge sort. (another: As for the heap sorting algorithm, a previous article has been described in detail for the algorithm implementation of heap sorting).

C + + Implementation code is as follows:

/************************************************************************* > File Name:sort.cpp > Author: Songlee ************************************************************************/#include <iostream> using

namespace Std;

typedef int ElementType; /* *<< Direct Insert Sort >> * To achieve the order of n number, insert the number of subsequent N-1 into the previously sorted subsequence, assuming that the first 1th number is a sorted sequence.
 A sequential sequence can be obtained after a N-1 trip.
 Time complexity: Best case O (n), worst case O (n^2), average situation O (n^2).
 Space complexity: O (1) * * * Stability: Stable */void Insertsort (ElementType a[], int n) {int i,j; ElementType temp; 
 Temporary variable for (i=1; i<n; ++i) {temp = A[i];
 for (j = i; j>0 && a[j-1]>temp;--j) a[j] = a[j-1];
 A[J] = temp; }/* *<< binary Insert Sort >> * Unlike direct insert ordering, the binary insertion sort is not a side-by-side move, but rather a separation of comparison and move-move operations, that is, binary find out where the element is to be inserted, and then uniformly move all elements after the bits are inserted.
 It's not hard to see that binary insertion sort only reduces the number of comparisons. Time complexity: O (n^2) * * * space complexity: O (1) * * * Stability: Stable/void Binaryinsertsort (ElementType a[), int n) {int I, j, low, High,
 Mid
 ElementType temp;
 For (I=1 i<n; ++i) {temp = A[i]; Low = 0; High = i-1;
 Sets the scope of the binary lookup while (low <= high) {mid = (Low+high)/2;//Take the intermediate point if (A[mid] > Temp) high = mid-1;
 else low = mid+1;
 For (j=i-1 j>=high+1;--j)//reunification a[j+1] = a[j]; A[HIGH+1] = temp; Insert}//* *<< Hill Sort >> * Hill sort by comparing the sequence of elements that are separated by a certain interval, i.e., a series of l[i,i+d,i+2d,... I+KD], then narrowing the spacing, and then sorting the grouped sequences. Until the last step of only comparing adjacent elements is *, the final spacing is 1.  Hill sort is sometimes called * narrowing incremental Sort * * * * time complexity: dependent on the choice of incremental sequence, but the worst case is O (n^2) * * * * * * * * * * (1) * * * Stability: unstable/void Shellsort (ElementType a[),
 
 int n) {int i, J, DK;//DK is Delta ElementType temp; For (j=i-dk j>=0 && a[j]>temp; j-=dk) A[J+DK] = A[j];
 Post-move A[J+DK] = temp; The basic idea of *<< bubble sort >> bubble sort is to compare the values of adjacent elements from the back forward (or former) 22, and then swap them until the sequence is complete. We call it a bubble.
 Each bubbling will place a * element in its final position. Time complexity: Best case O (n), worst case O (n^2), average situation O (n^2) * * * space complexity: O (1) * * Stability: Stable/void Bubblesort (ElementType a[], int n) {for
(int i=0; i<n-1; ++i) {BOOL flag = FALSE;//Indicates whether this bubble has exchanged flags for (int j=n-1; j>i;--j)//from post-forward {if (a[j-1] > A[j]) {flag = true;
 Exchange A[j-1] = A[j-1]^a[j];
 A[J] = A[j-1]^a[j];
 A[J-1] = A[j-1]^a[j];
 } if (flag = false) return; }/* *<< Quick Sort >> quick sort is an improvement to bubble sort. The basic idea is based on the divide-and-conquer method: In the Order table L[n] * to take an element pivot as a benchmark, through a sequence of sequences divided into two parts l[1...k-1] and *L[K+1...N], yes l[1...k-1] all elements are less than pivot, and l[k+ All elements * in 1...N] are greater than or equal to pivot. Then the pivot is placed on its final position L (k).
 The above process is then repeated recursively to two child * sequences until there is only one element or space in each part, i.e. all elements are placed in their final * position. Time complexity: The running time of the fast row is related to the symmetry of the partition, worst case O (n^2), best case *o (NLOGN), the average situation O (NLOGN) * * * space complexity: Due to the need for recursive work stack, the worst case is O (n), the average is O (logn) *  Stability: unstable/int Partition (ElementType a[], int low, int high) {//partition operation has many versions, here is always the first element in the current table as a pivot/benchmark ElementType Pivot
 = A[low];
 While [low < High] {while (Low 

Believe that the example code described in this article will help you to review and consolidate all sorts of sorting algorithms.

Related Article

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.