Algorithm 2: Select sorting and heap sorting directly

Source: Internet
Author: User

Algorithm 2: Select sorting and heap sorting directly

The previous article summarized the Bubble sorting and quick sorting of the exchange sorting. This article will summarize the selection of sorting, which can be divided into direct selection of sorting and heap sorting, mainly from the following points.

1. Directly select sorting and Algorithm Implementation

2. Heap sorting and Algorithm Implementation

1. Directly select sorting and Algorithm Implementation

Straight Select Sort is a simple sorting method. Its basic idea is to compare the elements by length-1, select the smallest element from the length-I + 1 element and exchange the position with the I element. The worst time complexity of sorting is O (n2), and the average time complexity is O (n2)

Shows the process of directly selecting sorting.

1-1,

 

1-2. Code

SelectionSort. java

Public class SelectionSort {public static void main (String [] args) {int [] list = {9, 1, 2, 5, 7, 4, 8, 6, 3, 5}; System. out. println ("**********************"); System. out. println ("Before sorting:"); display (list); System. out. println (""); System. out. println ("after sorting:"); selectionSort (list); display (list);}/*** directly select the Sorting Algorithm */public static void selectionSort (int [] list) {// number of times to be traversed (length-1) for (int I = 0; I <list. length-1; I ++) {// defines the current subscript as the minimum subscript int min = I; // traverses the data after min for (int j = I + 1; j <= list. length-1; j ++) {// if an element smaller than the current minimum value exists, assign its subscript to min if (list [j] <list [min]) {min = j ;}/// if min is not equal to I, the true minimum value if (min! = I) {swap (list, min, I) ;}}/*** swap two-position element in the array */public static void swap (int [] list, int min, int I) {int temp = list [min]; list [min] = list [I]; list [I] = temp ;} /*** print through traversal */public static void display (int [] list) {System. out. println ("********* display start **********"); if (list! = Null & list. length> 0) {for (int num: list) {System. out. print (num + "");} System. out. println ("");} System. out. println ("********* end of display ********");}}

Test results:

2. Heap sorting and Algorithm Implementation

Heap Sort uses the Heap (usually a large root Heap) method to Sort data. The basic idea is to construct the elements to be sorted into a large heap. At this time, the maximum value of the entire sequence is the root node on the top of the heap. Remove it (in fact, it is to exchange it with the end element of the array, at this time the end element is the maximum value), and then re-construct the remaining length-1 element into a large root heap, in this way, the secondary sequence of the length element is obtained. After repeated execution, an ordered sequence can be obtained.

A heap is a complete binary tree with the following properties: the value of each node is greater than or equal to the value of its left and right child nodes; the value of each node is smaller than or equal to the value of its left and right child nodes.

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

2-1,

Figure 1:

Figure 2:

Figure 3:

Figure 4:

Figure 5:

 

Figure 6:

 

2-2. Code

HeapSort. java

Public class HeapSort {public static void main (String [] args) {int [] list = {1, 3, 4, 5, 2, 6, 9, 7, 8, 0}; System. out. println ("************* heap sorting *************"); System. out. println ("Before sorting:"); display (list); System. out. println (""); System. out. println ("sorted:"); heapSort (list); display (list);}/*** heap Sorting Algorithm */public static void heapSort (int [] list) {// construct the unordered heap into a large root heap with length/two parent nodes for (int I = list. leng Th/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 a large root heap for (int I = list. length-1; I> 0; I --) {// swap (list, 0, I) is the last element of the heap top node and the unsorted subsequence ); headAdjust (list, 0, I) ;}/ *** construct a large root heap */public static void headAdjust (int [] list, int parent, int length) {// Save the current parent node int temp = list [parent]; // get the left child node int leftChild = 2 * parent + 1; while (leftChild <l Ength) {// if the parent has the right child, determine whether the left child is less than the right child if (leftChild + 1 <length & list [leftChild] <list [leftChild + 1]) {leftChild ++ ;} // if the Father's Day is greater than the child node, you do not need to exchange if (temp> = list [leftChild]) {break ;} // assign the value of a large subnode to the Father's Day node list [parent] = list [leftChild]; // then, use the subnode as the Father's Day node parent = leftChild; // find the left child node leftChild = 2 * parent + 1;} with a small Father's Day. // Finally, assign the temp value to a large child node, to form a two-value exchange list [parent] = temp;}/*** swap element in two positions in the array */public Static void swap (int [] list, int top, int last) {int temp = list [top]; list [top] = list [last]; list [last] = temp;}/*** print through traversal */public static void display (int [] list) {System. out. println ("********* display start **********"); if (list! = Null & list. length> 0) {for (int num: list) {System. out. print (num + "");} System. out. println ("");} System. out. println ("********* end of display ********");}}

Test results:

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.