Prepare for the coming interview... Data Structure

Source: Internet
Author: User

1. Exchange sorts

Bubble sort | cocktail sort | odd-even sort | comb sort | gnome sort | quicksort

I think the top two popular algorithms are bubble sort and quick sort, expecially the quick sort which is widely used.

Bubble sort

#region Bubble sortpublic static void BubbleSort(int[] array){for (int i = 0; i < array.Length; i++){for (int j = 0; j < array.Length - i - 1; j++){if (array[j] > array[j + 1]){array[j] = array[j] + array[j + 1];array[j + 1] = array[j] - array[j + 1];array[j] = array[j] - array[j + 1];}}}}

It compares each pair of adjacent items and swap them if they are in the wrong order, the pass through the list is repeated until no swaps are needed, then we get the sorted array.
After increment of the outer circle we get the biggest number from the current not sorted array.

 

Qucik sort

#region Quick sort/// /// Average: O(nlog2N)/// The worst: O(n^2)/// ///public static void QuickSort(int[] array){_QuickSort(array, 0, array.Length - 1);}private static void _QuickSort(int[] array, int low, int high){if (low <= high){int pivot = array[low]; //we can change this as we likeint i = low - 1;int j = high + 1;while (true){while (array[++i] < pivot) ;while (array[--j] > pivot) ;if (i >= j){break;}array[i] = array[i] + array[j];array[j] = array[i] - array[j];array[i] = array[i] - array[j];}//recursion_QuickSort(array, low, i - 1);_QuickSort(array, j + 1, high);}}

I think this is the most famous one in all these algorithms
1: Choose a records from the list
2: Separate the list to two parts, and ensure the left of the specified numbers are all less then equal, and the right of the specified numbers are all bigger the same
3: Then recursion the Left and Right Parts

 

2, insertion sorts

Insertion Sort | shell sort | tree sort | library sort | patience sorting
The first two algorithms are well-known

Insertion Sort

////// The aging rate of the basic order is significantly higher, and the second cycle is controlled in the circulating external body // completely ordered (ascending) ---> linear O (N) /// completely ordered (down) ---> worst O (N ^ 2 )//////Public static void insertionsort (INT [] array) {Int J; For (INT I = 1; I <array. length; I ++) {int toinsertelement = array [I]; for (j = I; j> 0 & toinsertelement <array [J-1]; j --) {array [J] = array [J-1]; // move a [J-1] to a [J]} array [J] = toinsertelement; // insert here }}

Loop the items in the list and compare the item with the other items which in the left hand of it, and if the item is bigger then it, move it back, until we find the correct position and insert it there.

 

Shell sort

/// /// It's just like insertion sort, change the 1 to d, and when d comes 1, we get the sorted array/// usually we think it's complexity is O(n^(3/2))/// The better choice of d is such as ...121,40,13,4,1/// S(i) = 3S(i-1)+1/// //////public static void ShellSort(int[] array, int d){int j;for (int i = d; i < array.Length; i++){int toInsertElement = array[i];for (j = i; j > d - 1 && toInsertElement < array[j - d]; j -= d){array[j] = array[j - d];}array[j] = toInsertElement;}}

It's just like insertion sort, the difference is Shell sort use a custom "D" instead of "1"

 

3, selection sorts

Selection sort | heapsort | smoothsort | Cartesian tree sort | tournament sort
Also the first two algorithms are well-known

Selection sort

public static void SelectionSort(int[] array){for (int i = 0; i < array.Length - 1; i++){for (int j = i + 1; j < array.Length; j++){if (array[i] > array[j]){array[i] = array[i] + array[j];array[j] = array[i] - array[j];array[i] = array[i] - array[j];}}}}

Select the smallest one to the head, and until the list sorted.

 

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.