Simple selection sorting and heap sorting

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " The basic operation of the > select Sort is to select the maximum or minimum element output of a keyword in a sequence of n elements, and then select the maximum or minimum element output from the remaining n-1 elements, and so on, until the sort is finished. </span>
<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > in ascending order for example, <span style= "color: #ff0000;" > Simple Select the sort </span> process as follows: 1 first find the minimum value in the array a[i], and then swap the position with A[i] and a[0]. </span><span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " ></span><span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " >2 start from a[1], and then find the minimum a[j] from a[1], and then swap the location with a[1], and so on. </span><span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " > nonsense not much to say, direct sticker code </span>
<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " ></span>

5 simple Select sort ************************//less than bubbling method, more, swap less void selectsort (int a[],int len) {int i,j,min, Temp;for (i=0;i<len-1;i++) {min=i;for (j=i+1;j<len;j++) if (A[j]<a[min]) min=j;temp=a[i];a[i]=a[min];a[min] =temp;}}

Simple selection sortingComplexity of TimeO (n^2)

Stability Analysis : Select the order is to each location to select the current element minimum (or maximum), such as to the first position to select the smallest, in the remaining elements to the second element to select the second small, and so on, until the first n-1 elements, the nth element is not selected, Because it's only one of the biggest elements left. Then, in a trip, if the current element is smaller than an element, and the small element appears behind an element that is equal to the current element, then the post-swap stability is destroyed. Rather awkward, for example, sequence 5 8 5 2 9, we know that the first time to select the 1th element 5 and 2 Exchange, then the original sequence of 2 5 of the relative sequence is destroyed, so the choice of sorting is an unstable sorting algorithm.

Simple selection of sorting time is mainly wasted on how to find the maximum or minimum value for n elements (unordered order)----in less than O (n) time

Improved: Heap Sorting

A heap is a completely binary tree, where any non-leaf node keyword is greater than (or less than) equal to the keyword of its child node. Divided into maximum heap and minimum heap

sort Ascending with the largest heap : The initial maximum heap is constructed first, because the first keyword of the largest heap is the maximum value, and the first keyword and the last keyword are exchanged directly (not similar to the simple selection sort), and then the rest of the keywords are adjusted to make the largest heap again. The first keyword is then exchanged with the last keyword of the current largest heap, and so on until the sort is complete.

In short: Construct the initial heap----> fetch the heap top------> Adjust the remaining heap------> take the remaining heap top-------;--------> Empty heap End

The process of constructing the initial heap is described as follows:


Nonsense not much to say, directly on the code:

6 Heap Sort *******************************//Ascending sort constructs a large top heap, placing the largest element (root) in the last position of the array each time// Reconstruct the two-fork heap for the remaining elements-----to store the heap as an array (full binary tree) void heapadjust (int a[], int i, int size) {//Resize heap (size size--a[0~size]) The I node makes it a large top heap  size=len-1 node number 0,1,...size.int lchild = 2*i+1;//i left child node ordinal int rchild = 2*i+2;//i Right child node ordinal int max=i;int temp;if (i <=SIZE/2)//If I is a leaf node there is no need to adjust {if (lchild<=size && A[lchild]>a[max]) max=lchild;if (rchild<=size && A[rchild]>a[max]) max=rchild;if (max!=i) {temp=a[i];a[i]=a[max];a[max]=temp;// Avoid resizing the subtree with man as the parent node is not a large top heap heapadjust (a,max,size);}}  void heapsort (int a[],int size) {//heapsort (a,9); size=n-1; number of elements int i,temp;for (i=size/2;i>=0;i--)//Build Initial heap The maximum ordinal value of a non-leaf node is SIZE/2   heapadjust (a,i,size); for (i=size;i>=0;i--) {temp=a[0];   Swap heap top and last element A[0]=a[i];a[i]=temp;//buildheap (a[],i-1),//re-establish the remaining elements as a large top heap heapadjust (a,0,i-1);//Resize the top node of the heap to become a large heap}}

Since the heap is a fully binary tree, it is actually used to describe the heap with an array ....

The code is explained below:

Heapsort the first for loop is the process of building a heap, starting from the first non-leaf node to adjust the heap (heapadjust) The last non-leaf node may be size/2, which may be size/2-1, as to why the number is size/ 2 start, is to ensure maximum possibility, further determine whether the leaf node can be in the heapsort in the IF (lchild<=size) and so on to judge.

Heapsort the second for loop is the process of taking a heap top and adjusting the heap.


The following analysis of time complexity (introduction to the reference algorithm)


Analyze the time complexity of the tuning heap

Reset heap, Time complexity T (LGN)

Heap sequencing time complexity T (NLGN) No matter the best, the worst is the same.




Simple selection sorting and heap sorting

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.