Algorithm is a programmer must be a skill, often appear in the interview, the following summarizes the interview in the common algorithms, these algorithms should be kept in mind, the programmer should be very skilled.
Insert Sort Algorithm
Principle: Divide the array into unordered and ordered two regions, and then continuously insert the first element of the unordered area into the ordered area in order of size, and eventually move all the unordered elements to the ordered area to complete the sorting.
Important: Set up Sentinel as a temporary storage and judgment array boundary.
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 Sort Algorithm
Principle: Also known as incremental reduction sorting. The sequence is divided by increments into groups of the same number of elements, sorted using the direct insertion sort method, then decreasing the increment to 1, and finally using the direct insert sort to complete the sorting.
Important: Incremental selection and sorting 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 areas, and is continuously sorted by swapping large elements to the end of the unordered area.
Key points: Design Exchange judgment conditions, ending early in order to arrange the sequence of the loop.
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 searching for the midpoint of a sequence, and then sorting the sequence around the midpoint, until all sequences are sorted, using the idea of divided treatment.
Main points: recursion, Division and treatment
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 unordered and ordered regions, searching for the minimum value in the disordered area and the first element exchange of the disordered area, the order area expands one, the loop finally completes all sorts
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 the idea of large root heap or small Gan, first build the heap, then exchange the heap head with the heap tail, and then the ordered area after the heap tail.
Important: Build heaps, swap, and adjust heaps
public class Heapsort { private static void Heapsort (int[] a) { //Create a large heap first, start with the first non-leaf node, then adjust the second non-leaf node ... for (int i = A.LENGTH/2; I >= 0; i--) { Shiftdown (A, I, a.length); } Adjusts the large heap, adjusting the largest element to the end of the not-ordered part 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 two sequential sequences, and then the merging algorithm is used to merge, then it is ordered sequence.
Key points: Merge, Divide and conquer
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 cent ER = 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 = Rightpo s-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]; } }}
binary 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 operation has a lower priority, use parentheses if (a[mid] = = v) {//have found re 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 }}
Common algorithms for interviewing-sorting lookup algorithms