"Algorithmic supplements (Java descriptive narration)"---Select sort (direct select sort, heap sort)

Source: Internet
Author: User

The basic idea of choosing a sort

Each trip selects the smallest record of the keyword from the record to be sorted, placing the order at the end of the ordered sub-file, knowing that all records are sorted. There are two main options for sorting: Direct selection of sort (or simple selection sort) and heap sorting.

Direct selection of the basic idea of sorting

At the beginning of the first sequencing, the current ordered and unordered areas are r[1 ... i-1] and r[i ... n] (1 <= i <= n-1), which is the smallest record of a keyword from the current unordered region R[k], which is exchanged with the first record of the unordered zone r[i], so r[1 ... I ] and r[i+1 ... n] into a new ordered area and a new unordered area.

Since each sort of order adds a record to the ordered area, and the record keywords in the ordered area are not greater than the keywords recorded in the unordered area, that is, after the first order of the r[1 ... i].keys <= r[i+1 ... n].keys, so after n-1 sequencing r[1 ... n-1]. Keys <= R[n].key, that is, after n-1 sequencing, the entire file r[1 ... n] is incremented and ordered. Note that when the first trip is started, the sort starts. Unordered area is r[1 ... n], ordered area is empty.

Java programs
/************************* * * Direct selection sort (simple selection sort) * *************************/ Public  class selectsort {    Private void Selectsort(int[] datas) {if(Datas = =NULL|| Datas.length <2)return;intMinValue;//unordered zone min variable         for(inti =0; I < datas.length-1; i++) {minValue = datas[i];//Initialize the minimum variable to the value on the first position in the unordered area             for(intj = i +1; J < Datas.length; J + +) {if(Datas[j] < MinValue) {minValue = datas[j];//Exchange two values                    inttemp = 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 ("******** sort before ********"); for(inti =0; i < datas.length; i++) {System.out.print (Datas[i] +","); } Selectsort Selectsort =NewSelectsort ();        Selectsort.selectsort (datas); System.out.println ("\n******** ******** after sorting"); for(inti =0; i < datas.length; i++) {System.out.print (Datas[i] +","); }    }}
Performance analysis
    1. Average time complexity of O (N2)
    2. Direct selection sort belongs to in-place sort
    3. Direct selection of the sort is not stable
Basic idea of heap sorting

Heap sorting is a method of sorting with a completely binary tree

The heap is first a completely binary tree .

Then meet one of the conditions:

(1) Ki <= k2i and ki <= k2i+1

(2) Ki >= k2i and ki >= k2i+1

The heap has Dagen (or the largest heap with the root node key value) and small Gan (the root node keyword is the smallest) of the points.

Dagen the basic operation of the sorting algorithm: the initialization operation is to construct the r[1 ... n] as the initial heap; The basic operation of each sequencing is to exchange the top record of the current unordered area r[1] and the last record of the interval. The new unordered area is then resized to the heap (also known as the Rebuild heap).

Obviously only need to do n-1 sequencing. Selecting a larger n-1 keyword will increase the order of the files. Sorting with small Gan is completely similar to this, except that the ordered result is descending and orderly.

Java programs
 Public  class heapsort {    /** * Build a large heap * * @param datas * Queue array * @param S * Parent node * @param length * Number of unordered items */    Private void Creatheap(int[] datas,intSintLength) {inttemp = Datas[s]; for(intj =2* s; J <= Length; J *=2) {if(J < length && Datas[j] < Datas[j +1]) ++j;//J is the subscript of the larger record in the keyword            if(temp >= datas[j]) Break; Datas[s] = Datas[j];//assigns the maximum value to the parent nodes = j; } Datas[s] = temp;//assigns the value of the original parent node to the child node with the maximum value. Finally the Exchange}/** * Heap Sort * * @param datas * Array to sort * @param Index * The number of items to be sorted in the array you want to sort * /    Private void Heapsort(int[] datas,intIndex) {if(Datas = =NULL|| Index <2)return; for(inti = index/2; i >0;        i--) {creatheap (datas, I, index); } for(inti = index; i >1; i--) {inttemp = Datas[i]; Datas[i] = datas[1]; datas[1] = temp;//Rebuild heap operation on remaining valuesCreatheap (Datas,1I1); }    } Public Static void Main(string[] args) {int[] Datas =New int[Ten]; 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;intindex =9; System.out.println ("******** sort before ********"); for(inti =1; I < index +1; i++) {System.out.print (Datas[i] +","); } heapsort heapsort =NewHeapsort ();        Heapsort.heapsort (datas, index); System.out.println ("\n******** ******** after sorting"); for(inti =1; I < index +1; i++) {System.out.print (Datas[i] +","); }    }}
Performance analysis

The time of the heap ordering is mainly composed of the time overhead of the two parts: the initial heap and the duplicate rebuild heap, all of which are implemented by calling Heapsort.

    1. The worst time complexity for heap sorting is O (NLGN). Average time performance of heap sequencing is closer to worst-case performance
    2. Heap sorting is not appropriate for files with fewer records due to the number of times required to build the initial heap
    3. Heap sort is low sort, auxiliary space is O (1)
    4. The time complexity of heap sequencing is O (NLGN), which is an unstable sorting method

Reference data: Data structure and algorithm analysis--java language description narrative, "Big talk data Structure"

"Algorithmic supplements (Java descriptive narration)"---Select sort (direct 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.