Sort (Insert sort, hill sort, select Sort, heap sort)

Source: Internet
Author: User
Tags assert

Select sort

How it works: each time the largest or smallest element is selected from the data element to be sorted, it is stored at the beginning of the sequence until all the data elements to be sorted are exhausted.


Void selectsort (int* a, size_t size) {    assert (a);     for  (int i = 0; i < size; i++)     {         int min = i;         for  (int j = i + 1; j < size; j++)          {        //Select the smallest element              if  (A[j] < a[i])              {                 min = j;             }        }         //in the first place        if  (A[i] != a[min])          {            int tmp  = a[i];            a[i] = a[ min];            a[min] = tmp;         }    }}

Heap Sort

only need


Ascending Void adjustdown (int* a, size_t size, int 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])          {            swap (A[child],  a[root]);            root = child;             child = root * 2 + 1;         }        else         {            break;         }    }}void adjustup (int* a, size_t  size, int 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])          {             swap (A[child], a[root]);             root = child;            child =  root * 2 + 1;        }         else        {             break;        }     }}void heap_sort (int* a, size_t size) {    assert (a);         //Building     for  (int i =  ( SIZE&NBSP;-&NBSP;2) &NBSP;/&Nbsp;2; i >= 0; i--)     {         adjustdown (a, size, i);    }    for  (int j  = size - 1; j > 0; j--)     {         swap (A[0], a[j]);         adjustdown (a, j, 0);     }}void _heap_sort (int* a, size_t size) {     assert (a);    for  (int i =  (SIZE&NBSP;-&NBSP;2)  / 2; i >= 0; i--)     {         adjustup (a, size, i);    }    for  ( int j = size - 1; j > 0; j--)     {      &nbSp;  swap (A[0], a[j]);         adjustup (a, j,  0);     }}


This article from "Write Quality or Low" blog, declined reprint!

Sort (Insert sort, hill sort, 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.