排序演算法是面試筆試中必定要涉及的內容,常見的排序演算法有:插入排序,氣泡排序,選擇排序,快速排序,堆排序,希爾排序,歸併排序,基數排序等,下面是我自己針對這幾種演算法進行一些總結和實現。
(1)按照時間效能來分,可以劃分為三類排序演算法:
1. o(nlogn): 快速排序,堆排序,歸併排序,其中以快排為最佳;
2. o(n2): 直接插入排序,氣泡排序,簡單選擇排序,其中以直接插入排序為最佳,尤其對於那些關鍵字近似有序的記錄序列;
3. o(n): 只有基數排序,基數排序適合n值很大而關鍵字較小的序列。
(2)效能易變性:
當待排序列有序時,直接插入排序和氣泡排序能到達o(n),而對於快速排序而言,這反而是最不好的情況,此時時間效能蛻化為o(n2);簡單選擇排序,堆排序和歸併排序的時間複雜度不隨數列中關鍵字的分布
而改變。
(3)穩定性:
選擇排序、快速排序、希爾排序、堆排序不是穩定的排序演算法,
氣泡排序、插入排序、歸併排序和基數排序是穩定的排序演算法。
各排序時間複雜度和空間複雜度對比表:
平台:vs 2008
演算法實現:
---------------插入排序(直接插入)--------------
#include "stdafx.h"
#include "stdio.h"
#define len 10
void sort_insert(int a,int len)
a[j + 1] = key; //最後把最小的key賦值給首元素a[0]}}
int _tmain(int argc, _tchar* ar**)
;int k;
sort_insert(a,len);
for (k = 0; k < len; k++)
return 0;
}---------------氣泡排序--------------
#include "stdafx.h"
#include "stdio.h"
#define len 10
void bubble_sort(int arry,int len)}}
}int _tmain(int argc, _tchar* ar**)
;int k;
bubble_sort(a,len);
for (k = 0; k < len; k++)
return 0;
}---------------選擇排序(簡單選擇排序)--------------
#include "stdafx.h"
#include "stdio.h"
#define len 10
//交換陣列中的兩個數
void swap(int v,int i,int j)
//選擇排序演算法
void selection_sort(int arry,int len)
}swap(arry,j,flag);}}
int _tmain(int argc, _tchar* ar**)
;int k;
selection_sort(a,len);
for (k = 0; k < len; k++)
return 0;
}---------------快速排序--------------
#include "stdafx.h"
#include "stdio.h"
#define len 10
//交換陣列中的兩個數
void swap(int v,int i,int j)
//快速排序演算法
void quick_sort(int v,int left,int right)
//確定大概中間位置元素為分割槽元素
//把分割槽元素移動到最左邊
swap(v,left,(left + right)/2);
last = left;
//分割槽:把小於分割槽元素的數全部移動到它的左邊
//把大於分割槽元素的數全部移動到它的右邊
//結果為: x
for (i = left + 1; i <= right; i++)
}//恢復分割槽元素
swap(v,left,last);
//一次快排結束
//遞迴呼叫排序演算法
quick_sort(v,left,last -1);
quick_sort(v,last + 1,right);
}int _tmain(int argc, _tchar* ar**)
;int k;
quick_sort(a,0,len - 1);
for (k = 0; k < len; k++)
return 0;
}演算法具體執行過程:
舉例:a[6] = ;
選定中間元素(0+6)/2 = 3,即a[3]為5:
3 6 2 5 0 4 7
把5與left互換;
5 6 2 3 0 4 7
left和last都指向5;
i從left + 1開始,即從6開始;
如果小於a[left]內的數,則和++last互換,否則不執行操作;
執行過程如下:
5 6 2 3 0 4 7
5 2 6 3 0 4 7
5 2 3 6 0 4 7
5 2 3 0 6 4 7
5 2 3 0 4 6 7
5 2 3 0 4 6 7
最後交換left和last,此時last指向的是4
4 2 3 0 5 6 7
最終一次快排結果為: 5
然後在和兩個集合中遞迴快排
---------------堆排序--------------
#include "stdafx.h"
#include "stdio.h"
#define len 10
//調整篩選
void shift(int h,int n,int m)
if (tmp < h[j])
else
}h[m] = tmp;//把tmp儲存的元素插入到正確位置
}//堆排序
void heapsort(int p,int n)
for (i = n - 1; i >= 1; i--)
}int _tmain(int argc, _tchar* ar**)
;int k;
heapsort(a,len);
for (k = 0; k < len; k++)
return 0;
}---------------希爾排序--------------
/#include "stdafx.h"
#include "stdio.h"
#define len 10
void shellsort(int a,int n)}}
} int _tmain(int argc, _tchar* ar**)
;int k;
shellsort(a,len);
for (k = 0; k < len; k++)
return 0;
}----------------------相關鏈結--------------------------
鍊錶與時間複雜度o:
a.歸併兩個鍊錶(有序)時,採用快速排序,時間複雜度為o(nlogn);
b.單鏈表,雙鏈表插入和刪除元素的時間複雜度都為o(n);
常見的排序演算法
一 氣泡排序 include include void swap int a,int b void bubblesort int arr,int size int main void bubblesort a,5 for int i 0 i 5 i cout 二 選擇排序 void selectio...
常見的排序演算法
需要包含的標頭檔案 include stdafx.h include include include 氣泡排序是穩定排序 時間複雜度 o n 2 void swap int a,int b void bubblesort int a,int n void printnum int a,int n a...
常見的排序演算法
排序演算法是最基礎,也是最簡單的演算法思想,因為應用場景多,書寫簡單,所以應用較為普遍,所以在面試和考試的時候,都會涉及到排序演算法,雖然排序演算法種類很多,但是只要理解了思想,然後靈活運用,那麼就不難記憶.排序演算法兩個需要記憶和理解的點就是 演算法思想和時間複雜度.下面我們就介紹和分析一下常見的...