Select Sorting Algorithm --- directly select sorting and heap sorting

Source: Internet
Author: User

This article mainly focuses on the analytical selection of sorting algorithms: Direct selection of sorting and heap sorting.

 

1. Directly select sorting

 
 

Basic Idea:

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, store it to the starting position of the sorting sequence, and then continue searching for the smallest (large) element from the remaining unordered elements, put it at the end of the sorted sequence. And so on until all elements are sorted.

The main advantages of sorting are related to data movement. If an element is in the correct final position, it will not be moved. When sorting is selected, an element is exchanged each time. At least one of them will be moved to its final position. Therefore, a maximum of N-1 exchanges can be performed on tables with n elements. In all sorting methods that rely entirely on swapping to move elements, selecting sorting is a good one.

  Algorithm Implementation: 
Public static void selectsort (INT [] data) {system. out. println ("start sorting"); int arraylength = data. length; // perform n-1 Comparison in sequence. the I-th comparison selects the I-th value and places it on the I-th position. For (INT I = 0; I <arraylength-1; I ++) {// The I data only needs to be compared with the data after it for (Int J = I + 1; j <arraylength; j ++) {// if the data at location I> data at location J, exchange them if (data [I]> data [J]) {int TMP = data [I]; data [I] = data [J]; data [J] = TMP ;}} system. out. println (Java. util. arrays. tostring (data) ;}} public static void main (string [] ARGs) {int [] array = new int [10]; for (int K = 0; k <array. length; k ++) {array [k] = (INT) (math. random () * 100);} system. out. print ("the result before sorting is:"); system. out. println (Java. util. arrays. tostring (array); system. out. println (""); selectsort (array );}

 

Sorting result:

The sorting result is as follows: [37, 93, 97, 9, 66, 68, 19, 5, 67, 45, 68, 19, 9, 67, 45] [5, 9, 97, 93, 66, 68, 37, 19, 67, 45] [5, 9, 19, 97, 93, 68, 66, 37, 67, 45] [5, 9, 19, 37, 97, 93, 68, 66, 67, 45] [5, 9, 19, 37, 45, 97, 93, 68, 67, 66] [5, 9, 19, 37, 45, 66, 97, 93, 68, 67] [5, 9, 19, 37, 45, 66, 67, 97, 93, 68] [5, 9, 19, 37, 45, 66, 67, 68, 97, 93] [5, 9, 19, 37, 45, 66, 67, 68, 93, 97]

 

Algorithm Analysis:

(1) Number of keyword comparisons
Regardless of the initial state of the file, the number of comparisons has nothing to do with the initial state of the keyword. To select the record with the minimum keyword in the I-th sorting, the n-I comparison is required. Therefore, the total number of comparisons is:
= 0 (N2)

(2) number of records moved
When the initial file is in positive order, the number of moves is 0.
When the initial state of the file is reverse, the switch operation is performed for each sort. the maximum number of moves is 3 (n-1 ).

The number of exchanges. The best case is that it has been ordered and 0 is exchanged. The worst case is reverse order and exchange. The number of exchanges is less than that of Bubble sorting. Because the CPU time required for switching is more than the CPU time required for comparison, and the value is smaller, the sorting is faster than the Bubble sorting.
The average time complexity of sorting is O (n2 ).

(3) Direct sorting is a local sorting.

(4) Stability Analysis
Direct selection of sorting is unstable, and in-situ operations are almost the only advantage of sorting

 

 

Ii. Heap sorting

 
Algorithm idea:

 

 

Select Sorting Algorithm --- directly select sorting and heap sorting

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.