Select sorting (Direct selection and heap sorting)

Source: Internet
Author: User

Directly select the sorting (unstable) sorting process: 1. First, select the smallest number among all data after N-1 comparison, and exchange it with 1st data, 2. Then, the minimum number of sorting codes is selected from the remaining data, and the minimum number of sorting codes is exchanged with 2nd data ...... and so on until all data is sorted out. Select the data with the smallest keyword in the I-th sorting, and perform n-I comparison. Complexity: the total number of comparisons is n (n-1)/2 = O (n2 ). When the initial file is in positive order, the number of moves is 0. When the initial file state is in reverse order, the switch operation is executed for each sort. the maximum number of moves is 3 (n-1 ). The average time complexity of direct sorting is O (n2), and direct selection of sorting is unstable. Differences from Bubble Sorting: 1. Select sorting: a [0] is compared with a [1]. If a [0] is greater than a [1], the position 1 of the minimum number of records a [1], and then j ++ becomes a [2] compared with the current minimum number a [1]. Then, the record returns slowly to the next loop, with the minimum number of records, it is best to swap the smallest number with the first number a [0]. 2. Bubble Sorting: first, compare the first number a [0] with the second number a [1] (from small to large ), then compare the second number a [1] with the third number a [2]; the third number a [2] with the fourth number a [3] ...... the first inner loop ends. The program is as follows:

Void SelectSort (int * arr, int num) {if (NULL = arr) return; for (int I = 0; I <n-1) {int k = I; for (int j = I + 1; j <n; ++ j) {if (arr [j] <arr [k]) k = j; // k write down the location of the minimum number found currently} if (k! = I) {int temp = arr [I]; arr [I] = arr [k]; arr [k] = temp ;}}}

 

Heap sorting (unstable) Heap Introduction: heap is actually a full Binary Tree, and any non-leaf node of heap meets the following requirements: key [I] <= key [2i + 1] & Key [I] <= key [2i + 2] or Key [I]> = Key [2i + 1] & & key> = key [2i + 2], that is, the keywords of any non-leaf node are not greater than or less than those of the left and right child nodes. A heap is divided into a large heap and a small heap. The heap meets the requirements of Key [I]> = Key [2i + 1] & key> = key [2i + 2, meeting Key [I] <= key [2i + 1] & Key [I] <= key [2i + 2] is called a small top heap. From the above properties, we can see that the keywords of the heap top of the big top stack must be the largest among all keywords. the keywords of the heap top of the small top stack are the smallest of all keywords. Heap sorting: the largest keyword (minimum keyword) is recorded on the top of a large heap (small top heap), so that the maximum record (minimum record) is selected from unordered each time) easy to use. The basic idea is (Big Top heap): 1) sequence of the initial keywords to be sorted (R1, R2 .... rn) build a large top heap, which is the initial unordered zone; 2) exchange the top element R [1] with the last element R [n, in this case, a new unordered partition (R1, R2 ,...... rn-1) and the new ordered zone (Rn), and meet R [1, 2... n-1] <= R [n]; 3) because the new stack top R [1] After switching may violate the stack nature, the unordered zone (R1, r2 ,...... rn-1) adjusted to the new heap, and then re-exchange the R [1] with the last element of the unordered area, get the new unordered area (R1, R2 .... rn-2) and the new ordered zone (Rn-1, Rn ). Repeat this process until the number of elements in the ordered area is n-1, the entire sorting process is completed. The procedure is as follows: 1) initialize the heap: Set R [1 .. n] is constructed as a heap. 2) Adjust the heap to make it a large top heap. 3) start sorting, swap the heap top element R [1] In the current unordered zone with the last record in the interval, and then adjust the new unordered zone to the new heap. Therefore, the two most important operations for heap sorting are the construction of the initial heap and the adjustment of the heap. In fact, the construction of the initial heap is also the process of adjusting the heap, however, when the initial heap is constructed, all non-leaf nodes are adjusted. Complexity: The worst time complexity of heap sorting is O (nlgn ). The average performance of heap sorting is closer to the worst performance. Because the initial heap requires a large number of comparisons, the heap sorting is not suitable for files with a small number of records. Heap sorting is a local sorting method with the auxiliary space of O (1). It is an unstable sorting method. Code:/* heap sorting (Big Top heap) 2011.9.14 */# include <iostream> # include <algorithm> using namespace std; void HeapAdjust (int * a, int I, int size) // adjust heap {int lchild = 2 * I; // The left child node number of I int rchild = 2 * I + 1; // The right child node number of I int max = I; // Temporary Variable if (I <= size/2) // if I is not a leaf node, no adjustment is required. {if (lchild <= size & a [lchild]> a [max]) {max = lchild ;} if (rchild <= size & a [rchild]> a [max]) {max = rchild;} if (max! = I) {swap (a [I], a [max]); HeapAdjust (a, max, size ); // avoid having to set up a heap {int I; for (I = size/2; I> = 1; I --) // The maximum number of non-leaf nodes is size/2 {HeapAdjust (a, I, size );}} void HeapSort (int * a, int size) // heap sorting {int I; BuildHeap (a, size); for (I = size; I> = 1; I --) {// cout <a [1] <""; swap (a [1], a [I]); // swap heap top and last element, that is, each time the creator of the remaining element is put to the end //buildheap (a, I-1); // re-establish the remaining element as the big top HeapAdjust (a, 1, I-1 ); // reset the heap top node to a large top heap} int main (int argc, char * argv []) {// int a [] =, 8}; int a [100]; int size; while (scanf ("% d", & size) = 1 & size> 0) {int I; for (I = 1; I <= size; I ++) cin> a [I]; HeapSort (a, size); for (I = 1; I <= size; I ++) cout <a [I] <""; cout <endl;} return 0 ;}

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.