Each time sorting algorithm complexity analysis and presentation

1. Overview of the sorting algorithm

By any key comparison algorithm, the time complexity is not better than nlgn. Assign a rank not key comparison algorithm.
Here Insert Picture Description

2. exchange sorting (bubble sort)

Algorithms Introduction

The so-called switching, the sequence is based on the comparison result recorded in two keys to swap the two recorded positions in the sequence, exchange sorting characteristic is: the key-aft movement of a large recording sequence, the more key small recording is moved to the front portion of the sequence.

void swapsort(int a[])
{
for(int i=1; i<=n; i++){
for(intj=i+1; j<=n; j++){
if(a[i]>a[j]){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}

time complexity

For-i executed n times, in the first pass for-i, for-j executed n-1 times, during the second pass for-i, for-j performs n-2 times, ..., and finally in the for-i when again, for-j 0 execution times.
Therefore, T (n) = (n- 1) + (n-2) + ... + 1 + 0 = (n-1) * n / 2.

3. Merge Sort

Algorithm introduced
i.e. the sequence to be sorted into several subsequences, each subsequence is ordered. And then ordered the whole sub-sequences into an ordered sequence.
Here Insert Picture Description
Time complexity
for the best, worst and average case, the problem will have to break down the scale of up to 1, so the same time complexity of the three.
   T (n) = 2T (n / 2) + θ (n) n> 1 when
   T (n) = θ (1 ) n = 1
analyzed by Videos recursive tree apparent T (n) = cnlgn + cn , time complexity It is the degree of θ (nlgn), of course, also o (nlgn).

4. Quick Sort

Algorithms Introduction

Quick sort proposed by CAR Hoare in 1960. The basic idea is: a trip by ordering the data to be sorted into two independent parts, the part where all the data than the other part of all data to be small, then this method of data for the two parts separately fast sorting, sorting the entire process can be recursively, in order to achieve the whole data into an ordered sequence.

The insertion sort

Algorithm introduced
insertion sort is a simple insertion sort, the basic idea is: the record to be sorted according to their size by one key value is inserted into an already sorted ordered sequence, until all records into finished So far, get a new ordered sequence.

Hill sorting (packet insertion)

Shell sort is the subject of pressing the recording packet in increments, the use of direct insertion sort each sorting algorithm; With increment is gradually reduced, more and more keywords each comprising, when reduced to an increment, the entire file is divided into just one set, the algorithm will terminate. The method proposed in 1959 due to DLShell named.
This method is essentially a packet insertion method.

6. Selection Sort

Algorithm introduced
first data element to be selected from the sorted minimum (or maximum) of an element stored in the starting position of the sequence, and then to find from the remaining unsorted elements to a minimum (large) elements, and then put to the end of the sequence sorted. And so on, until all the number of data elements to be sorted is zero
FUNC SelectionSort (the nums [] Int32) {
length: = len (the nums)
for I: = 0; I <length; I ++ {
min: = I
for J : = I +. 1; J <length; J ++ {
IF the nums [J] <the nums [min] {
min = J
}
}
TEMP: = the nums [I]
the nums [I] = the nums [min]
the nums [min] = TEMP
}
fmt.Println (the nums)
}

Time complexity
since only the bottom one exchange for-i, n-1 so that total switching times, and each needs to exchange 3 times assigned so that in all cases (worst) time complexity is 3 (n-1).

7 heap sort

Algorithm introduced
HEAPSORT (English: Heapsort) refers to a sorting algorithm such a data structure designed for use heap. A stack is nearly complete binary tree structure, properties and satisfy deposited: key or index that is a child node is always less than (or greater than) its parent node.

Void heapsort(int n ,heap&H){
Makeheap(n,H);
Removekeys(n,H,H.S);
}

In fact, the algorithm is the constant construction heap, and then remove the root node. Every time the removal of the root node is the maximum, so the equivalent of completed sequencing. The following figure is an example.
Here Insert Picture Description

8. assign a rank (radix sort)

Algorithm introduced
not compare key class algorithms.

For example, the following numbers are base 10, the first row of the left side first, into a different pile; then ranked second on the left, and finally ranked third on the left. So after checking the median is the number of columns and orderly. If a waiting is also possible from the far right, similar to the truth.

Here Insert Picture Description

Published 21 original articles · won praise 18 · views 1455

Guess you like

Origin blog.csdn.net/zephyr_wang/article/details/104264554