幾種常見的排序方法(C語言實現)

來源:互聯網
上載者:User

標籤:

#include <stdio.h>#include <stdlib.h>#include <Windows.h>//直接插入排序void InsertSort(int arry[], int n){    int i, j;    int temp;//臨時變數    for (i = 1; i < n; i++)    {        temp = arry[i];        for (j = i - 1; j >= 0; j--)        {            if (temp > arry[j])                break;            else                arry[j + 1] = arry[j];        }        arry[j+1] = temp;    }}//直接選擇排序void SelectSort(int arry[], int n){    int i, j;    int temp;    for (i = 0; i < n-1; i++)    {        temp = i;        for (j = i + 1; j < n; j++)        {            if (arry[j] < arry[temp])                temp = j;        }        if(temp!=i)            arry[temp] ^= arry[i] ^= arry[temp] ^= arry[i];//實現arry[temp]和arry[i]值交換    }}//冒泡排序void BubbleSort(int arry[], int n){    int i, j, k;    for (i = 0; i < n-1; i++)    {        for (j = i; j < n; j++)        {            if (arry[j - 1]>arry[j])                arry[j - 1] ^= arry[j] ^= arry[j - 1] ^= arry[j];//實現arry[temp]和arry[i]值交換        }    }}//希爾排序第一類,void ShellSort1(int arry[], int n){    int i, j, k, gap,temp;    for (gap = n / 2; gap > 0; gap /= 2)    {        for (k = 0; k < gap; k++)//同組先排        {            for (i = k+gap; i < n; i += gap)            {                temp = arry[i];                for (j = i - gap; j >= 0; j -= gap)                {                    if (temp > arry[j])                        break;                    else                        arry[j + gap] = arry[j];                }                arry[j + gap] = temp;            }        }    }}//希爾排序第二類void ShellSort2(int arry[], int n){    int i, j, k, gap, temp;    for (gap = n / 2; gap > 0; gap /= 2)    {        for (i = gap; i < n; i++)//所有組一起排序            {                temp = arry[i];                for (j = i - gap; j >= 0; j -= gap)                {                    if (temp > arry[j])                        break;                    else                        arry[j + gap] = arry[j];                }                arry[j + gap] = temp;            }            }}//希爾排序,利用gap分組後採用冒泡方式排序void ShellSort3(int arry[], int n){    int i, j, k, gap, temp;    for (gap = n / 2; gap > 0; gap /= 2)    {        for (k = 0; k < gap; k++)        {            for (i = k; i < n-gap; i += gap)            {                for (j = k+gap; j <n ; j += gap)                {                    if (arry[j - gap]>arry[j])                        arry[j - gap] ^= arry[j] ^= arry[j - gap] ^= arry[j];                }                            }        }    }}//快速排序,遞迴調用void QuickSort(int arry[], int s,int t){    int i, j;    if (s < t)    {        i = s;        j = t+1;        while (1)        {            do            {                i++;            }while (!(arry[s] <= arry[i] || i >= t));            do            {                j--;            } while (!(arry[s] >= arry[j] || j <= s));                        if (i < j)                arry[i] ^= arry[j] ^= arry[i] ^= arry[j];                            else                break;        }        if (arry[j]!= arry[s])            arry[j] ^= arry[s] ^= arry[j] ^= arry[s];        QuickSort(arry, s, j-1);        QuickSort(arry, j + 1, t);    }}int main(){    int arry1[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41};    int arry2[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };    int arry3[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };    int arry4[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };    int arry5[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };    int arry6[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };    int arry7[15] = { 19,15,13,10,3,30,23,76,5,16,7,30,32,2,41 };        //七種方法任選一種,但效率有別    //一般情況下:快速排序>直接插入>直接選擇==冒泡    //希爾排序看情況而定。一般優於直接選擇排序    //InsertSort(arry1, 15);        //SelectSort(arry2, 15);        //BubbleSort(arry3, 15);        //ShellSort1(arry4, 15);    //ShellSort2(arry5, 15);        //ShellSort2(arry6, 15);        //QuickSort(arry7,0,14);        int i = 0;    for (i = 0; i < 15; i++)    {        printf("%d  ", arry7[i]);    }    system("pause");}

 

幾種常見的排序方法(C語言實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.