a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C > 正文

常用排序算法整理分享(快速排序算法、希爾排序)

2020-01-26 15:33:58
字體:
來源:轉載
供稿:網友

整理了幾個排序算法,通過測試來看,最快的還是快速排序算法,簡直不是一個數量級的速度。

復制代碼 代碼如下:

#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)
{
 //思路:最后一個依次和前面的進行比較
 //將滿足的條件的往后移動,當然是從頭
 //開始且是從最小比較數組開始逐漸擴大
 //到整個數組
 int i,j,temp;
 for(i = 1;i < len;++i) {
  //獲取最后一個索引數據
  temp = a[i];
  for(j = i - 1;j >= 0;--j) {
   //從倒數第二個開始
   if(a[j] > temp)//升序排列
    a[j + 1] = a[j];
   else
    break;//立刻退出
  }
  //將最后一個位置插入到合適的位置
  a[j + 1] = temp;
 }
}

//希爾排序
void
shell_insert_sort(int *a,int len)
{
 //思路:就是比直接插入排序多了層
 //循環,這層循環是用來控制步進按
 //照算法的本來思路是這樣可以減少
 //交換次數
 int i,j,h,temp;
 for(h = len / 2;h > 0;h /= 2) {
  //內層其實本質還是直接插入
  //算法思路
  //注意下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)
{
 //思路:從數組最后開始兩兩比較
 //將底層滿足要求的數據逐漸交換
 //到上層,可能導致交換次數太多
 int i,j,temp;
 //如果一次冒泡中沒有發生交換可
 //以認為此次排列已經結束
 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)
{
 //思路:從數組中找一個值
 //然后排列數組使其兩邊要
 //么大于要么小于這個值,
 //然后遞歸下去排序
 //優勢在于每次找中間值可
 //以交換很多次。
 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大的數據
   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)
{
 //思路:就是遍歷數組找到極值
 //放到頭或者尾,這樣逐漸縮小
 //范圍到最小比較數組
 int i,j,pos,temp;
 for(i = 0;i < len - 1;++i) {
  //從頭開始獲取一個值假設為極值
  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;
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 一级毛片视频 | 在线观看免费av的网址 | 免费一区二区三区 | 久久久精品日韩 | 国产视频中文字幕 | 欧美激情首页 | 精品国产第一国产综合精品 | 亚洲成人av在线 | 欧美片网站免费 | 亚洲一区二区三区免费在线观看 | xxxwww日本| 亚洲成年| 黄色毛片在线播放 | a∨在线观看 | 日本私人网站在线观看 | 成人在线看片 | 国产激情视频 | 亚洲高清av | 一区二区在线看 | 中文字幕av在线播放 | 91精品国产综合久久香蕉922 | 日韩伦理一区二区三区 | 成人日韩在线观看 | 亚洲二区在线 | 亚洲欧美在线视频 | 国产精品久久久久无码av | 中文二区| 欧美午夜视频在线观看 | 中文字幕亚洲精品 | 国产自产自拍 | 精品久久久久久国产三级 | 中文字幕亚洲在线 | 日韩欧美在| 久久99热精品免费观看牛牛 | 久在线视频 | 久久爱9191 | 成人精品网 | 国产老女人精品毛片久久 | 一区二区日韩精品 | 在线久草 | 免费一区二区 |