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

來源:互聯網
上載者:User

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

複製代碼 代碼如下:

#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;
}

聯繫我們

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