常用排序算法整理分享(快速排序算法、希爾排序)
整理了幾個排序算法,通過測試來看,最快的還是快速排序算法,簡直不是一個數(shù)量級的速度。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <time.h>
#include <unistd.h>
//一些排序算法整理
//插入排序算法
//直接插入排序
void
direct_insert_sort(int *a,int len)
{
//思路:最后一個依次和前面的進行比較
//將滿足的條件的往后移動,當然是從頭
//開始且是從最小比較數(shù)組開始逐漸擴大
//到整個數(shù)組
int i,j,temp;
for(i = 1;i < len;++i) {
//獲取最后一個索引數(shù)據(jù)
temp = a[i];
for(j = i - 1;j >= 0;--j) {
//從倒數(shù)第二個開始
if(a[j] > temp)//升序排列
a[j + 1] = a[j];
else
break;//立刻退出
}
//將最后一個位置插入到合適的位置
a[j + 1] = temp;
}
}
//希爾排序
void
shell_insert_sort(int *a,int len)
{
//思路:就是比直接插入排序多了層
//循環(huán),這層循環(huán)是用來控制步進按
//照算法的本來思路是這樣可以減少
//交換次數(shù)
int i,j,h,temp;
for(h = len / 2;h > 0;h /= 2) {
//內(nèi)層其實本質(zhì)還是直接插入
//算法思路
//注意下i += h和i++兩者對算
//法的影響
for(i = h;i < len;i += h) {
//獲取最后一個索引的值
temp = a[i];
for(j = i - h;j >= 0;j -= h) {
if(a[j] > temp)//升序排列
a[j + h] = a[j];
else
break;
}
//將找到的位置插入最后一個索引
a[j + h] = temp;
}
}
}
//選擇排序
//冒泡排序
void
bubble_swap_sort(int *a,int len)
{
//思路:從數(shù)組最后開始兩兩比較
//將底層滿足要求的數(shù)據(jù)逐漸交換
//到上層,可能導致交換次數(shù)太多
int i,j,temp;
//如果一次冒泡中沒有發(fā)生交換可
//以認為此次排列已經(jīng)結(jié)束
bool exchange = false;
for(i = 0;i < len - 1;++i) {
for(j = len - 1;j > i;--j) {
//滿足條件的就進行交換
if(a[j] < a[j - 1]) {
temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
exchange = true;
}
}
if(exchange)
exchange = false;
else
break;
}
}
//快速排序
void
quick_swap_sort(int *a,int low,int high)
{
//思路:從數(shù)組中找一個值
//然后排列數(shù)組使其兩邊要
//么大于要么小于這個值,
//然后遞歸下去排序
//優(yōu)勢在于每次找中間值可
//以交換很多次。
int _low,_high,qivot;
if(low < high) {
_low = low;
_high = high;
//這里從最后一個開始
qivot = a[low];
//找中間值的辦法就是逐漸逼近
//從頭尾兩端開始逼近,順便也
//排序了
while(_low < _high) {
//既然是從low開始,那么首先
//就從high找小于qivot的(升
//序排列)
while(_low < _high && a[_high] > qivot)
--_high;//逐漸向中間逼近
if(_low < _high)//必然是找到了a[_high] > qivot的情況
a[_low++] = a[_high];
//這下a[_high]空出位置來了,所以從low找
//比qivot大的數(shù)據(jù)
while(_low < _high && a[_low] < qivot)
--_low;//逼近中間
if(_low < _high)
a[_high--] = a[_low];
}
//最后_low == _high那么這個位置就是qivot的位置
a[_low] = qivot;
//遞歸下去
quick_swap_sort(a,low,_high - 1);
quick_swap_sort(a,_low + 1,high);
}
}
//選擇排序
//直接選擇排序
void
direct_select_sort(int *a,int len)
{
//思路:就是遍歷數(shù)組找到極值
//放到頭或者尾,這樣逐漸縮小
//范圍到最小比較數(shù)組
int i,j,pos,temp;
for(i = 0;i < len - 1;++i) {
//從頭開始獲取一個值假設(shè)為極值
pos = i;
for(j = i + 1;j < len;++j) {
//滿足條件
if(a[pos] > a[j])//升序
pos = j;
}
if(pos != i) {
//進行交換
temp = a[pos];
a[pos] = a[i];
a[i] = temp;
}
}
}
void
disp(int *a,int len)
{
int i = 0;
for(;i < len;i++) {
if(i != 0 && i % 16 == 0)
printf("\n");
printf(" %d",a[i]);
}
printf("\n");
}
#define TEST_ARRAY_LEN 100000
#define TEST_COUNT 1
int
main(int argc,char *argv[])
{
//int a[] = {1,8,4,0,9,6,3,7,2,18,74,5,64,12,39};
//int len = sizeof(a) / sizeof(a[0]);
//direct_insert_sort(a,len);
//shell_insert_sort(a,len);
//bubble_swap_sort(a,len);
//quick_swap_sort(a,0,len - 1);
//direct_select_sort(a,len);
disp(a,len);
return 0;
}
您可能感興趣的文章
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10深入第K大數(shù)問題以及算法概要的詳解
- 01-10深入N皇后問題的兩個最高效算法的詳解
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實現(xiàn)方法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10深入理解堆排序及其分析
- 01-10深入單鏈表的快速排序詳解
- 01-10貪心算法 WOODEN STICKS 實例代碼


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實例總結(jié)
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實現(xiàn)頁面的局部加載