# Sorting, finding algorithms

Source: Internet
Author: User

1> Insert Sort

`//Insert Sort (takes the first as well, and then inserts it sequentially into the ordered queue) Average time complexity 0 (n^2)     Public voidInsertsort (int[] a) {         for(inti = 1;i<a.length;i++){             for(intj = i;j > 0;j--){                if(A[j] < a[j-1]){                    intTMP =A[j]; A[J]= A[j-1]; A[j-1] =tmp; }            }        }    }`

2> Hill Sort

`/*Hill Sort: The average time complexity O (n^2) first divides the entire sequence of pending elements into a number of sub-sequences (consisting of an element separated by an "increment"), respectively, and then increments the order, and then sorts the elements in the whole sequence (the increments are small enough), and then to the whole element A direct insert sort*/     Public voidShellsort (int[] a) {        inti,j,gap,tmp,k; intn =a.length;  for(Gap = n/2;gap > 0;gap/=2) {//group until the increment is small enough             for(i = 0;i<gap;i++){                                 for(j = i + gap;j < N;j + =Gap) {                    if(A[j] < a[j-Gap]) {tmp=A[j]; K= J-Gap;  while(k >= 0 && A[k] >tmp) {A[k+ Gap] =A[k]; K-=Gap; } a[k+ Gap] =tmp; }                }            }        }    }`

3> Bubble Sort

` //Bubble Sort: The first element, the N-pass comparison, moving to the last; Average time complexity: O (n^2);     Public voidBubblesort (int[] a) {        intn =a.length;  for(inti = 0;i<n;i++){             for(intj = 0;j < n-i-1;j++){                if(A[j] > a[j + 1]){                    intTMP =A[j]; A[J]= A[j + 1]; A[j+ 1] =tmp; }            }        }    }`

4> Select Sort

`/*Select Sort: In the group of numbers to be sorted, choose the minimum (or maximum) number to exchange with the number of the 1th position, and then find the minimum (or maximum) number of the 2nd position in the remaining number, and so on, until the n-1 element (the penultimate number) and the nth element (the last number)    Comparison. */     Public voidSelectsort (int[] a) {        intindex,tmp; intn =a.length;  for(inti = 0;i<n-1;i++) {Index=i;  for(intj=i+1;j<n;j++){                if(A[j] <A[index]) {Index=J; }} tmp=A[i]; A[i]=A[index]; A[index]=tmp; }    }`

5> Quick Sort

`/*Quick sort: Average time complexity: O (N*LOGN). 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. */    voidFastsort (int[] A,intLeftintRight ) {      if(Left <Right ) {            intLow = left, height = right, x =A[left];  while(Low <height) {               while(Low < height && A[height] >= x)//find the first number less than x from right to leftheight--; A[low]=A[height];  while(Low < height && A[low] < x)//find the first number greater than or equal to x from left to rightlow++; //if (i < j)//s[j--] = s[i];A[height] =A[low]; } A[low]=x; Fastsort (A, low, low-1);//Recursive invocationFastsort (A, low + 1, right); }`

Sorting algorithm Reference: http://blog.csdn.net/happy_wu/article/details/51841244

Find algorithm:

1> Order Lookup

` Public int Find (int[] A,int  x) {        for (int i=0;i<a.length;i++ {            if(a[i] = = x) {                return  i;            }        }         return -1;    }`

2> Two-point search

`//Two-point search, the premise is orderly.      Public intBinaryfind (int[] A,intx) {        intBegin = 0; intEnd =a.length;  while(Begin <end) {            intMiddle = (begin + END)/2; if(A[middle] = =x) {                returnMiddle; }            if(A[middle] <x) {Begin= middle + 1; }            if(A[middle] >x) {Begin= Middle-1; }        }        return-1; }`

Sorting, finding algorithms

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.