Nine kinds of sorting algorithms finishing

0.5 Comparison

Algorithm name time complexity stability
A counting sequencing \ (O (N) \)
2 Select Sort \ (O (N ^ 2) \) ×
3 bubble sort Worst \ (O (N ^ 2) \) , preferably \ (O (N) \)
4 Insertion Sort Average \ (O (N ^ 2) \) , preferably \ (O (N) \)
5 radix sort \ (H (referred to as \ times N) \)
6 merge sort \ (O (NlogN) \)
7 Quick Sort Average \ (O (NlogN) \) , the worst \ (O (N ^ 2) \) ×
Sort 8 Hill Dispute, mean \ (O (NlogN) \) , the lower limit \ (O (N ^ {1.5 }) \) ×
9 heapsort \ (O (NlogN) \) ×

A counting sort

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;//元素最大值 
int a[maxn+5];
int main(){
    int n;scanf("%d",&n);
    for(int i=1,x;i<=n;i++)scanf("%d",&x),a[x]++;
    for(int i=1;i<=maxn;i++)for(int j=1;j<=a[i];j++)printf("%d ",i);
    return 0;
}

Algorithm Description: all thrown into the bucket, pour out the last one.

Other: limitations, the size of the array elements limit.

The least complex sorting algorithm \ (O (n-m +) \) (n-number of elements, m is bounded on the element size), stabilized.

Second, the selection sort

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;//元素个数 
int a[maxn+5];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++){
        int k=i;
        for(int j=i+1;j<=n;j++)if(a[j]<a[k])k=j;
        if(k!=i)swap(a[i],a[k]);
    }
    for(int i=1;i<=n;i++)printf("%d ",a[i]);
}

Algorithm Description: each front discharge to find the minimum.

\ (O (the n-^ 2) \) , unstable.

Third, the bubble sort

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int a[maxn+5];
int main(){
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<n;i++){
        int f=0;
        for(int j=1;j<=n-i;j++){
            if(a[j]>a[j+1])swap(a[j],a[j+1]),f=1;
        }
        if(!f)break;
    }
    for(int i=1;i<=n;i++)printf("%d ",a[i]);
}

Algorithm Description: Every compare adjacent elements. High priority element (large bubbles) is gradually increased.

Other: Bubble Sort exchange for the number of reverse order of numbers.

Worst \ (O (^ n-2) \) , preferably when the \ (O (n-) \) .

Stable sort.

Fourth, insertion sort

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int a[maxn+5];
int main(){
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++){
        int x=a[i],j=i-1;
        for(;j>0&&x<a[j];j--)a[j+1]=a[j];
        a[j+1]=x; 
    }
    for(int i=1;i<=n;i++)printf("%d ",a[i]);
}

Sort algorithm: to maintain an orderly prefix, the current element inserted front.

Average \ (O (^ n-2) \) , preferably \ (O (n-) \) .

stable.

Fifth, radix sort

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int s[10][maxn+5],a[maxn+5];
int main(){
    int n;scanf("%d",&n);
    for(int i=0;i<n;i++)scanf("%d",&a[i]);
    int len[10],d=1;
    for(int i=0;i<10;i++){
        memset(len,0,sizeof(len));
        for(int j=0;j<n;j++){
            int k=a[j]/d%10;//求第i位上的数值 
            s[k][len[k]++]=a[j];
        }
        int m=0;
        for(int j=0;j<10;j++)for(int k=0;k<len[j];k++){
            a[m++]=s[j][k];
        }
        d*=10;
    }
    for(int i=0;i<n;i++)printf("%d ",a[i]);
}

Algorithm Description: from the lowest Start, once sorted.

Other: Sorting the premise that all the elements are non-negative . If there is a negative number, by adding a large value are converted to non-negative sequence elements.

\ (O (n-\ CDOT m) \) , (m is the maximum length in bits).

stable.

Sixth, merge sort

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int a[maxn+5],b[maxn+5],cnt=0;
void merge(int l,int r){
    if(l==r)return;
    int mid=l+r>>1;
    merge(l,mid);merge(mid+1,r);
    int i=l,j=mid+1,k=l;
    while(i<=mid&&j<=r){
        if(a[i]<=a[j])b[k++]=a[i++];
        else{
            b[k++]=a[j++];
            cnt+=mid-i+1;//统计逆序对数 
        }
    }
    while(i<=mid)b[k++]=a[i++];
    while(j<=r)b[k++]=a[j++];
    
    for(int o=l;o<=r;o++)a[o]=b[o]; 
}
int main(){
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    merge(1,n);
    printf("%d\n",cnt);
    for(int i=1;i<=n;i++)printf("%d ",a[i]);
}

Algorithm Description: divide and conquer, will merge the two ordered sequence.

Other: may \ (O (NlogN) \) to give the number of reverse time.

\ (O (nlogn) \) . When merging two lengths are ordered array of n up to the number of comparisons \ (. 1-2N \) (preliminary NOIP2017).

stable.

Seven, quick sort

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int a[maxn+5],n;
void Qsort(int l,int r){
    if(l>=r)return;
    int i=l,j=r,mid=a[(l+r)>>1];
    while(i<=j){
        while(a[i]<mid)i++;
        while(a[j]>mid)j--;
        if(i<=j)swap(a[i++],a[j--]);
    }
    Qsort(l,j),Qsort(i,r);
}
int main(){
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    Qsort(1,n);
    for(int i=1;i<=n;i++)printf("%d%c",a[i],i==n?'\n':' ');
}

Algorithm Description: is selected from a group amount, the release element is larger than his back, he is less than the front discharge element, recursively two subintervals.

Other: Improved bubble sort.

The average time complexity is \ (O (nlogn) \) , the worst case \ (O (^ n-2) \) .

Instability.

Eight, Hill sorting

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
int a[maxn+5];
int main(){
    int n;scanf("%d",&n);
    for(int i=0;i<n;i++)scanf("%d",&a[i]);
    for(int step=n/2;step>=1;step/=2){//下标增量
        for(int i=step;i<n;i++)if(a[i]<a[i-step]){
            int k=i-step,tmp=a[i];
            while(k>=0&&tmp<a[k]){
                a[k+step]=a[k];
                k-=step;
            }
            a[k+step]=tmp;
        }
    }
    for(int i=0;i<n;i++)printf("%d%c",a[i],i==n?'\n':' ');
}

Algorithm Description: pressing the target packet in increments, each direct insertion sort of sorting algorithms; With increment is gradually reduced, more and more keywords each comprising, when reduced to an increment, sorting is completed.

Other: insertion sort improved version.

Controversy exists complexity, lower limit \ (O (nlogn) \) , the mean \ (O (n-^ {for 1.5}) \) .

Instability.

Nine, heap sort

#include<bits/stdc++.h>
using namespace std;
const int maxn=1000;
struct Heap{
    int a[maxn+5],sz;
    void init(int n){
        sz=n;
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    }
    /*void up(int x){
        while(x>1){
            int fa=x/2;
            if(a[x]<a[fa])swap(a[x],a[fa]);
            else return;
            x=fa;
        }
    }*/
    void down(int x){//维护小根堆 
        while(2*x<=sz){
            int son=x<<1;
            if(son+1<=sz&&a[son]>a[son+1])son|=1;//找较小的儿子
            if(a[x]>a[son])swap(a[x],a[son]);
            else return;
            x=son;
        }
    }
}heap;

int main(){
    int n;scanf("%d",&n);
    heap.init(n);   
    for(int i=n/2;i>=1;i--)heap.down(i);//对于非叶子节点 
    for(int i=1;i<=n;i++){
        printf("%d ",heap.a[1]);
        heap.a[1]=heap.a[heap.sz--];
        heap.down(1);
    }
}

Algorithm Description: using the stack data structure.

Each upward and downward operations are logN, time complexity is \ (O (nlogn) \) .

Instability.

Supplementary blog

Blog 1

Guess you like

Origin www.cnblogs.com/Tieechal/p/11567885.html