Interview Common algorithm-sort find algorithm __ Insert sort

Source: Internet
Author: User

"Summary catalogue of common interview questions >>>"


Algorithm is a programmer must be a skill, often appear in the interview, the following summed up the interview in the common algorithms, these algorithms programmers should keep in mind, to be very skilled.

Insert Sort Algorithm

Principle: The array is divided into unordered area and ordered area of two regions, and then the first element of the unordered area is inserted into the ordered area sequentially, and then all unordered area elements are eventually moved to the ordered area to complete the sort.

Important: Set up sentinels for temporary storage and judgment of array boundaries.

public class Insertsort {
   private static void Insertsort (int[] a) {
       int J;
       int tmp;
       for (int i = 1; i < a.length i++) {
           tmp = a[i];
           for (j = i; j > 0 && tmp < A[J-1]; j--) {
                a[j] = a[j-1];
           }
           A[J] = tmp;}}}

Hill Sorting algorithm

Principle: Also called incremental narrowing sort. Divide the sequence incrementally into groups of the same number of elements, use the direct Insert Sort method, and then keep shrinking until 1, and finally use the direct insert sort to finish sorting.

Important: The choice of increments and the sort end in 1 increments.

public class Shellsort {
    private static void Shellsort (int[] a) {
        int J;
        int tmp;
        for (int gap = A.LENGTH/2 gap >0; Gap/= 2) {for
            (int i = gap; I < a.length;i++) {
                tmp = a[i];
                for (j = i; J >= Gap && tmp< A[j-gap]; J-= Gap) {
                    a[j] = A[j-gap]
                ;
                A[J] = tmp;}}}

Bubble Sort Algorithm

Principle: The sequence is divided into unordered and ordered regions, and the sorting is done continuously by exchanging large elements to the end of the unordered region.

Main points: The Design exchange judgment condition, the early end to arrange the sequence circulation.

public class Bubblesort {
   private static void Bubblesort (int[] a) {for
        (int i = 0; i < a.length-1;i++) {
  
   for (int j = 0; J < a.length-1-I; j + +) {
                if (A[j] > a[j + 1]) {
                    swap (A, J, J + 1);}
           }
    }
   private static void Swap (int[] A, int x, int y) {
       int tmp = a[x];
       A[X] = A[y];
       A[y] = tmp;
    }
}
  

Fast Sorting algorithm

Principle: Constantly looking for the midpoint of a sequence, and then to the midpoint of the sequence of recursion to sort, until the whole sequence of sorting completed, using the idea of partition.

Main points: recursion, divide and conquer

public class QuickSort {
   private static void QuickSort (int[] a) {
       QuickSort (A, 0, a.length-1);
    }
   private static void QuickSort (int[] A, int left, int right) {
       if (left < right) {
           int pivot = A[left];
           int lo = left;
           int hi = right;
           while (Lo < hi) {while
                (Lo < hi &&a[hi] >= pivot) {
                    hi--;
                }
                A[lo] = A[hi];
                while (Lo < hi &&a[lo] <= pivot) {
                    lo++;
                }
                A[hi] = A[lo];
           }
           A[lo] = pivot;
           QuickSort (A, left, lo-1);
           QuickSort (A, lo + 1, right);}}

Simple selection sorting algorithm

Principle: The sequence is divided into disordered and ordered regions, the minimum value in the disordered region and the first element exchange of the disordered region are found, the ordered region expands one, and the loop finally completes the whole order.

public class Selectsort {
   private static void Selectsort (int[] a) {
       int idx;
       for (int i = 0; i < a.length i++) {
           idx = i;
           for (int j = i + 1; j < A.length; J + +) {
                if (A[idx] > A[j]) {
                    idx = j;
                }
           }
           Swap (A, IDX, i);
       }
    }
   private static void Swap (int[] A, int x, int y) {
       int tmp = a[x];
       A[X] = A[y];
       A[y] = tmp;
    }
}

Heap Sorting algorithm

Principle: Using large pile or small Gan thought, first build the heap, then the heap head and heap tail exchange, after the heap tail for ordered area.

Main points: Build, Exchange, adjust the heap

public class Heapsort {private static void Heapsort (int[] a) {//Create a large heap, adjust from the first non-leaf node, and adjust the second
       Non-leaf node ... for (int i = A.LENGTH/2 i >= 0; i--) {Shiftdown (A, I, a.length);
           ///Adjust the large heap, adjust the largest element to the end of the unfinished section for (int i = a.length-1 i > 0; i--) {swap (a, 0, i);
       Shiftdown (A, 0, i);
       } private static void Shiftdown (int[] A, int i, int n) {int child;
       int tmp;
           for (TMP = a[i], I * 2 + 1 < n; i = child) {child = i * 2 + 1;
           if (Child!= n-1 && A[child] < A[child + 1]) {child++;
            } if (TMP < A[child]) {a[i] = A[child];
           } else {break;
    }} A[i] = tmp;
       private static void Swap (int[] A, int x, int y) {int tmp = a[x];
       A[X] = A[y];
    A[y] = tmp; }
}

Merge Sort Algorithm

Principle: The original sequence is divided into ordered two sequences, and then merged by merging algorithm, then it is ordered sequence.

Main points: Merge, divide and treat

public class MergeSort {private static void MergeSort (int[] a) {int[] b = new Int[a.length];
    MergeSort (A, B, 0, a.length-1); } private static void MergeSort (int[] A, int[] b, int left, int right) {if (left < right) {int C
           Enter = left + (right-left)/2;
           MergeSort (A, B, left, center);
           MergeSort (A, B, center + 1, right);
       Merge (A, B, left, center + 1, right); } private static void merge (Int[] A, int[] b, int leftpos, int rightpos, intrightend) {int leftend = Righ
       tPos-1;
       int temppos = LeftPos;
 
       int numelements = Rightend-leftpos + 1;
                while (LeftPos <= leftend && rightpos <= rightend) {if (A[leftpos] <= A[rightpos]) {
                B[temppos] = A[leftpos];
                temppos++;
           leftpos++;
                else {B[temppos] = A[rightpos];
                temppos++;
  rightpos++;         } while (LeftPos <= leftend) {B[temppos] = A[leftpos];
           temppos++;
       leftpos++;
           while (Rightpos <= rightend) {B[temppos] = A[rightpos];
           temppos++;
       rightpos++;
       for (int i = 0; i < numelements i++, rightend--) {a[rightend] = B[rightend]; }
    }
}

Two-point search algorithm

public class BinarySearch {public
   static int BinarySearch (int[] A, int v) {
       int mid;
       int lo = 0;
       int hi = a.length-1;
       while (lo <= hi) {
           mid = lo + ((Hi-lo) >>> 1);//shift operations have lower precedence, with parentheses
           if (a[mid] = v) {//RE is found
                Turn mid;
           } else if (A[mid] < V) {//May be on the right
                lo = mid + 1;
           } else {//may be on the left
                hi = mid-1;
           }
       }
       return-1; Not found
    }
}

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.