Sorting Algorithm Summary

Source: Internet
Author: User

Sorting Algorithm

Overview

The purpose of sorting algorithms is to orderly a group of unordered elements. Sorting is usually divided into primary storage sorting and external sorting. The main storage sorting means that the data volume is not too large, and the entire sorting process is completed in the memory. Because the data volume is too large, the sorting cannot be completed in the primary storage, the sort that needs to be completed on the disk or tape.

Main storage sorting includes insert sorting (O (N2), Hill sorting (O (N2), Merge Sorting (O (NlogN), and heap sorting (O (NlogN )), fast sorting (O (NlogN), barrel sorting (O (N )).

Algorithm Analysis

In algorithm analysis, we will introduce the features and implementation principles of various sorting algorithms. You can search for the source code online or leave a message.

Insert sort

Insert sorting is composed of N-1 sort. For p = 1 to N-1, insert sort ensures that elements from position 0 to position p are sorted. When sorting k, elements from 0 to K-1 are sorted, and the k elements are compared with the K-1 to 0, until the k-th element is greater than the compared element, stop.

Hill sorting

Hill sorting works by comparing elements separated by a certain number. The comparison interval decreases with the algorithm until the last sorting of adjacent elements is compared. Hill sorting uses a sequence h1, h2 ...... As an incremental sequence and the number of elements at each comparison interval, the selection of this incremental sequence is very important and determines the computing time of the entire algorithm. One process of incremental sequence is to use the sequence recommended by Shell: ht = N/2 and hk = h (k + 1)/2. Pseudocode:

For from gap = length/2 to 1, the transformation factor is divided into 2

For I from gap to a. length, ++

For j: from I to gap, j-= gap

Tmp = a [I];

If a [j] is greater than a [I], a [I] = a [j];

A [j] = tmp;

Heap sorting

Heap sorting mainly utilizes the data structure of the priority queue. The steps include constructing a binary heap (cost O (N )), then, the root node is deleted N times (cost O (NlogN), and the deleted root node is stored in a new array (cost O (N )). One problem is that it uses an additional array. The key code is the above Code. Its pseudocode is as follows:

PerDown (a [], int I, int length)

Int temp = a [I];

A [I] = a [length];

Int child;

For (int j = I; 2 * child + 1

Child = 2 * I;

If (a [2 * I]> a [2 * I + 1])

Child ++;

If (a [I]> a [child])

{

A [I] = a [child]

} Else {

Break;

}

A [I] = temp;

}

Merge Sorting

The core of Merge Sorting is recursion, and it takes O (NlogN) to sort two sorted tables ). For example, tables 1 and 2 are sorted, and the sorting results of the two tables are stored in Table 3. The Code is as follows:

Public voidMergeSort (IntLeft,IntRight,Int[] Input,Int[] Tmp ){

If(Left> = right)

Return;

IntMiddle = (left + right)/2;

MergeSort (left, middle, input, tmp );

MergeSort (middle + 1, right, input, tmp );

}

Private void Merge (IntLeft,IntMiddle,IntEnd,Int[],Int[] Tmp){

IntRight = middle + 1;

IntTmpPos = left;

While(Left <= middle & right <= end ){

If(Tmp [left]> tmp [right]) {

Tmp [tmpPos ++] = tmp [right ++];

}Else{

Tmp [tmpPos ++] = tmp [left ++];

}

}

While(Left <= middle ){

Tmp [tmpPos ++] = a [left];

Left ++;

}

While(Right <= end ){

Tmp [tmpPos ++] = a [right];

Right ++;

}

For(IntI = 0; I

A [I] = tmp [I];

}

}

Quick sorting

The average running time of quick sorting is O (NlogN), which is also a recursive algorithm for grouping. The sorting process mainly includes three processes:

1. Select the pivot element as a key element of comparison;

2. The table is divided into two parts (A and B) based on the pivot element position. The elements smaller than the element are placed on the left, and those greater than the element are placed on the right;

3. Sort the parts A and B quickly.

The Code is as follows:

Public voidQuickSort (IntStart,IntEnd,Int[] Input ){

Media (start, end, input );

IntLeft = start;

IntRight = end-1;

While(Left <right ){

While(Input [left] <input [end]) {

Left ++;

}

While(Input [right]> input [end]) {

Right --;

}

IntTmp = input [left];

Input [left] = input [right];

Input [right] = tmp;

}

IntTmp = input [left + 1];

Input [left + 1] = input [end];

Input [end] = tmp;

QuickSort (start, left, input );

QuickSort (left + 1, end, input );

}

Private voidMedia (IntStart,IntEnd,Int[] Input ){

IntMiddle = (start + end)/2;

If(Input [start]> input [middle]) {

IntTmp = input [start];

Input [start] = input [middle];

Input [middle] = tmp;

}

If(Input [middle]> input [end]) {

IntTmp = input [middle];

Input [end] = input [middle];

Input [middle] = tmp;

If(Input [start]> input [middle]) {

Tmp = input [start];

Input [start] = input [middle];

Input [middle] = tmp;

}

}

IntTmp = input [middle];

Input [end] = input [middle];

Input [middle] = tmp;

}

Barrel sorting

The bucket sorting method calculates the total cost of O (N), which is equivalent to a time-space conversion process. It has certain restrictions on the sorted data and must be an integer element, input data A1, A2 ,...... The algorithm must be composed of only positive integers smaller than M. The algorithm is simple: Use a count data with a size of M, which is initialized to all 0. Therefore, it has M units or buckets. When Ai is read, count [Ai] = 1. After all input data is read, scan the Data count to print the sorted table.

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.