Data Structure Tutorial 36th Lesson Select Sort, Merge sort

Source: Internet
Author: User

Teaching Purpose: Mastering the choice of sorting, merging the sorting algorithm

Teaching emphases: Choosing the sort of heap and merging sort algorithm

Teaching Difficulty: heap Sort algorithm

Teaching Content:

One, select the sort

Each trip in the n-i+1 (i=1,2,... n-1) record selects the record with the smallest keyword as the first record in the ordered sequence.

Second, the simple choice of sorting

Algorithm:

Smp_selecpass (ListType &r,int i)

{

K=i;

for (j=i+1;j<n;i++)

if (R[j].key<r[k].key)

K=j;

if (k!=i)

{t=r[i];r[i]=r[k];r[k]=t;}

}

Smp_sort (ListType &r)

{

for (i=1;i<n-1;i++)

Smp_selecpass (R,i);

}

Three, tree-type selection sorting

Also called tournament sort, the keywords for n records are compared to 22, and then 22 comparisons are made between half of the smaller ones, so repeat until a record of the minimum key is selected.

Iv. Heap Sequencing

Only one secondary space is required for the record size, and each record to be sorted occupies only one storage space.

What is a heap? The sequence of n elements {k1,k2,..., kn} is called a heap if and only if the following relationships are satisfied. Relationship One: ki<=k2i Relationship II: ki<=k2i+1 (i=1,2,..., N/2)

Heap sorting solves two problems: 1, how to build a heap from a unordered sequence? 2. How do I adjust the remaining elements to become a new heap after I output the top element of the heap?

Solution to question 2:


Sift (ListType &r,int k,int m)

{

I=k;j=2*i;x=r[k].key;finished=false;

T=R[K];

while ((j<=m) && (!finished))

{

if ((j<m) && (R[j].key>r[j+1].key)) J + +;

if (X<=r[j].key)

Finished:=true;

else {r[i]=r[j];i=j;j=2*i;}

}

r[i]=t;

}

Heapsort (ListType &r)

{

for (i=n/2;i>0;i--) sift (r,i,n);

for (i=n;i>1;i--) {

r[1]<->r[i];

Sift (r,i,i-1)

}

}

Five, merge sort

The method of combining two or more ordered tables into a new ordered table is called merge.

Assuming that the initial sequence contains n records, it is considered to be N ordered subsequence, with a length of 1 and then 22 merged to obtain an ordered sequence of N/2 with a length of 2 or 1, then 22 merge, so repeat.

Merge (ListType r,int l,int m,int n,listtype &r2)

{

P>I=L;J=M+1;K=L-1;

while (i<=m) and (j<n) do

{

k=k+1;

if (r[i].key<=r[j].key) {r2[k]=r[i];i++;}

Else {r2[i]=r[j];j++}

}

if (i<=m) r2[k+1..n]=r[i ... M];

if (j<=n) r2[k+1..n]=r[j ... n];

}

MergeSort (listtype &r,listtype &r1,int s,int t)

{

if (s==t)

R1[s]=r[s];

Else

{

MergeSort (R,R2,S,S+T/2);

MergeSort (r,r2,s+t/2+1,t);

Merge (R2,S,S+T/2,T,R1);

}

]

Vi. Summary

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.