Summary P2 of sorting algorithm under comparison model

Source: Internet
Author: User

Merge sort:

Thought: If there is only one element, the sort ends. Otherwise, the first and second halves are recursively sorted, and then the two parts that are sorted are merged together. The method of merging is to take the first and second half of the sort as two input arrays A and B, a temporary output array C, and three counter AP,BP,CP and initialize it to the beginning of the corresponding array. A[AP] and B[BP] are copied to the next position in C, and the relevant counter advances one step forward. When one of the two input arrays is exhausted, copy the remainder of the other array to C. Finally, the array C elements are copied back to the corresponding position of the original array.

First look at the code of the merge algorithm:

voidMergeintA[],intT[],intL_pos,intR_pos,intr_end) {    inti, L_end, num, t_pos; L_end= R_pos-1; T_pos=L_pos; Num= R_end-l_pos +1;  while(L_pos <= L_end&&r_pos <=r_end)if(A[l_pos] <=A[r_pos]) T[t_pos+ +] = a[l_pos++]; ElseT[t_pos+ +] = a[r_pos++];  while(L_pos <=l_end) T[t_pos+ +] = a[l_pos++];  while(R_pos <=r_end) T[t_pos+ +] = a[r_pos++];  for(i =0; i < num; i++, r_end--) A[r_end]=t[r_end];}
voidM_sort (intA[],intT[],intLeftintRight ) {    intCenter; if(Left <Right ) {Center= (left + right)/2;        M_sort (A, T, left, center); M_sort (A, T, center+1, right); Merge (A, T, left, center+1, right); } }voidMerge_sort (intA[],intN) {    int*T; T= (int*)malloc(n *sizeof(int)); if(t! =NULL) {M_sort (A, T,0N1);  Free(t); }}

The merge sort algorithm above looks a bit complicated, and also points out two functions of M_sort and Merge_sort, which is for the purpose of optimizing the algorithm. A temporary array is used for each merge, and if each call to the merge function allocates and releases an array dynamically, it takes a lot of time for malloc to take up.

To analyze the merge sort algorithm, we set the time t (N) in the worst case for the merging of n elements, observe m_sort, because the time used for the merge is O (n), and in the worst case n we get a recursive formula about T (N):

T (1) =1 (spend constant time, we record it as 1, and remember it as theta (1) can also)

T (N) =2t (N/2) +n

The recursive formula can be solved by: T (n) =θ (NLOGN)

In the worst case, the run time is θ (NLOGN) and the run time of the merge algorithm is O (NLOGN). Although it runs at O (NLOGN), it requires linear additional memory at the time of merging, and many copies of the data throughout the algorithm, which seriously affect the speed of sequencing. Therefore, a quick sort is a better choice for important internal sorting.

The time is late, occupy the pit to fill tomorrow.

Summary P2 of sorting algorithm under comparison model

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.