Select Sort---Simple select sort heap sort

Source: Internet
Author: User

A simple choice of sorting

For the n number to be ordered n times, the first time, the smallest number is placed first. The second time, the second small tree, put in the second one ....

Each time compared to the following number, if the order is small to large, when the current number than the back of the large, to be exchanged.


#include <stdio.h>void chosesort (int a[],int length) {int i,j,temp;    for (i=0;i<length;i++) for (j=i+1;j<length;j++) {if (A[i]>a[j]) {   temp = a[i];   A[i] = a[j];   A[J] = temp;}}   } void  Main () {int I;int a[] = {4,2,6,7,44,32,35,23};int n = 8;chosesort (a,n); for (i =0;i<n;i++) {  printf ("%d", a [i]);}  }


Second, heap sequencing

The heap is a completely binary tree with each node having a value greater than or equal to (less than or equal to) its own left and right children.

An array of n is constructed, the root node is the maximum (minimum), and then the remaining number of n-1 is made into a heap, and the root node continues to be fetched.

To complete the above procedure, you need to complete two tasks:

1. Make a heap of some numbers

Building a Heap method: the process of building a heap on the initial sequence is a process of filtering over and over again.

1) A complete binary tree of n nodes, then the last node is the sub-tree of the first N/2 node.

2) filter starts with a subtree that is the root of the N/2 node, and the subtree becomes a heap.

3) After that, the subtree of each node is then filtered forward, making it a heap until the root node.


2. After removing the root node, rebuild the heap

Swap the last one with the root node. In the way of building the heap (only the first number is to be built to sort the heap), the number of the first n-1 is built.



#include <stdio.h>
/*
1, S for the number of S to be adjusted, n is the length of the entire array
   To compare each node to the child's node.
*/void headadjust (int a[],int s,int n) {int child,temp;    temp = A[s];        Child = 2*s+1;        while (Children < n) {     //First find the child, the larger if ((child+1) <n && A[child+1]>a[child]) {    child++;} if (A[s]<a[child]) {    A[s] = A[child];                s = Child;child = 2*s+1;} else{break   ;} A[s] = temp;}} Each time the heap is established, the root node of the heap is exchanged to the last, and then the first element is heap sorted with the Headadjust function void heap (int a[],int length) {   int i,j,temp;    For (i= (length-1)/2;i>=0;i--) {  headadjust (a,i,length);} for (j=length-1;j>=0;j--) {   temp = a[0];   A[0] = a[j];   A[J] = temp;   Headadjust (A,0,J);}}   void Main ()   {   int i;   int a[] = {3,1,5,7,2,4,9,6,10,8};     int n=10;       Heap (a,n);   for (i = 0;i<n;i++)   {     printf ("%d", A[i]);   }      }



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Select Sort---Simple select sort heap sort

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.