Some algorithms that can be thought of

Source: Internet
Author: User

Bubble sort
Ideas: A total comparison of n-1 round, each comparison of the adjacent two number, the large number is placed in the back, after the first round of comparison will be the largest
The second round, in addition to the last one, will put the second-largest number in the second-lowest. Until the end.

Insert Sort
The array is divided into two parts, the first half is a good sequence, each take the second half of the data, followed by each number in the first half of the comparison, if less than the backward shift, if the number is greater than fixed in the subscript, the back part of the next move
There are such arrays [1 5 6 3 7 9 4 8]

[1| 5 6 3 7 9 4 8] divided into 1  and behind two sections (just abstract divided into two parts)
[1 5| 6 3 7 9 4 8] The first sort 5>1 placed behind 1 is now divided into [1 5] and the remainder
[1 5 6| 3 7 9 4 8] The second sort 6>1 6>5 6 is placed behind 5 | Dividing the array into two parts
[1 3 5 6| 7 9 4 8] The third sort 3>1 3<5 places 3 in the original 5 position, and 5 6 translates backwards.

The rest is not written, that's what it means. Code is also implemented, the only possible problem is to find the location, after each one needs to move back one, as long as careful on it.

Select sort

The first round selects the smallest of the first, and the second round selects the remaining smallest in the second place. Until the end.

Quick Sort

The

is one of the more complex of the above.
The overall idea of choosing a base, will be greater than this cardinality on the right, less than this base on the left. Select a cardinality from the left side, and divide the left side in two parts according to the method above. Until the final left only has two numbers left, order to the right in turn.
such an array [7 5 10 3 7 9 4 8], round to say

 First round: 7 as cardinality 
first from the right, 8>7 skip 8
[4l 5 3 7 9 4h 8] 4<7 7 to 4 H for the position of high, this position is waiting for the replacement
to the right to find a number less than the base 7 after the left Edge
[4 5 10l 3 7 9 4h 8] Now the first position is 4, 4 5<=7 skipped, 10 is greater than 7, and the position of 10 is marked L (lowercase l low)
[4 5 10l 3 7 9 10h 8] Convert high data (original 4) to L ow data (10) to complete the loop but not yet complete the first round
[4 5 10l 3 7h 9 10 8]] The next second loop first determines where the right is greater than the base. The position of high continues to move forward 9>7 Skip, to 7 of the position, the
generally if encountered the same number, is calculated in the smaller than the base of the portion, that is, when judging greater than the cardinality with Numbers[high] > Temp
When judging less than the cardinality with Numbers[low] <= temp, this temp is the cardinality (7 of the first position of the original data here)
encounters 7 less than base 7, where 7 is Labeled H
[4 5 7l 3 7h 9 10 8] The position of high data to Low,low is changed from 10 to 7
[4 5 7 3 7LH 9 8] 7<=7 3<=7 now out of the loop in the same position , the position of low is assigned a value of base 7. The
Now 7 of the LH position is actually a copy of 7 of the fifth position in the original array, which is not actually the cardinality of the first position in the original array 7. Don't get mixed up by these two 7.
Assuming that the fifth position in the original array is not 7 is 6, then when you want to be in the case will be [4 5 6 3 6LH 9 10 8], repeat two 6, the original cardinality is not in the array.

By the end of the first level here, the left <=7 of base 7 has been implemented, the right >=7, and cardinality 7 has reached the correct position. The next step is to sort the left by the method above, sorting the right. Paste the Java code

   Public Static voidMain (string[] args) {int[] numbers = {1, 3, 9, 5, 6, 7, 2, 0, 8, 4, 9};    Quicksort.quick (numbers);  for(inti:numbers) System.out.print (i+ " "); }   Public Static classQuickSort { Public Static voidQuickint[] numbers) {      if(Numbers.length > 0) {QuickSort (numbers,0, Numbers.length-1); }    }    Private Static voidQuickSort (int[] numbers,intLowintHigh ) {      if(Low <High ) {        intMiddle =Sortandgetmiddle (Numbers, low, high); QuickSort (Numbers, low, middle-1); QuickSort (Numbers, Middle+ 1, high); }    }    Private Static intSortandgetmiddle (int[] numbers,intLowintHigh ) {      inttemp =Numbers[low];  while(Low <High ) {         while(Low < High && Numbers[high] >temp) { High--; } Numbers[low]=Numbers[high];  while(Low < High && Numbers[low] <=temp) { Low++; } Numbers[high]=Numbers[low]; } Numbers[low]=temp; returnLow ; }  }

Fast sorting performance is less stable, adding data such as [9, 8, 7, 6, 5, 4, 3, 2, 1] then it became bubbling. How to choose this base can be specifically adjusted.

Two-difference tree

Balanced two-pronged tree

Some algorithms that can be thought of

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.