7大排序演算法小結

來源:互聯網
上載者:User
1,快速排序 :

快速排序具有神奇的排序效率,平均狀況下它的時間複雜度為Ο(nlogn),但它通常明顯比其他Ο(nlogn)的演算法更快。快速排序概括起來三步:找基準,通常為序列首尾元素;與基準比較後左右分區;遞迴。
Java代碼:
public void QuickSort(int[] array )
{
    int i = 0 ;
    int j = array.length - 1;
    int k = array[0];
    while(i != j)
    {
        while(array[j] > k)
        {
            j--;
        }
        int temp = k;
        k = array[j];
        array[j] = temp;
        while (array[i] < k)
        {
            i++;
        }
        int temp1= k;
        k = array[i];
        array[i] = temp1;
    }
}

2,歸併排序 :

歸併排序採用分治法遞迴實現,單次排序實現將兩個有序序序列合并成一個。步驟為:申請大小為兩個已經排序序列之和的空間;設定兩個指標分別指向序列的起始位置;比較兩個指標處的元素,小的放入空間,並移動指標;重複前一步直到某一指標達到序列尾;將另一序列剩下的所有元素直接複製到定序序列尾;遞迴。
C語言實現:
void Merge(int *R,int low,int m,int high)
{
    int i=low,j=m+1,p=0;
    int *R1;
    R1=(int *)malloc((high-low+1)*sizeof(int));
    if(!R1) return;
    while(i<=m&&j<=high)
        R1[p++]=(R[i]<=R[j])?R[i++]:R[j++];
    while(i<=m)
        R1[p++]=R[i++];
    while(j<=high)
        R1[p++]=R[j++];
    for(p=0,i=low;i<=high;p++,i++)
        R[i]=R1[p];
}
void MergeSort(int R[],int low,int high)
{
    int mid;
    if(low<high)
    {
        mid=(low+high)/2;
        MergeSort(R,low,mid);
        MergeSort(R,mid+1,high);
        Merge(R,low,mid,high);
    }
}

3,堆排序 :

堆排序,先建一個大頂堆,將堆頂元素與序列中最後一個記錄交換,然後重新將它調整為一個大頂堆,再將堆頂記錄和第 n-1 個記錄交換,如此反覆直至排序結束。
大頂堆是一棵任一非葉結點的關鍵字均不小於其左右孩子(若存在)結點的關鍵字的完全二叉樹
C語言實現:
// array是待調整的堆數組,i是待調整的數組元素的位置,nlength是數組的長度
void HeapAdjust(int array[], int i, int nLength)//本函數功能是:根據數組array構建大根堆
{
    int nChild;
    int nTemp;
    for (nTemp = array[i]; 2 * i + 1 < nLength; i = nChild)
    {
        // 子結點的位置=2*(父結點位置)+ 1
        nChild = 2 * i + 1;
        // 得到子結點中較大的結點
        if (nChild < nLength - 1 && array[nChild + 1] > array[nChild])
            ++nChild;
        // 如果較大的子結點大於父結點那麼把較大的子結點往上移動,替換它的父結點
        if (nTemp < array[nChild])
        {
            array[i]= array[nChild];
        }
        else // 否則退出迴圈
        {
            break;
        }
        // 最後把需要調整的元素值放到合適的位置
        array[nChild]= nTemp;
    }
}
// 堆排序演算法
void HeapSort(int array[], int length)
{
    // 調整序列的前半部分元素,調整完之後第一個元素是序列的最大的元素
    for (int i = length / 2 - 1; i >= 0; --i)
    {
        HeapAdjust(array, i, length);
    }
    // 從最後一個元素開始對序列進行調整,不斷的縮小調整的範圍直到第一個元素
    for (int i = length - 1; i > 0; --i)
    {
        // 把第一個元素和當前的最後一個元素交換,
        // 保證當前的最後一個位置的元素都是在現在的這個序列之中最大的
        Swap(&array[0], &array[i]);
        // 不斷縮小調整heap的範圍,每一次調整完畢保證第一個元素是當前序列的最大值
        HeapAdjust(array, 0, i);
    }
}

4,選擇排序 :

選擇排序簡單直觀。其原理是:首先在未排序序列中找到最小元素,存放到排序序列的起始位置,然後,再從剩餘未排序元素中繼續尋找最小元素,放到排序序列末尾。以此類推,直到所有元素均排序完畢。
Java語言實現:
void SelectSort(SeqList R)
{
    int i,j,k;//最小關鍵字所在的位置
    for(i=1;i<n;i++){//做第i趟排序(1≤i≤n-1)
        k=i;
        for(j=i+1;j<=n;j++) //在當前無序區R[j..n]中選key最小的記錄R[k]
            if(R[j].key<R[k].key)
                k=j; //k記下目前找到的最小關鍵字所在的位置
        if(k!=i){ //交換R[i]和R[k]
            R[0]=R[i];R[i]=R[k];R[k]=R[0]; //R[0]作暫存單元
        } //endif
    } //endfor
} //SeleetSort

5,冒泡排序 :

冒泡排序是通過依次比較相鄰兩元素的大小,若逆序則交換,這樣一趟將最大的那個先找出來,然後再對剩下的隊列進行下一趟排序直到沒有發生交換為完成。
C語言實現:
void bubble_sort(int array[],int n)
{
    int i,j,flag,temp;
    for(i = 0; i < n-1; i++)
    {
        flag = 1;//交換標誌
        for(j = 0; j < n-i-1; j++)
        {
            if(array[j] > array[j+1])
            {
            temp = array[j];
            array[j] = array[j+1];
            array[j+1] = temp;
            flag = 0;
        }
    }
}

6,插入排序 :

插入排序,先在待排序列中選擇第一個元素,在排序序列中從後至前比較,當先元素大於序列中元素時插入。然後重複以前步驟。
void InsertSort(char array[],unsigned int n)
{
    int i,j;
    int temp;
    for(i=1;i<n;i++)
    {
        temp = array[i];//store the original sorted array in temp
        for(j=i ; j>0 && temp < array[j-1] ; j--)//compare the new array with temp
        {
            array[j]=array[j-1];//all larger elements are moved one pot to the right
        }
        array[j]=temp;
    }
}

7,希爾排序 :

希爾排序是一種分組插入排序演算法,它採用縮小增量方法對原序列進行分組,在組內實行直接插入排序直到增量=1 。增量d的取法:d1<n且避免序列中的值(尤其是相鄰的值)互為倍數
C#語言實現:
public void Shell_Sort(int[] list)
{
    int inc;
    for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
    for (; inc > 0; inc /= 3)
    {
        for (int i = inc + 1; i <= list.Length; i += inc)
        {
            int t = list[i - 1];
            int j = i;
            while ((j > inc) && (list[j - inc - 1] > t))
            {
                list[j - 1] = list[j - inc - 1];
                j -= inc;
            }
            list[j - 1] = t;
        }
    }
}

<完>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.