Temporal complexity (time complexity):
The maximum number of times an expression in a total operand is affected by the change in n (without coefficients)(note: If the number of statements executed in the algorithm is a constant, the time complexity is O (1))
If T (n)/f (n) the limit can be obtained a constant C, then the time complexity t (n) =o (f (n)).
As the module n increases, the growth rate of the algorithm execution is proportional to the growth rate of f (n), so the smaller the F (n), the lower the complexity of the algorithm, the higher the efficiency of the algorithm.
Spatial complexity (space complexity):
A measure of how much storage space is temporarily occupied by an algorithm (including the space occupied by the program code , the space occupied by the input data , and the space occupied by the auxiliary variable ) in the three aspects of the operation. )
Note:
- If the spatial complexity of an algorithm is a constant, that is, it does not change with the size of the amount of data being processed, it can be represented as O (1);
- When the spatial complexity of an algorithm is proportional to the logarithm of the base N of 2, it can be represented as 0 (log2n);
- When an algorithm's empty I-division complexity is linearly proportional to n, it can be represented as 0 (n).
Bubble sort
Select sort
Step: (Ascending)
The smallest number in the array is first found, with the first digit exchange position of the array (if the minimum number is itself exchanged with itself). Then find the second smallest number in the remaining number, swap the position with the second digit of the array (if the second decimal is itself exchanged with itself) .... Follow the same steps to execute the last element in turn.
Data exchange N times, data comparison (N-1) + (N-2) +........+3+2+1=n* (1+n-1)/2=N^2/2 times (last element exchanged with itself)
Complexity of Time: O (n2)
Space complexity: O (1)
1#include <stdio.h>2#include <malloc.h>3 intMain ()4 {5 intn,i,j,min,temp;6scanf"%d",&n);7 int*a= (int*)malloc(sizeof(int)*n);8 for(i=0; i<n;i++)9scanf"%d",&a[i]);Ten for(i=0; i<n;i++) One { AMin=a[i];//Minimum number -Temp=i;//The position of the minimum number in the array - for(j=i+1; j<n;j++) the { - if(Min>a[j])//Compare Size - { -min=A[j]; +temp=J; - } + } AA[temp]=a[i];//Exchange ata[i]=min; - } - for(i=0; i<n;i++) -printf"%d", A[i]); - return 0; -}
- The run time is independent of the input, and the ordered array input is the same as the unordered array of the same length.
- data movement is minimal , only two arrays are moved at a time
Insert Sort
Direct Insert Sort
Step: (Ascending)
Divides the original array into two groups, one for the ordered array A, and the other for the random ordinal Group B. Array a reverse (a[length-1]) each element is compared to an element of array B (starting from b[0], if an element of group A is larger than the Group B element, a group of elements back one is, B group elements make room, then Group B pushes the next element.
Complexity of Time: O (n2)
Space complexity: O (1)
1#include <stdio.h>2#include <malloc.h>3 voidSortint*a,intn);4 intMain ()5 {6 intn,i;7scanf"%d",&n);8 int*a= (int*)malloc(sizeof(int)*n);9 for(i=0; i<n;i++)Tenscanf"%d",&a[i]); One sort (a,n); A for(i=1; i<n;i++) -printf"%d", A[i]); the return 0; - } - intLessintAintb) - { + if(A<B)return 1; - return 0; + } A voidSortint*a,intN) at { - inti,j,temp,k; - for(i=1; i<n;i++) - { -temp=A[i]; - for(j=i-1; j>=0&&less (Temp,a[j]); j--) in { -a[j+1]=A[j]; to } +a[j+1]=temp; - } the}
- The time it takes to insert a sort depends on the order of the input elements
- Insert sort is more suitable for partially ordered arrays, and small-scale arrays
Hill sort
Step: (Ascending)
To speed up the insertion sort, the interchange of nonadjacent elements changes the array parts to order. Makes the array interval h (h changes) an element in order. h before changing, the program makes the fractional group of H elements spaced in order.
Example: 2-6-70 (decimal Group Each element interval h elements) ...
Complexity of Time:
Space complexity: O (1)
#include <stdio.h>#include<malloc.h>//int less (int a[],int i,int j) { if(a[i]<a[j]) return 1; return 0 ;}
intMain () {intn,h,i,j,temp,k; scanf ("%d",&N); int*a= (int*)malloc(sizeof(int)*N); for(i=0; i<n;i++) scanf ("%d",&A[i]); H=1; while(h<n/3) H=3*h+1; while(h>=1) { for(i=h;i<n;i++) { for(J=i;j>=h&&less (a,j,j-h); j-=h) {Temp=A[j]; A[J]=a[j-h]; A[j-h]=temp; }} H/=3; } for(i=0; i<n;i++) printf ("%d", A[i]); return 0; }
question 1: A decimal group such as 6-4-5 appears, sorted or unordered decimal group?
A: There is no 6-4-5 such a decimal group, because:
J=h is a 6-4 decimal group that is reordered to an ordered 4-6 decimal group
J=2H is a 4-6-5 decimal group that is reordered to an ordered 4-5-6 decimal group
Question 2: How to choose the increment number of H series?
The 3*h+1 used in the book is similar to the performance of the complex increment sequence, but it is relatively simple, so the increment series is presented as a reference in the book.
No optimal increment sequence found
Advantage:
Hill sort is much faster than insert sort and select sort, and the larger the array, the greater the advantage.
Reference: The fourth edition of the algorithm
How to calculate time/space complexity: http://www.cnblogs.com/zakers/archive/2015/09/14/4808821.html
Algorithm (fourth edition) Learning Note (ii)--Primary sorting algorithm