Data Structure-search and sorting algorithms

Source: Internet
Author: User
How many types of queries are available?

/*

1. Sequential search
2. Half-Lookup
3. multipart search

*/

Half-lookup Method
/* 1. the premise is that sequential storage is required. 2. sorted by keyword size */INT binsearch (recordlist L, keytype K) {int low = 1, high = L. length; while (low <= high) {mid = (low + high)/2; If (k = L. R [Mid]. key) return mid; else if (k <L. R [Mid]. key) High = mid-1; elselow = Mid + 1;} return-1 ;}
Tree-based search

/*
The binary sorting tree, also known as the binary search tree, is a special binary tree.
Insert and create a binary sorting tree (algorithm)
1. If the binary tree is an empty tree, s becomes the root of the binary tree.
2. If the binary tree is not empty, the S. Key is compared with the key word of the binary sorting tree root node.
2.1 If the key value is equal to the value of the root node, stop inserting
2.2 If the key value is smaller than the root node value, s inserts the left subtree
2.3 if the key value is greater than the root node value, s inserts the right subtree
*/
Recursive Algorithms for binary tree search

BSTree SearchBST(BSTree bst,KeyType key){if(!bst)return bst;else if(bst->key==key)return bst;else if(bst->key>key)return SearchBST(bst->lchild,key);else return SearchBST(bst->rchild,key);}
Non-recursive algorithm of binary sorting tree
BSTree SearchBST(BSTree bst,KeyType key){BSTree q;q=bst;while(q){if(q->key ==  key)return q;if(q->key > key)q=q->lChild;elseq=q->rChild;}return NULL;}
Stability and instability

/*
If the two numbers are sorted to the last order regardless of how they are placed, this algorithm is called stable.
If the two numbers change after sorting, this algorithm is called unstable.
*/
Sort algorithms insert sorting directly

Void insertsort (recordtype R [], int length) {for (INT I = 2; I <= length; I ++) {R [0] = R [I]; // number to insert, R [0] is monitoring whistle J = I-1; while (R [0]. key <R [J]. key) {R [J + 1] = R [J]; j --;}}}
Semi-insert sorting
Void binsort (recordtype R [], int length) {for (INT I = 2; I <= length; I ++) {x = R [I]; Low = 1; high = I-1; while (low <= high) {// determine the insert position mid = (low + high)/2; If (X. key <R [Mid]. key) High = mid-1; elselow = Mid + 1;} // move position for (j = I-1; j> = low; -- J) {R [J + 1] = R [J];} R [low] = x ;}}
Sort by hill to narrow down incremental sorting

/*
Algorithm IDEA
The optimal nature of direct insertion sorting is used: the time complexity can be increased to O (n) when ordering );
1. Divide the sequence of keywords to be sorted into several smaller sub-sequences, and insert and sort the sub-sequences directly, so that the sorting is sorted properly.
2. Sort insert directly
*/
The bubble sort of the exchange class. Two Adjacent comparisons are made, and then the largest (small) bubbles to the last

Void BubbleSort(RecordType r[],int length){n=length;change=true;for(int i=1;i<n-1&&change;++i){change=false;for(int j=1;j<n-i;++j){if(r[j].key>r[j+1].key){x=r[j];r[j]=r[j+1];r[j+1]=x;change=true;}}}}
Fast Sorting Algorithm for exchange sort
Void qksort (recordtype R [], int low, int high) {If (low Select a class Sorting Algorithm (Simple selection and sorting)
Void simplesort (recordtype R [], int length) {n = length; For (INT I = 0; I <length; I ++) {for (j = I + 1; j <length; j ++) {If (R [J]> r [I]) {// The exchange class code can all be a = A + B; B = A-B; A = A-B; X = R [I]; R [I] = R [J]; R [J] = x ;}}}}
Merge Sorting

// 2-way recursive Merge Sorting Algorithm

Void msort (recordtype R1 [], int low, int high, recordtype R3 []) {/* R1 sort the order and put it in R3, r2 is a secondary space */recordtype R2 [N]; If (Low = high) {R3 [low] = R1 [low];} else {mid = (low + high)/2; msort (R1, low, mid, R2); msort (R1, mid, high, R2); merge (R2, low, mid, high, R3) ;}} void Merge (recordtype R2 [], int low, int mid, int high, recordtype R3 []) {// merge two ordered arrays, merge to R3 I = low; j = Mid + 1; k = low; while (I <= mid) & (j <= high )) {If (R2 [I] <R2 [J]) {R3 [k] = R2 [I]; I ++ ;} else {R3 [k] = R2 [J]; j ++;} k ++;} // configure the remaining data of the array while (I <= mid) {R3 [k] = R2 [I]; k ++; I ++;} while (j 

9.7 comprehensive comparison of various sorting methods

First, we compare the algorithm's average time complexity, worst time complexity, and the auxiliary storage space required by the algorithm, as shown in table 9-1. The simple sorting includes insertion sorting, Bubble sorting, and simple selection sorting except the hill sorting.

 

Table 9-1 Performance Comparison of various sorting methods

Sorting Method

Average time complexity

Worst time complexity

Auxiliary storage space

Simple sorting

O (n2)

O (n2)

O (1)

Quick sorting

O (nlogn)

O (n2)

O (logn)

Heap sorting

O (nlogn)

O (nlogn)

O (1)

Merge Sorting

O (nlogn)

O (nlogn)

O (N)

Base sort

O (D (n + rd ))

O (D (n + rd ))

O (RD)

 

    

The following conclusions can be drawn from the comprehensive analysis and comparison of various sorting methods:

L simple sorting is generally used only when n is small. When the record in a sequence is "basic order", direct insertion of sorting is the best sorting method, which is often used in combination with other sorting methods such as quick sorting and merge sorting.

L The average time complexity of fast sorting, heap sorting, and Merge Sorting is O (nlogn), but the experimental results show that fast sorting is the best in all sorting methods in terms of average time performance. Unfortunately, the time in the worst case of quick sorting can be O (n2 ). The worst time complexity of heap sorting and Merge Sorting is still O (nlogn). When n is large, the time performance of Merge Sorting is better than that of heap sorting, but it requires the most auxiliary space.

L the time complexity of base sorting can be written as O (D * n ). Therefore, it is most suitable for a sequence with a large N value and a small number of digits D of the keyword.

L in terms of sorting stability, the base sorting is stable. In addition to simple sorting, other simple sorting methods are also stable. However, fast sorting, heap sorting, Hill sorting, and other sorting methods with better time performance, as well as simple selection sorting are unstable. In most cases, sorting is based on the record's primary keywords. In this case, you do not need to consider the stability of the sorting method. If sorting is based on the record's secondary keyword, the stability of the sorting method should be fully considered.

In summary, each sorting method has its own characteristics. No method is absolutely optimal. We should select an appropriate sorting method based on the actual situation, or we can combine multiple methods for use.

 

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.