Common sorting and Selection Algorithms

Source: Internet
Author: User

1 insert order

The left side of the insert order is ordered. One number is taken from the right side and inserted to the left until there is no more number on the right side. At this time, all the numbers on the left are sorted in order.

Insert and sort in C Language

void insertion_sort(int array[], int length){int i,j;for(i=1;i<length;i++){for(j=i;j>0;j--){if(array[j]<array[j-1]){int temp = array[j];array[j] = array[j-1];array[j-1] = temp;}elsebreak;}}}

The average time complexity of insertion sorting is O (n ^ 2). When the input sequence is sorted, it can reach O (n ). Therefore, when the array is partially sorted, insertion sorting is more efficient.

Here is a dance video about insertion sorting: http://t.cn/hros0W. In any case, videos can only help you understand a general idea of sorting. However, it is true that only code is used to implement video sorting. Sometimes, an equal sign is missing and an iteration is missing, will result in errors. To fully understand the application of each algorithm, we also need to accumulate experience and good mathematical skills in our work.

2. bubble sort

Bubble is probably one of the best sorting algorithms, but it took some time for me to understand it during my first contact. In terms of name, the Bubble Sorting is like a bubble in the water. The largest elements are constantly emerging and ranked at the end of the entire array. Specifically, For the first iteration, the largest elements are arranged at the end of the array, and for the second iteration, the second and third iterations of the array are arranged, the third largest element will be ranked in the last third of the array ...... And so on.

The average time complexity of counterfeit sorting is O (n ^ 2), the worst case is O (n ^ 2), and the best case is O (n ).

C implementation code for Bubble Sorting:

void bubbleSort(int arr[],int len){int i,j;int flag;for(i = 0;i < len;i++){flag = 0;for(j = 0; j < len -i -1; j++){if(arr[j]>arr[j+1]){//swap arr[j], arr[j+1];int tmp = arr[j];arr[j] = arr[j+1];arr[j+1] = tmp;flag = 1;}}/*if in this loop, flag has never been changed, then the whole array is already sorted*/if(flag == 0){break;}}}

Bubble sort dance video: http://t.cn/hrf58M

3. Fast sorting

Fast sorting, like its name, is one of the fastest sorting. The average time complexity of quick sorting isO (nlogn)The worst case isO (N ^ 2).

Quick sorting is a recursive sorting algorithm. First, select an axis element and route the elements in the array smaller than the axis element to the left and those in the array larger than the axis element to the right. Then, recursively sort the Left and Right Parts of the sequence in a quick order. Until the overall implementation is orderly. Here, the algorithms that route elements to the two sides of the partition are called the partition algorithm. The best choice of parallelism is to select the median in an array. If the max or min in an array is accidentally selected, the worst case may occur. The specific analysis is not mentioned here. If you want to know the introduction to CLRS algorithms, there is a detailed partition algorithm, and how to select a good partition.

The C language for fast sorting is simple:

void quicksort(int array[],int left,int right){int index = partition(array,left,right);/*recursively sort the left part*/if(left<index-1){quicksort(array,left,index-1);}/*recursively sort the right part*/if(index<right){quicksort(array,index,right);}}/*return the index of pivot element*/int partition(int array[],int left,int right){int pivot = array[(left+right)/2];while(left<right){/*find the element that should on right side of pivot*/while(array[left]<pivot){left++;}/*find the element that should on left side of pivot*/while(array[right]>pivot){right--;}/*swap left and right*/if(left<=right){int tmp = array[left];array[left] = array[right];array[right] = tmp;left++;right--;}}return left;}

Also at the end of the dance video: http://t.cn/ScTA1d

4. Merge and sort merge sort

I can't figure out why we need to translate merge into a merge...

Merge sort also uses recursion. More specifically, it uses divide and conquer's division and governance idea. Division: divides an array into two sections, sorts the left first, and then sorts the right, merge: Finally, It is merged into an array.

Pseudocode:

Void mergesort (

Left = mergesort (array, 0, middle );

Right = mergesort (array, middle, end );

Merge (left, right );

}

The average and worst time complexity of merge sort are O (nlogn). For how to obtain it, see CLRS in the introduction to algorithms. To put it simply, set the time complexity to T (n ), then the pseudo code above can introduce a recursive formula: T (n) = 2 T (n/2) + O (N), here the time complexity of the O (n) Last merge process, we can find that T (n) is O (nlogn) and there is a formula. You can also deduce or guess an answer to prove it. In short, there are many methods, if your undergraduate algorithm class is good, you should be impressed.

Let's briefly talk about the merge process. Now there are two sorted arrays, and then we need to combine them. How can we do this? Iteration starts from the headers of the two arrays, compares them one by one, and removes the small ones until the iteration is completed.

The C language implementation of merge sort:

void mergeSort(int a[],int start,int end){if(start<end){int mid = (start+end)/2;mergeSort(a,start,mid);mergeSort(a,mid+1,end);merge(a,start,mid,end);}}void merge(int a[], int start, int mid, int end){int i = start;//copy array a into a temporary helper arrayint helper[end];while(i<=end){helper[i] = a[i];i++;}//start to merge left part and right partint left = start;int right = mid+1;//pointer 'current' is pointing at current index of array 'a'int current = start;while(left <= mid && right <= end){if(helper[left]<=helper[right]){a[current] = helper[left];left++;}else{a[current] = helper[right];right++;}current++;}//copy the remaining of left part into array 'a'while(left <= mid){a[current] = helper[left];current++;left++;}}

Let's talk about the merge process. At the end of the above, we only need to copy the rest of the left part to the original number group. Why? If the right half is left, it is already in the correct position of the original number group.

For example, if the left and right parts of an array such as merge are 2 3 7 | 1 8 9, the light is first iterated on the left, and 8 and 9 are left on the right, however, the location is already correct. For example, 1 8 9 | 2 3 7. At this time, the left side will be left, and iteration is required.

The video http://v.youku.com/v_show/id_XMzMyODk5Njg4.html of merge sort is given at last.

Interviews are rarely sorted by you directly. They are usually caught in some questions. I will update some questions later.

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.