C/c ++ common algorithms (10) -- Basic sorting algorithms (select sorting)

Source: Internet
Author: User

Select sort


The basic idea of SelectionSort is to select the record table with the smallest keyword from the record to be sorted each time, and then exchange with the first record in the record sequence to be sorted, until the entire record sequence is ordered.


1. Simple selection and sorting


The basic operation of Simple Selection Sort is to compare the n-I keywords, select the least keyword record from n-I + 1 record, and then exchange with the I record, I = ,... N-1.


1. 1 sorting example


For example, if a keyword sequence is set to 7, 4,-2, 19, 13, and 6, the sorting process is directly selected, as shown in Figure 10-8.




1.2 algorithm example


// Select sorting # include
 
  
# Include
  
   
# Define SIZE 10 void SelectionSort (int * a, int len) {int I, j, k, h; int temp; for (I = 0; I
   
    
Running result
    



2. Heap sorting


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8aDI + Mi4xICC20bXEtqjS5TwvaDI + cjxwp133 vcD4KPHA + PGJyPgo8L3A + signature + tfjo7o8L3A + Signature = "http://www.bkjia.com/uploadfile/Collfiles/20131224/20131224104656238.jpg" alt = "\">


According to the definition of heap, heap is a Complete Binary Tree With k1 as the root. If the nodes of the binary tree are numbered (from top to bottom, from left to right), the resulting sequence is to store the nodes of the Binary Tree in an ordered structure, the Heap Structure is exactly the same as the sequence structure.


2.2 heap nature


① The heap is a Complete Binary Tree with sequential storage structure, and k1 is the root node;

② The heap root node is the minimum (or maximum) value in the keyword sequence, which is called a small (or large) Root heap respectively;

③ The sequence composed of elements from the root node to each leaf node path is non-decreasing (or non-incrementing) by the element value (or keyword value;

④ Any subtree In the heap is also a heap.

The minimum (or maximum) keyword value of the heap top record is used to select the record with the minimum (or maximum) keyword from the record to be sorted in sequence, you can sort data records. This sorting method is called heap sorting.


2.3 heap sorting ideas


① Create a heap for a set of records to be sorted according to the definition of the heap;

② If the heap top record is switched to the last record, n-1-1 record is unordered, And the last record is ordered;

③ After the top record is exchanged, the first n-1 record is no longer a heap. Instead, the first n-1 record to be sorted must be reorganized into a heap, then, the heap top record and the last and last record are exchanged to adjust (exclude) The record of the Second Small keyword value in the whole sequence to the unordered zone;

④ Repeat the preceding steps until all records are sorted.


Conclusion: if a small root heap is used in the sorting process, the non-decreasing sequence is obtained after sorting. If a large root heap is used, the non-incrementing sequence is obtained after sorting.

4.4 heap adjustment-Filtering


(1) Heap adjustment ideas

After the top element of the heap is output, replace it with the last element in the heap. Then, compare the root node value with the root node value of the left and right subtree and exchange it with the small one; repeat the preceding operation until the leaf node or its keyword value is less than or equal to the value of the Left or right subtree keyword, a new heap is obtained. The process from the top of the heap to the leaf is called "screening", as shown in 10-10.

Note: during the screening process, the left and right subtree of the root node are heap. Therefore, filtering is an adjustment process from the root node to a leaf node.




2.5 code implementation:

// Heap sorting # include
     
      
# Include
      
       
# Define SIZE 10 void HeapSort (int a [], int n) {int I, j, h, k; int t; for (I = n/2-1; i> = 0; I --) {while (2 * I + 1 <n) {j = 2 * I + 1; if (j + 1) <n) {if (a [j] <a [j + 1]) {j ++ ;}} if (a [I] 0; I --) {t = a [0]; a [0] = a [I]; a [I] = t; k = 0; while (2 * k + 1
       
        




Reference books: C/C ++ common algorithm manuals, data structures-yan Weimin, Tsinghua University

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.