排序(插入排序,希爾排序,歸併排序,快速排序)

1.插入排序:每一步將一個待排序的元素按照其關鍵字值的大小插入到已排序序列的適當位置,知道待排序元素插入完爲止。

(1).核心代碼:

void sort(int a[],int n)//插入排序 
{
    int i,j;
    int temp;
    for(i = 0;i<n;i++)
    {
        int j = i;
        int temp = a[i];
        while(j>0 && temp < a[j-1])//逐個比較,temp>=a[j-1]時,j便是應該插入的位置 
        {
            a[j] = a[j-1];//往後移,找到插入位置時可以馬上插入 
            j--;
        }
        a[j] = temp;
    }
} 

(2).例題:將序列 3,1,4,1,5,9,2,6,5用插入排序排位升序列。

#include <stdio.h>
void sort(int a[],int n)//插入排序 
{
    int i,j;
    int temp;
    for(i = 0;i<n;i++)
    {
        int j = i;
        int temp = a[i];
        while(j>0 && temp < a[j-1])
        //逐個比較,temp>=a[j-1]時,j便是應該插入的位置 
        {
            a[j] = a[j-1];//往後移,找到插入位置時可以馬上插入 
            j--;
        }
        a[j] = temp;
    }
} 
int main()
{
    int a[9] = { 3,1,4,1,5,9,2,6,5};
    sort(a,9);
    for(int i = 0;i<9;i++)
    {
        printf("%d\t",a[i]);
        }   
} 

運行結果:
這裏寫圖片描述
2.簡單排序算法的下界:
(1、插入排序的運行時間時O(I+N),其中I爲原始數組中的逆序數。
(2、N個互異的數的數組平均逆序數時N(N-1)/4。
(3、通過交換相鄰元素進行排序的任何算法平均需要 歐(N平方)時間

3.希爾排序:既有插入排序的簡單的優點,也克服了只能交換相鄰元素的缺點。
(1.主要代碼:

void shell_sort(int a[],int n)
{
    int i,j,gap;
    for(gap = n/2;gap > 0; gap /= 2)
    {
        for(i = 0;i < gap;i++)
        {
            for(j = i;j < n;j += gap)
            {
                if(a[j] < a[j-gap])//如果逆序,則尋找插入的位置 
                {
                    int temp = a[j];
                    int k = j-gap;
                    while(k>=0 && a[k] > temp)
                    {
                        a[k+gap] = a[k];
                        k -= gap;
                    }
                    a[k+gap] = temp;
                }
            }
        }
    }
}

(2.完整代碼:

#include <stdio.h>
void shell_sort(int a[],int n)
{
    int i,j,gap;
    for(gap = n/2;gap > 0; gap /= 2)
    {
        for(i = 0;i < gap;i++)
        {
            for(j = i;j < n;j += gap)
            {
                if(a[j] < a[j-gap])//如果逆序,則尋找插入的位置 
                {
                    int temp = a[j];
                    int k = j-gap;
                    while(k>=0 && a[k] > temp)
                    {
                        a[k+gap] = a[k];
                        k -= gap;
                    }
                    a[k+gap] = temp;
                }
            }
        }
    }
}
int main()
{
    int a[5] = {2,4,5,1,3};
    shell_sort(a,5);
    for(int i = 0;i<5;i++)
    {
        printf("%d\t",a[i]);
    }
}

運行結果:這裏寫圖片描述

4.歸併排序:將兩個子序列歸併爲有序的序列,運用了分而治之、遞歸的思想,排序算法穩定。
(1.主要代碼:

void merge(int a[],int temp[],int l,int r,int rightend)
{
    int leftend = r-1;//leftend是左邊的終點,r是右邊的起點,假設兩邊是挨着的
    int temp1 = l;
    int num = rightend-l+1;
    while(l <= leftend && r <= rightend) 
    {
        if(a[l] < a[r])
        temp[temp1++] = a[l++];
        else
        temp[temp1++] = a[r++];
    }
    while(l <= leftend)
    temp[temp1++] = a[l++];
    while(r <= rightend)
    temp[temp1++] = a[r++];//實際情況中只有一個while滿足 
    for(int i = 0;i<num;i++,rightend--)
    {
        a[rightend] = temp[rightend];
    }    
}
void mysort(int a[],int temp[],int l,int rightend)
{
    int center;
    if(l < rightend) 
    {
        center = (l+rightend)/2;
        mysort(a,temp,l,center);
        mysort(a,temp,center+1,rightend);//遞歸排序 

        merge(a,temp,l,center+1,rightend);//將兩個有序空間排序成一個有序空間 
    }
}

(2.完整代碼:

#include <stdio.h>
void merge(int a[],int temp[],int l,int r,int rightend)
{
    int leftend = r-1;//leftend是左邊的終點,r是右邊的起點,假設兩邊是挨着的
    int temp1 = l;
    int num = rightend-l+1;
    while(l <= leftend && r <= rightend) 
    {
        if(a[l] < a[r])
        temp[temp1++] = a[l++];
        else
        temp[temp1++] = a[r++];
    }
    while(l <= leftend)
    temp[temp1++] = a[l++];
    while(r <= rightend)
    temp[temp1++] = a[r++];//實際情況中只有一個while滿足 
    for(int i = 0;i<num;i++,rightend--)
    {
        a[rightend] = temp[rightend];
    }    
}
void mysort(int a[],int temp[],int l,int rightend)
{
    int center;
    if(l < rightend) 
    {
        center = (l+rightend)/2;
        mysort(a,temp,l,center);
        mysort(a,temp,center+1,rightend);//遞歸排序 

        merge(a,temp,l,center+1,rightend);//將兩個有序空間排序成一個有序空間 
    }
}
int main()
{
    int a[5] = {23,3,1,2,4};
    int temp[5];
    mysort(a,temp,0,4);
    for(int i = 0;i<5;i++)
    {
        printf("%d\t",a[i]);
    }
}

運行結果:
這裏寫圖片描述

5.快速排序:快速排序也是一種分值的遞歸算法,對於規模較小的排序,應採用簡單排序(如:插入、冒泡排序等),對於大規模的使用快速排序更好。
(1、主元的選取:選擇合適的主元,會使代碼更加優化,更快

//選主元 
int median(int a[],int left,int right)
{
    int center = (left+right)/2;
    if(a[left] > a[right])
    swap(&a[left],&a[right]);
    if(a[left] > a[center])
    swap(&a[left],&a[center]);
    if(a[right] < a[center])
    swap(&a[right],&a[center]);

    swap(&a[center],&a[right-1]);
    //將主元元放到a[right-1],只考慮a[left+1]到a[right-2]; 
    return a[right-1];
}

(2、核心代碼:

void quick_sort(int a[],int left,int right)
{
    if(left<right)//如果right>=left,則停止遞歸 
    {

        int med = median(a,left,right);
        int i = left, j = right - 1;
        for( ; ; ) 
        {
            while ( a[ ++i ] < med ) { }
            while ( a[ --j ] > med ) { }
            if ( i < j )
            swap( &a[i], &a[j] );
            else break;
        }
        swap( &a[i], &a[ right-1 ] );
        quick_sort( a, left, i-1 );
        quick_sort( a, i+1, right );
    }   
 }
 void Quick(int a[],int n)//規範排序函數 
 {
    quick_sort(a,0,n-1);
 } 

(3、完整代碼:

#include <stdio.h>
void swap(int *a,int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
//選主元 
int median(int a[],int left,int right)
{
    int center = (left+right)/2;
    if(a[left] > a[right])
    swap(&a[left],&a[right]);
    if(a[left] > a[center])
    swap(&a[left],&a[center]);
    if(a[right] < a[center])
    swap(&a[right],&a[center]);

    swap(&a[center],&a[right-1]);
    //將主元元放到a[right-1],只考慮a[left+1]到a[right-2]; 
    return a[right-1];
}
void quick_sort(int a[],int left,int right)
{
    if(left<right)//如果right>=left,則停止遞歸 
    {

        int med = median(a,left,right);
        int i = left, j = right ;
        for( ; ; ) 
        {
            while ( a[ ++i ] < med ) { }
            while ( a[ --j ] > med ) { }
            if ( i < j )
            swap( &a[i], &a[j] );
            else break;
        }
        swap( &a[i], &a[ right-1 ] );
        quick_sort( a, left, i-1 );
        quick_sort( a, i+1, right );
    }   
 }
 void Quick(int a[],int n)//規範排序函數 
 {
    quick_sort(a,0,n-1);
  } 
int main()
{
    int a[5] = {5,4,3,2,1};
    Quick(a,5);
    for(int i = 0;i<5;i++)
    {
        printf("%d\t",a[i]);
    }
}

運行結果:
這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章