2016 Computer Postgraduate Examination: Analysis of common algorithms of data structure

Source: Internet
Author: User

Do not know the blog park there is no computer professional postgraduate examination party, hope to enumerate the computer examination site can help everyone, the following is the data structure commonly used algorithm analysis, if you see what is wrong, welcome error correction AH hahaha. 2016 Postgraduate!!!!!!!!!

Internal sorting (in-memory ordering does not require access to external memory) external ordering (very large, sorted by batch read-write external memory, final sort)

Stable and unstable sorting: see if the relative order of the same records changes back. The main look in the sorting process is not adjacent records, if the comparison is adjacent, it must be a stable sort. If it is not adjacent to the comparison, it is unstable.

Internal sorting method as of now, various internal sorting methods can be grouped into the following five categories:

(1) Insert sort; (2) Exchange sort; (3) Select Sort; (4) Merge sort; (5) Base order.

Insert sort: include direct insert sort and hill sort

Direct Insert sort: (Stable)

Algorithm description

void Insort (SqList &l) ∥ algorithm for sequential file F direct insertion sorting ∥

{int i,j;

for (i=2;i<=l.len;i++) ∥ inserts a n-1 record ∥

{

if (L.r[i].key

{L.r[0]=l.r[i];∥ The record to be inserted is first stored in the lookout ∥

L.R[I]=L.R[I-1];

for (j=i-2; L.r[0].key

L.R[J+1]=L.R[J]; ∥ record order and move back ∥

L.R[J+1]=L.R[0]; ∥ original r[i] Insert j+1 position ∥

}

}

}

Algorithm analysis

The key comparison number in the set sort is C,c min as cmin and the maximum is Cmax.

(1) When the original order (positive order), each insert a r[i] only need to compare key once, namely:

(2) When the original reverse order (key from large to small), each insert a r[i] and the child table i-1 a key comparison, plus the same as their own r[0] comparison, at this time the most, that is:

(3) record the total number of moves m (M min as mmin, Max Mmax)

In the positive order, the movement of the records in the child table is waived, i.e.:

In reverse order, inserting r[i] involves moving the entire child table. The number of moves is i-1 =i+1, when the table moves the most times, namely:

The time complexity of sorting takes the highest magnitude, so it is directly inserted into the ordered T (N) =o (n2).

Shell (Hill) sort is also known as "reduced increment" sort (unstable)

Sorting of Exchange classes: (Bubble sort and quick sort)

Bubble Sorting Algorithm Description

void Bubsort (SqList &l)

{int I,flag;∥flag is the token for the record interchange ∥

Retype temp;

for (i=l.len;i>=2;i--) ∥ up to n-1 sequencing ∥

{flag=0;//Record whether each trip has been exchanged

for (j=1;j<=i-1;j++) ∥ a trip to the bubbling sort ∥

if (L.r[j].key>l.r[j+1].key) ∥ 22 comparison ∥

{Temp=l.r[j];∥r[j]? R[j+1]∥

L.R[J]=L.R[J+1];

L.r[j+1]=temp;

flag=1;

}

if (flag==0) break; ∥ sorting is complete without record swapping ∥

}

}

Algorithm analysis

Set the length of the queue to N, the total key comparison in the algorithm is C. If the positive sequence, the first trip there is no record exchange, Exit cycle, Cmin=n-1=o (n); If reverse order, you need to n-1, the number of comparisons per key is I-1 (2≤i≤n), so:

Similarly, the maximum number of moves recorded is:

So the time complexity of the bubble sequencing t (n) =o (n2). And it's stable.

Quick sort: (unstable, time complexity O (NLOGN)), no auxiliary space required, but with best and worst points

Segmentation algorithm

int Partition (Sqlist&l,int low,int High)

{L.r[0]=l.r[low];

Pivotkey=l.r[low].key;

while (Low

{while (Low=pivotkey)

--high;

L.r[low]=l.r[high];

while (Low

++low;

L.r[high]= L.r[low];

}

return low;

}

Total Control function:

void QSort (Sqlist&l,int low,int High)

{if (Low

{

Pivotloc=partition (L,low,high);

QSort (l,low,pivotloc-1);

QSort (L,pivotloc+1,high);

}

}

Calling method: QSort (L,1,l,lenght);

Algorithm Analysis:

If the original or reverse order, after each call, the number of records reduced only one, so when T (n) =c (n+ (n-1) +......+1) =o (n2). This is the worst-case scenario for fast sorting. So the fast sorting algorithm needs to be improved.

Simple selection Sort: The time complexity of stable sequencing (O (n2))

Algorithm description

void Slectsort (sqlist& L) ∥ algorithm for direct selection sorting ∥

{

for (i=1;i

{

J=seloctminkey (L,i); Select the minimum key record from I to L.len

if (I!=J)

{Temp=l.r[i];

L.R[I]=L.R[J];

L.r[j]=temp;

}

}

}

Heap sort: Belongs to select sort of unstable, time complexity (O (NLOGN)), no best and worst difference, also need auxiliary stack space

If Ki≥k2i, ki≥k2i+1. At this point, the root node k1 the largest value, called the "big root heap."

If ki≤k2i, ki≤k2i+1 meet "" relationship, then K1 minimum, called "Small Gan".

In the heap sort, we use the big root heap, the reason is the big root heap, the root biggest, to delete very convenient, directly to it and the last leaf node exchange is possible.

Record key collection K={k1 k2......kn}, sorted in two steps:

(1) Build {K1 k2......kn} into a large heap;

(2) Take the root of the heap (key Max), and then adjust the remaining (n-1) key to the heap, then fetch the current heap

Root (key secondary), ... until all keys have been selected.

A heap tuning algorithm for an element:

Known h.r[s...m] except for h.r[s], which satisfies the definition of a heap, this function simply puts H.r[s] in the heap that is already a heap

void Heapadjust (sqlist& H, int s, int m) ∥ will (H.r[s] ... H.R[M]) adjusted to a large root heap ∥

{

Rc=h.r[s]; ∥ Temporary storage H.r[s]∥

for (j=2*s;j<=m; j*=2)//filter down on key-larger child nodes

{

if (Jl.r[j].key) j + +; ∥ J for S about child key Max number

if (Rc.key>l.r[j].key)

break;//Description H.r[s] is already larger than all the elements in the current heap, is already heap, does not need to adjust the

L.R[S]=L.R[J]; ∥ put the biggest one on the root node.

S=j; ∥ S and J Exchange, making s and lower heap comparison

}

L.R[S]=RC; ∥ Initial root regression ∥

}

void Heapsort (sqlist& H) ∥ heap sorting algorithm ∥

{

Initial build Heap

for (I=L.LEN/2; i>=1; i--)//starting from the last non-leaf node of the complete binary tree

Heapadjust (L,i,l.len); ∥ adjustment (R[i] ... R[n]) for heap ∥

Each time you remove the root (the largest element) so-called Take it off, just put it to the end of the array, change the lower bound of the array

for (i=l.len;i>=2;i--) ∥ a total of n-1 times ∥

{Temp=f.r[1];∥ root is swapped with the current last node ∥

F.r[1]=f.r[i];

F.r[i]=temp;

Heapadjust (L, 1,i-1); ∥ the last element to the appropriate position ∥

}

}

Two-Way merge sort: (Stable, Time complexity O (NLOGN) and stable and efficient, but need auxiliary space TR[1....N]

The core operation of the two-way merging is to merge the two ordered sequences of the first-dimension array into an ordered sequence.

(Note that this is the one we often test in the Linetype table, and two ordered lists are synthesized into a new ordered table)

The algorithm is described as follows:

void Merge (Rcdtype sr[],rcdtype&tr[],int i,int m,int N)

Merge ordered SR[I...M] and SR[M+1....N] into ordered TR[I....N]

{

for (j=m+1,k=i; i<=m&&j<=n; ++k)//Who first put the TR in the small

{if (Sr[i].key<=sr[j].key)

Tr[k]=sr[i++];

Else

Tr[k]=sr[j++];

}

if (i<=m)//Put the remaining SR directly into the TR

TR[K....N]=SR[I...M];

if (j<=n)//Put the remaining SR directly into the TR

TR[K...N]=SR[J....N];

}

void Msort (Rcdtype sr[], rcdtype&tr1[], int s, int t)

{//sr[s...t] Merge sort to tr1[s...t]

if (s= =t)//Only one element

Tr1[s]=sr[s];

Else

{

M= (s+t)/2;//divides sr[] into two halves

Msort (SR, TR2, S, m);

Msort (SR, TR2, m+1, T);

Merge (TR2, TR1, S, M, T);

}

More Review Data summary:

<p><a href= "http://www.kyjxy.com/yingyu/zhenti/" > Postgraduate English title </A></P>
<p><a href= "http://www.kyjxy.com/zhuanshuo/" > The difference between Master and Shuo </A></P>
<p><a href= "http://www.kyjxy.com/shuxue/ziliao/" > Postgraduate Math Review Program </A></P>

2016 Computer Postgraduate Examination: Analysis of common algorithms of data structure

Related Article

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.