[Algorithm retrieval (java description)] --- Select sorting (directly select sorting and heap sorting)

Source: Internet
Author: User

[Algorithm retrieval (java description)] --- Select sorting (directly select sorting and heap sorting)


Select the record with the smallest keyword from the record to be sorted, and put it at the end of the sorted subfile. All records are sorted. There are two main sorting methods: Direct sorting (or simple sorting) and heap sorting.

Directly select the basic idea of sorting

At the beginning of the I-th sorting, the current ordered and unordered areas are respectively R [1 ...... I-1] and R [I ...... N] (1 <= I <= N-1), this sort is to select the record with the smallest keyword from the current unordered area R [k], exchange it with the first record R [I] In the unordered zone, so that R [1 ...... I] and R [I + 1 ...... N] into a new ordered area and a new disordered area respectively. Because each sort adds a record to the ordered area, and the record keywords in the ordered area are not greater than those recorded in the disordered area, that is, R [1 ...... I]. keys <= R [I + 1 ...... N]. keys, so R [1… after N-1 sorting ...... N-1]. keys <= R [n]. key, that is, the entire file R [1 ...... N. Note: At the beginning of the first sorting, the unordered area is R [1 ...... N], the ordered area is empty.

Java program
/*************************** Directly select sorting (Simple selection of sorting) * ************************/public class SelectSort {private void selectSort (int [] datas) {if (datas = null | datas. length <2) return; int minValue; // variable of the minimum value of the unordered partition for (int I = 0; I <datas. length-1; I ++) {minValue = datas [I]; // initialize the minimum variable to the value at the first position of the unordered zone for (int j = I + 1; j <datas. length; j ++) {if (datas [j] <minValue) {minValue = datas [j]; // exchanges two values: int temp = datas [I]; datas [I] = minValue; datas [j] = temp ;}}} public static void main (String [] args) {int [] datas = new int [] {8, 5, 2, 6, 9, 3, 1, 4, 0, 7}; System. out. println ("**********"); for (int I = 0; I <datas. length; I ++) {System. out. print (datas [I] + ",");} SelectSort selectSort = new SelectSort (); selectSort. selectSort (datas); System. out. println ("\ n ********* after sorting ********"); for (int I = 0; I <datas. length; I ++) {System. out. print (datas [I] + ",");}}}
Performance Analysis The average time complexity is O (n2) Direct selection of sorting belongs to local sorting It is unstable to directly select sorting.Basic Idea of heap sorting

Heap sorting is a method that uses full Binary Trees for sorting.

Heap is a Complete Binary Tree first.. Then, one of the following conditions is met:

(1) Ki <= K2i and Ki <= K2i + 1

(2) Ki> = K2i and Ki> = K2i + 1

The heap can be divided into a large root heap (or a heap with the largest root node keyword value) and a small root heap (the smallest root node keyword value.

The basic operation of the big root heap Sorting Algorithm: the initialization operation is to set R [1 ...... N] is constructed as the initial heap. The basic operation of each sort is to exchange the top record R [1] of the unordered zone with the last record in the interval, then adjust the new unordered zone to a heap (also known as rebuilding the heap ). Obviously, you only need to sort the file by N-1 and select a large n-1 keyword to make the file ascending and orderly. Sorting with a small root heap is exactly the same as this sorting type, except that the sorting result is descending and ordered.

Java program
Public class HeapSort {/*** create a large root heap ** @ param datas * array to be sorted * @ param s * parent node * @ param length * Number of unordered items */private void creatHeap (int [] datas, int s, int length) {int temp = datas [s]; for (int j = 2 * s; j <= length; j * = 2) {if (j <length & datas [j] <datas [j + 1]) ++ j; // j is the subscript of a large record in the keyword if (temp> = datas [j]) break; datas [s] = datas [j]; // assign the maximum value to the parent node s = j;} datas [s] = temp; // assign the value of the original parent node to the child node with the maximum value, complete the final switching}/*** heap sorting ** @ param datas * the array to be sorted * @ param index * The number of items to be sorted */private void heapSort (int [] datas, int index) {if (datas = null | index <2) return; for (int I = index/2; I> 0; I --) {creatHeap (datas, i, index) ;}for (int I = index; I> 1; I --) {int temp = datas [I]; datas [I] = datas [1]; datas [1] = temp; // perform creatHeap (datas, 1, I-1) for the remaining values;} public static void main (String [] args) {int [] datas = new int [10]; datas [1] = 6; datas [2] = 5; datas [3] = 3; datas [4] = 1; datas [5] = 8; datas [6] = 7; datas [7] = 2; datas [8] = 4; datas [9] = 9; int index = 9; system. out. println ("**********"); for (int I = 1; I <index + 1; I ++) {System. out. print (datas [I] + ",");} HeapSort heapSort = new HeapSort (); heapSort. heapSort (datas, index); System. out. println ("\ n ********* after sorting ********"); for (int I = 1; I <index + 1; I ++) {System. out. print (datas [I] + ",");}}}
Performance Analysis

The HeapSort time is mainly composed of the time overhead of establishing the initial heap and re-building the heap. Both of them are implemented by calling HeapSort.

The worst time complexity of heap sorting is O (nlgn ). The average time performance of heap sorting is closer to the worst performance. Because the initial heap requires a large number of comparisons, therefore, heap sorting is not suitable for sorting files with a relatively small number of records. The time complexity of the auxiliary space is O (1). the time complexity of heap sorting is O (nlgn ), is an unstable sorting method

References: Data Structure and algorithm analysis-java language description and big talk Data Structure

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.