Selection and sorting of common sorting algorithms

Source: Internet
Author: User
Tags assert

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/86/36/wKiom1e4VhPCEcFTAAEKCEW4XWs985.png "style=" float: none; "title=" 1.png "alt=" Wkiom1e4vhpcecftaaekcew4xws985.png "/>

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M01/86/36/wKiom1e4VhPhRUAXAAENhyAGQkY878.jpg "style=" float: none; "title=" 2.jpg "alt=" Wkiom1e4vhphruaxaaenhyagqky878.jpg "/>

1. Direct selection of sorting

The direct selection sort and the direct insert sort, all divide the data into the ordered and unordered areas, the difference is that the direct insertion sort is to insert the first element of the unordered area directly into the ordered area to form a larger ordered area, and the direct selection of the order is to select a minimum element from the unordered area directly to the end of the ordered area.

Set the array to a[0...n-1].

1. Initially, the array is all unordered area is a[0..n-1]. Make I=0

2. Select a minimal element in the unordered area a[i...n-1] and swap it with a[i]. After the exchange, A[0...I] formed an orderly area.

3. i++ and repeat the second step until the i==n-1. Sort complete

This gives a direct selection sort of comparison optimization, with each iteration selecting the maximum and minimum values.

void Selectsort (int* A, size_t N)//select sort optimized Version {assert (a); for (int left = 0, right = n-1; left < right; ++left,--right) {int min = left;int max = right;for (int i = left, I <= right; ++i) {if (A[i] < a[min]) min = I;else if (A[i] > A[m AX]) max = i;} if (min! = left) {Std::swap (a[min], a[left]); if (max = = left) {max = min;}} if (max! = right) {Std::swap (A[max], a[right]);}}}

2. Heap Sequencing

void adjustdown (int* a, size_t size, size_t  root) {assert (a);int child = root * 2 + 1;while  (child <  size) {if  (child+1 < size && a[child + 1] > a[ Child]) {++child;} if  (A[child] > a[root]) {Std::swap (A[child], a[root]);root = child;child  = root * 2 + 1;} Else{break;}}} Void heapsort (int* a, size_t n) {assert (a);for  (int i =  (n-2)/2; i  >=0 ; --i) {Adjustdown (a, n, i);   //build (Large) heap}for  (int i =  n - 1; i > 0; --i)   //This write better {Std::swap (a[0], a[i]); Adjustdown (a, i - 1, 0);}} 

650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0004.gif "style=" White-space:normal "alt=" j_0004.gif "/ >650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0004.gif "style=" White-space:normal "alt=" j_0004.gif "/>650" this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0004.gif "style=" White-space:normal "alt=" j_0004. GIF "/>650" this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0004.gif "style=" white-space:normal; "alt=" j_ 0004.gif "/>650" this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0004.gif "style=" white-space:normal; "alt=" J_0004.gif "/>650" this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0004.gif "style=" White-space:normal; "alt = "J_0004.gif"/>650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0004.gif "style=" white-space:normal; " alt= "J_0004.gif"/>650) this.width=650, "src=" Http://img.baidu.com/hi/jx2/j_0004.gif "style=" White-space:normal ; "alt=" J_0004.gif "/>650) this.width=650; src=" Http://img.baidu.com/hi/jx2/j_0004.giF "style=" white-space:normal; "alt=" J_0004.gif "/>650" this.width=650; "src=" http://img.baidu.com/hi/jx2/j_0004. GIF "style=" white-space:normal; "alt=" J_0004.gif "/>650" this.width=650; "src=" http://img.baidu.com/hi/jx2/j_ 0004.gif "style=" white-space:normal; "alt=" J_0004.gif "/>

This article is from the "11408774" blog, please be sure to keep this source http://11418774.blog.51cto.com/11408774/1840682

Selection and sorting of common sorting algorithms

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.