Sort Select sort && heap Sort

Source: Internet
Author: User
Tags array length

Select Sort && Heap Sort

1. Select sort:

  Description: Select sort (Selection sort) is a simple and intuitive sorting algorithm . It works as follows. First find the smallest (Large) element in the unordered sequence, place it at the beginning of the sort sequence, and then continue looking for the smallest (large) element from the remaining unsorted elements, and then place it in the sorted The end of the sequence. And so on until all elements are sorted .

  Step: assume that the array length n is an array with n data unsorted data

1. The first traversal will exchange the smallest data in the N data with Array[0].

2. The second pass iterates through the N-1 data, swapping the smallest and array[1 in the N-1 data (the array in the second trip starts with the array[1 in the original array) and excludes the smallest data from the first trip.

...

  Program implementation:

Method 1: Select minimum data for each trip to exchange

1 voidSelectsort (int*_array, size_t _arraysize)2 {3ASSERT (_array&&_arraysize);4      for(size_t i =0; I < _arraysize;++i)5     {6         intMinindex = i;//record the first position subscript7          for(size_t j = i +1; J < _arraysize;++j)8         {9             if(_array[minindex] > _array[j])//compares each element after the first position and assigns the subscript of the smallest data to the MinindexTenMinindex =J; One         } A         //if (minindex! = i)//prevents the first element from being exchanged at minimum, reducing unnecessary overhead - swap (_array[minindex], _array[i]); -     } the     return; -}

Method 2: Similar to method one, is optimized, each trip to the smallest data head, the largest element of the tail

1 voidSelectsort_optimize (int* _array,int_arraysize)2 {3ASSERT (_array&&_arraysize);4 5     intleft =0;6     intright = _arraysize-1;7      for(; left <= Right;++left,--right)//each trip find the maximum and minimum values to reduce the number of cycles8     {9         intMinindex =Left ;Ten         intMaxindex =Right ; One          for(intindex = Left;index <= right;++index)//at this point the array is closed, and there is a difference when not optimized A         { -             if(_array[minindex] >_array[index]) -Minindex =index; the             if(_array[maxindex] <_array[index]) -Maxindex =index; -         } -         if(Left! =Minindex) +         { - swap (_array[left], _array[minindex]); +             if(left = = Maxindex)//avoid an error if the maximum value is _array[left] when the previous action is moved to _array[minindex] AMaxindex = Minindex;//Update the Maxindex at         } -         if(Right! =Maxindex) - swap (_array[right], _array[maxindex]); -     } -     return; -}

Efficiency analysis:

  Algorithmic Stability: Unstable

Complexity of Time: O (n^2)

  Space complexity: O (1)

2. Heap Sequencing
  Description: heap sequencing (heapsort) is a sort of sorting algorithm designed by using the data structure of the heap tree, which is a sort of selection. The elements of the specified index can be quickly positioned using the features of the array . Heaps are divided into large piles and small heaps, which are completely binary trees . sort Ascending when using a large heap, descending sort when using a small heap.

Step: sort the array _array in ascending order, assuming that arrays are n in length, there are n data unsorted data in the array

1: After the N data in the _array is a large heap, the maximum value is _array[0] and then the _array[0] is exchanged with _array[n-1].

2: Except _array[n-1], the rest of the data continues to establish a large heap, at this time the maximum value is still _array[0], and then the _array[0] and _array[n-2] Exchange.

...

The process of heap sequencing can be clearly understood according to the following URL

Http://www.tyut.edu.cn/kecheng1/site01/suanfayanshi/heap_sort.asp

 Program implementation:

1 voidAdjustdown (int* _arr,size_t Parindex,int_arrsize)//adjust downward from parent node2 {3size_t Childindex = Parindex *2+1;//find the parent node's left sub-tree subscript4 5      while(Childindex <_arrsize)6     {7         if((_arr[childindex] < _arr[childindex +1]) && ((Childindex +1) < _arrsize))//To find the subscript of a larger value in the left and right subtree of the parent node8childindex++;9         if(_arr[parindex] <_arr[childindex])Ten         { OneSwap (_arr[parindex], _arr[childindex]);//Exchange If the parent node data is less than the child node data AParindex = Childindex;//continue adjusting the swap subtree to ensure that any subtree in the heap is a large heap -Childindex = Parindex *2+1; -         } the         Else//if _arr[parindex] > _arr[childindex] means that it is already large and the subtree is a large heap, jumping out of the loop -              Break; -     } - } + voidHeapsort (int* _array,int_arraysize) - { +ASSERT (_array&&_arraysize >0); A     //Build a heap at      for(inti = _arraysize/2-1; I >=0;-I.)//adjust downward from the last non-leaf node until the root node has been adjusted - Adjustdown (_array, I, _arraysize); -  -      for(intj = _arraysize-1; J >0;--j) -     { -Swap (_array[0], _array[j]); inAdjustdown (_array,0, j); -     } to     return; +}

The following is the sort procedure when the array is {3,12,24,2,6}

Efficiency analysis:

  Algorithmic Stability: Unstable

Time complexity: O (n^lg N)

  Space complexity: O (1)

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.