Algorithm 2 sorting algorithm: Direct selection of sorting and heap sorting

Source: Internet
Author: User

The previous article summarizes the bubble sort and quick sort of the interchange sort. This article summarizes the selection of sorting, the choice of sorting is divided into direct selection of sorting and heap sorting, mainly from the following points to summarize.

1. Direct selection sequencing and algorithm implementation

2. Heap sequencing and algorithm implementation

1. Direct selection sequencing and algorithm implementation

The direct selection sort (Straight select sort) is a simple sorting method whose basic idea is to select the smallest element from the length-i+1 element and exchange the position with the first element by comparing the LENGTH-1 elements. The worst time complexity for direct selection sorting is O (N2), and the average time complexity is O (n2)

Shows the process of directly selecting a sort.

1-1.

1-2. Code

Selectionsort.java

 Public classSelectionsort { Public Static voidMain (string[] args) {int[] List = {9, 1, 2, 5, 7, 4, 8, 6, 3, 5}; System.out.println ("************ Direct Select Sort ************"); System.out.println ("Before sorting:");        Display (list); System.out.println (""); System.out.println ("After sorting:");        Selectionsort (list);    Display (list); }    /*** Direct selection sorting algorithm*/     Public Static voidSelectionsort (int[] list) {        //number of iterations to traverse (length-1 times)         for(inti = 0; i < list.length-1; i++) {            //define the current subscript as the minimum subscript            intMin =i; //traverse the data behind Min             for(intj = i + 1; J <= List.length-1; J + +) {                //If there is an element less than the current minimum value, assign its subscript to min                if(List[j] <List[min]) {min=J; }            }            //if min is not equal to I, the description finds the true minimum value            if(min! =i) {swap (list, Min, i); }        }    }    /*** Swap elements of two positions in an array*/     Public Static voidSwapint[] list,intMininti) {inttemp =List[min]; List[min]=List[i]; List[i]=temp; }    /*** Traverse Print*/     Public Static voidDisplayint[] list) {System.out.println ("******** Show begins ********"); if(List! =NULL&& list.length > 0) {             for(intnum:list) {System.out.print (num+ " "); } System.out.println (""); } System.out.println ("******** Show ends ********"); }}

Test results:

2. Heap sequencing and algorithm implementation

Heap sort (heap sort) uses a heap (typically Dagen) to sort the method. Its basic idea is to construct the elements to be sorted into a large heap. At this point, the maximum value of the entire sequence is the root node of the heap top. Remove it (in fact, swap it with the end element of the array, at which point the element at the end is the maximum value), and then re-construct the remaining length-1 elements into a large heap, so that you get the second largest value in the length element. With this repeated execution, an orderly sequence can be obtained.

A heap is a complete binary tree with the following properties: Each node's value is greater than or equal to the value of its left and right child nodes, called Dagen; each node's value is less than or equal to the value of its left and right child nodes, called the small Gan.

The worst time complexity for heap sequencing is O (n*log2n), and the average time complexity is O (n*log2n)

2-1.

Figure One:

Figure II:

Might

Figure IV:

Figure V:

Figure VI:

2-2. Code

Heapsort.java

 Public classHeapsort { Public Static voidMain (string[] args) {int[] list = {1, 3, 4, 5, 2, 6, 9, 7, 8, 0}; System.out.println ("************ Heap Sort ************"); System.out.println ("Before sorting:");        Display (list); System.out.println (""); System.out.println ("After sorting:");        Heapsort (list);    Display (list); }    /*** Heap Sorting algorithm*/     Public Static voidHeapsort (int[] list) {        //constructs an unordered heap into a large heap, Dagen has LENGTH/2 parent node         for(inti = LIST.LENGTH/2-1; I >= 0; i--) {headadjust (list, I, list.length); }        //gradually swap the root node of each maximum value with the end element, and then adjust it to Dagen         for(inti = list.length-1; i > 0; i--) {            //swaps the last element of the heap top node and the currently unsorted subsequence positionSwap (list, 0, i); Headadjust (list,0, i); }    }    /*** Construction Dagen*/     Public Static voidHeadadjust (int[] list,intParentintlength) {        //Save current parent node        inttemp =List[parent]; //get left child node        intLeftchild = 2 * parent + 1;  while(Leftchild <length) {            //If the parent has a right child, determine if the left child is less than the right child            if(Leftchild + 1 < length && List[leftchild] < List[leftchild + 1]) {leftchild++; }            //Father node is greater than the child node, there is no need to exchange            if(Temp >=List[leftchild]) {                 Break; }            //assigns the value of a larger child node to the Father nodeList[parent] =List[leftchild]; //then make the child node the parent nodeParent =Leftchild; //find the Father node with a small left child nodeLeftchild = 2 * parent + 1; }        //Finally, assign the temp value to a larger child node to form a two-value interchangeList[parent] =temp; }    /*** Swap elements of two positions in an array*/     Public Static voidSwapint[] list,intTopintLast ) {        inttemp =List[top]; List[top]=List[last]; List[last]=temp; }    /*** Traverse Print*/     Public Static voidDisplayint[] list) {System.out.println ("******** Show begins ********"); if(List! =NULL&& list.length > 0) {             for(intnum:list) {System.out.print (num+ " "); } System.out.println (""); } System.out.println ("******** Show ends ********"); }}

Test results:

Welcome reprint, but please keep the original source of the article

This address: http://www.cnblogs.com/nnngu/p/8282291.html

Algorithm 2 sorting algorithm: Direct selection of 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.