# Sorting and finding

Source: Internet
Author: User

Organized from: http://blog.csdn.net/morewindows/article/details/6684558

http://blog.csdn.net/pzhtpf/article/details/7560294

First, bubble sort

(1) The basic idea: in order to sort a group of numbers, the current is not yet ranked in the range of all the number, from top to bottom of the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller upward. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged.

(2) Example:

Java implementations:

` Public classBubblesort { PublicBubblesort () {inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51}; intTemp=0;  for(inti=0;i<a.length-1;i++){         for(intj=0;j<a.length-1-i;j++){        if(a[j]>a[j+1]) {temp=A[j]; A[J]=a[j+1]; A[j+1]=temp; }        }    }     for(inti=0;i<a.length;i++) System.out.println (A[i]); }}`

Second, quick sort

The quick sort is a sort of division exchange proposed by C.r.a.hoare in 1962. It adopts a strategy of division, which is usually referred to as the Division method (Divide-and-conquermethod).

The basic idea of this method is:

1. First, a number is taken from the series as the base number.

2. The partitioning process, which puts the number of large numbers on its right, is less than or equal to its number to the left.

3. Repeat the second step for the left and right intervals until there is only one number for each interval.

Although the quick sort is called the divide-and-conquer method, the three words of the divide-and-conquer method clearly fail to generalize all the steps of the quick sort. So my quick sort is further explained: Pit filling + divide-and-conquer method:

Let's take a look at the example, and give it the definition below (it would be better to summarize the definition in your own words, which would be helpful for implementing the code).

Taking an array as an example, the first number of intervals is the base count.

 0 1 2 3 4 5 6 7 8 9 72 6 57 88 60 42 83 73 48 85

Initially, i = 0;   j = 9; X = A[i] = 72

Since the number in a has been saved to X, it can be understood that a hole has been dug in the array a, and other data can be populated here.

Looking forward from J to a number smaller than x or equal to X. When j=8, meet the conditions, will a dug up and then fill in the last pit a. A=A;  i++; Such a pit a was done, but formed a new pit a, what to do? Simple, and then find the number to fill a this pit. This time from I began to find a number greater than X, when i=3, meet the conditions, will a dug up and then filled in the last pit a=a; j--;

The array becomes:

 0 1 < P align= "right" >2 3 4 5 6 7 8 9 48 6 57 88 60 42 83 73 88 85

i = 3;   j = 7; x=72

Repeat the above steps, looking forward from behind, then looking backwards .

Start looking forward from J, when j=5, meet the conditions, will a dug into the last pit, a = a; i++;

From I start looking backwards, when i=5, due to i==j exit.

At this point, I = j = 5, and a is just the last hole dug, so X is filled into a.

The array becomes:

 0 1 < P align= "right" >2 3 4 5 6 7 8 9 48 6 57 42 60 72 83 73 88 85

You can see that the number before a is less than it, and the number behind A is greater than it . So again to a[0 ... 4] and a[6 ... 9] These two sub-zones repeat the above steps.

Summarize the number of digging pits

1. I =l; j = R; Gouge out the base number to form the first pit a[i].

2. j--from the back forward to find a number smaller than it, found after digging out of this number before filling a pit a[i].

3. i++ from the front to find a larger number than it, find and dig out this number to fill the previous pit a[j].

4. Repeat 2, 32 steps until I==j, and fill in the base number into a[i].

According to this summary it is easy to achieve the number of pit fill code:

`intAdjustarray (intS[],intLintR//returns the position of the adjusted base number{    inti = l, j =R; intx = S[l];//S[l] That is the first pit of S[i]     while(I <j) {//find the number less than X from right to left to fill s[i]         while(I < J && S[j] >=x) J--; if(I <j) {S[i]= S[j];//fill s[j] into s[i], s[j] form a new pit.i++; }        //find a number greater than or equal to x from left to right to fill s[j]         while(I < J && S[i] <x) I++; if(I <j) {S[j]= S[i];//fill s[i] into s[j], s[i] form a new pit.j--; }    }    //when exiting, I equals J. Fill in the hole with X. S[i] =x; returni;}`

Then write the code for the Divide and conquer method:

`void quick_sort1 (intintint  r) {    if (L < r)    {         int i = Adjustarray (S, L, R); // first into the excavation pit filling method adjustment s[]        //         Quick_sort1 (S, i + 1, r);}    }`

Such code is obviously not concise enough to combine it with:

`//Quick SortvoidQuick_sort (intS[],intLintR) {    if(L <r) {//Swap (S[l], s[(l + R)/2]);//swap this number and first number in the middle to see note 1        inti = l, j = r, x =S[l];  while(I <j) { while(I < J && S[j] >= x)//find the first number less than x from right to leftj--; if(I <j) S[i++] =S[j];  while(I < J && S[i] < x)//find the first number greater than or equal to x from left to righti++; if(I <j) S[j--] =S[i]; } S[i]=x; Quick_sort (S, l, I-1);//Recursive invocationQuick_sort (S, i + 1, R); }}`

Three or two-point method search

` Public classBinarySearch { PublicBinarySearch () {List<Integer> list=NewArraylist<integer>();  for(inti=0;i<10000;i+=2) {//Adding all the even numbers to the list that gradually increases by 1-10000 as an experimental array, it is clear that he is orderly! List.add (i);//There's also an array of available    }      intLow=0; intHigh=list.size (); intkey=3334;  while(low<=High ) {          intMid= (Low+high)/2; if(key==List.get (mid)) {System.out.println (The position of this value in the list is: "+mid);  Break; }      if(key>List.get (mid)) { Low=mid+1;//when less than, the low pointer moves backwards and the high pointer remains unchanged    }      if(key<List.get (mid)) { High=mid-1;//when greater than, the high pointer moves forward, and the low pointer does not change    }            }      if(low>High ) {System.out.println ("No results found!" "); }  }  }  `

Sorting and finding

Related Keywords:

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.