日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

快速排序c语言实现,快速排序的C语言代码实现

發(fā)布時(shí)間:2025/3/12 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 快速排序c语言实现,快速排序的C语言代码实现 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

快速排序?qū)嵸|(zhì)上是對(duì)“冒泡排序”的一種改進(jìn),整個(gè)排序過程可概括為:通過N趟的排序?qū)⒃镜呐判驍?shù)據(jù)分為若干塊進(jìn)行分塊排序,而在每趟排序過程中,以指定的關(guān)鍵字將待排數(shù)據(jù)分別分為比關(guān)鍵字大的部分和比關(guān)鍵字小的部分,反復(fù)上述過程,將整個(gè)待排數(shù)列分散為若干個(gè)小數(shù)列而分別進(jìn)行排序操作。假設(shè)我們現(xiàn)對(duì)一列數(shù)進(jìn)行快速排序,其C語言代碼實(shí)現(xiàn)如下:

#include

int partition(int *data,int low,int high)

{ ??????int t = 0;

t = data[low];

while(low < high)

{ ??????while(low < high && data[high] >= t)

high--;

data[low] = data[high];

while(low < high && data[low] <= t)

low++;

data[high] = data[low];

}

data[low] = t;

return low;

}

void sort(int *data,int low,int high)????????//快排每趟進(jìn)行時(shí)的樞軸要重新確定,由此進(jìn) //一步確定每個(gè)待排小記錄的low及high的值

{ ??????if(low >= high)

return ;

int pivotloc = 0;

pivotloc = partition(data,low,high);

sort(data,low,pivotloc-1);

sort(data,pivotloc+1,high);

}

void quick_sort(int *data,int n) ????????//該函數(shù)進(jìn)行sort過程的調(diào)用

{ ????????sort(data,0,n-1); }

int main()

{?????? int i;

int data[]={49,38,32,98,65,74,12,8};

quick_sort(data,sizeof(data)/sizeof(int));

for( i = 0 ; i < sizeof(data)/sizeof(int); i++)

printf("%d ",data[i]);

printf("\n");

return 0;

}

總結(jié)

以上是生活随笔為你收集整理的快速排序c语言实现,快速排序的C语言代码实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。