Java Learning notes-sorting algorithm advanced ordering (heap sorting and division and sorting)

Source: Internet
Author: User

Spring Silkworm to the dead silk side, wax torch into Ashes tears began to dry

--Untitled

Here are two more difficult algorithms:

1. Heap Sequencing

2. Divide and conquer and sort

Say the heap first.

Let us first understand the data structure of the complete binary tree.

The heap is a completely binary tree. The large top heap is in the heap, and any parent value is greater than (or equal to) its child value, which is called the Big Top heap.

Steps to sort the heap:

1. Think of the array as a heap. The index+1 of an array is its corresponding ordinal in the heap

2, the order of the values in the heap, get the big Top heap

3. Exchange the first value of the heap with the end value of the heap, and sort the maximum value.

4. Reduce the heap size by 1, repeat steps 2 and 3 until only one element is left in the heap. Sort finished

On the code:

1  Public classHeapsort {2 3      Public Static voidHeapsort (int[] arr) {4         //set up a complete binary tree, starting with the last parent to adjust the parent value until the root, after the completion of the large top heap5          for(inti = Arr.length >>1; i >0; I--) {6Heapadjust (arr, I, arr.length);//The call heap must be from 1 to length to meet the definition of the heap7         }8         //Heap top and heap low swap, get maximum value, then adjust large top heap9          for(inti = arr.length-1; i >0; i--) {TenArr[i] = arr[i]^arr[0]; Onearr[0] = arr[i]^arr[0]; AArr[i] = arr[i]^arr[0]; -Heapadjust (arr,1, i);//because the heap count starts at 1, size = EndIndex + 1 -         } the     } -     //Adjust the large top heap. First find left dial hand, then and right son ratio, take value big, in and parents themselves than, oneself than son big, break, otherwise exchange. Note: The root must start at 1 to find left dial hand -      Public Static voidHeapadjust (int[] arr,intParents,intsize) { -         intJ//The children's mark is J, index all-1 +         inti = parents;//parents are I, index all-1 -          while(I <<1<=size) { +j = i <<1;//Left dial Hand A             if(j +1<= size) {//have right son at                 if(Arr[j-1] < Arr[j +1-1]) -J + +; -             } -             if(Arr[i-1] > Arr[j-1]) -                  Break; -arr[i-1] = arr[i-1]^arr[j-1]; inarr[j-1] = arr[i-1]^arr[j-1]; -arr[i-1] = arr[i-1]^arr[j-1]; toi = j;//son to father, here do not know is left dial hand or right son, so can not directly through the For loop iteration step <<i Adjustment I value, if the right son is wrong (right son <<1+1) +         } -     } the}

Then divide and conquer and sort.

Here's the first thing to know about recursion.

1  Public classMergingtest {2 3      Public Static voidMain (string[] args) {4Msort (0,3);5     }6     Private Static voidMsort (intLeftintRight ) {7         intM = (left + right)/2;8         if(left = =Right ) {9System. out. println (left);Ten             return; One         } A Msort (left, m); -Msort (m+1, right); -     } the}

These lines of code are the core of the algorithm. Running the code will output 0123456789, although it looks simple, but if you really understand that you already fully understand the idea of recursion, write and return the algorithm is not easy.

Why does it output 0123?

The direction of code execution: 1→2→4→2→5→2→1→3→6→3→7→3→1→return

Can understand this thing is good to run, on the code:

1  Public classMergingsort {2 3      Public Static voidMergingsort (int[] arr) {4         int[] temp =New int[Arr.length];
Temp is the equivalent of a card, through the left,m,right logically divided into two arrays, to divide and conquer the order, arr is the original array, array sorting does not pass arrays how to do?! 5Msort (arr, temp,0, arr.length-1);6 }7 Private Static voidMsort (int[] arr,int[] temp,intLeftintRight ) {
The grouping logic here does not use temp and arr, but instead passes them as parameters to the merge method8 intm = (left+right)/2;9 if(left = =Right ) {Ten return; One } A msort (arr, temp, left, m); -Msort (arr, temp, m+1, right); - merge (arr,temp,left,m,right); the } - Private Static voidMergeint[]arr,int[] temp,intLeftintMintRight ) {
Sorting is extremely simple, known two ordered arrays, to merge them into an ordered array, with what method do not have to say, everyone think about it. - for(intc =0; c < arr.length;c++){ -TEMP[C] =Arr[c]; + } - for(intp =0; P < temp.length; p++) { +System. out. println ("temp["+p+"] = "+temp[p]); A } at inti =Left ; - intj = m +1; - intK =Left ; - while(I<=m && j<=Right ) {
Use the branching structure to pick out the smallest of each stack of cards to arr - if(Temp[i] <Temp[j]) { -arr[k++] = temp[i++]; in } - if(Temp[j] <Temp[i]) { toarr[k++] = temp[j++]; + } - }
Use the branching structure to put all the remaining stacks of cards in arr the if(I >m) { * while(J <=Right ) { $arr[k++] = temp[j++];Panax Notoginseng } - } the if(J >Right ) { + while(I <=m) { Aarr[k++] = temp[i++]; the } + } - } $ $}

Java Learning notes-sorting algorithm advanced ordering (heap sorting and division and 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.