排序演算法總結

2021-06-01 05:01:17 字數 4078 閱讀 5878

插入排序:

是乙個對少量元素進行排序的有效演算法。實現比較簡單。時間複雜度:o(n^2),空間複雜度:o(1)。是穩定的排序方法。

**:view plain

copy to clipboard

print?

//insertion sort

#include 

using

namespace std;  

//insertion sort

void insertionsort(int *a,int n)  

*(a + j + 1) = temp;  

}  }  

int main()  

/*//insertion sort

for(int i = 1;i 

*(a + j + 1) = temp;

}*/insertionsort(a,n);  

cout<

cout<

view plain

copy to clipboard

print?

free(a);  

view plain

copy to clipboard

print?

}  資料測試:

上述**可以改進的乙個地方是:在查詢插入位置的時候可以採用二分查詢,但是這樣依然不可以把時間複雜度降低為o(nlogn),因為移動元素的複雜度沒有降低。所以時間複雜度仍然是o(n^2)。

做此改進需要新增函式insertloc用於二分查詢需要插入的位置,以及修改函式insertionsort的實現。具體如下:

view plain

copy to clipboard

print?

//改進:用二分查詢來找到插入的位置

//在陣列a[low]---a[high]查詢val插入的位置

int insertloc(int *a,int low,int high,int val)  

int mid = (low + high) / 2;  

if(val > *(a + mid) && val > *(a + mid + 1))  

return insertloc(a,mid + 1,high,val);  

else

if(val 

return insertloc(a,low,mid,val);  

else

return mid;  

}  void insertionsort(int *a,int n)  

*(a + insert_location) = temp;  

for(int m = 0;m <= i;++m)  

cout<

cout

}  

選擇排序

第一次找出a中最小的元素,與a[0]交換,接著,找出a中次小得元素,與a[1]互換。對a中頭n-1個元素執行這一過程。時間複雜度:o(n^2),不需要輔助空間。是穩定的排序方法。

**:view plain

copy to clipboard

print?

//選擇排序

#include 

using

namespace std;  

void chosesort(int* a,int n)  

}  }  }  

int main()  

chosesort(a,n);  

cout<

cout<

free(a);  

view plain

copy to clipboard

print?

}  合併排序

採用分治法。將n個元素分成各含n/2個元素的子串行,用合併排序法對兩個子串行遞迴的排序(子串行長度為1時遞迴結束),最後合併兩個已排序的子串行得到結果。時間複雜度:o(nlogn),空間複雜度:o(n)。不是穩定的排序

**:view plain

copy to clipboard

print?

//合併排序

#include 

using

namespace std;  

#define max_value 100000//用於設定哨兵,避免檢查是否每乙個堆都是空的

//合併兩個子陣列的函式

void merge(int *a,int p,int q,int r)  

else

}  free(a1);  

view plain

copy to clipboard

print?

free(a2);  

view plain

copy to clipboard

print?

}  view plain

copy to clipboard

print?

//遞迴合併排序演算法

void mergesort(int *a,int p,int r)  

}  int main()  

mergesort(a,0,n - 1);  

cout<

cout<

free(a);  

view plain

copy to clipboard

print?

}  如果不使用哨兵元素,需要修改merge函式,如下:

view plain

copy to clipboard

print?

//合併兩個子陣列的函式(不使用哨兵元素)

void merge(int *a,int p,int q,int r)  

else  

}  while(index1 

while(index2 

free(a1);

class="cpp" name="code">    free(a2);

view plain

copy to clipboard

print?

}  氣泡排序

每一趟都比較相鄰兩個元素,若是逆序的,則交換。結束的條件應該是「在一趟排序過程中沒有進行過交換元素的操作」。時間複雜度:o(n^2),不需要空間複雜度。是不穩定的排序。

view plain

copy to clipboard

print?

#include 

using

namespace std;  

void bubblesort(int *a,int n)  

}  if(flag == 0)break;  

}  }  

int main()  

bubblesort(a,n);  

cout<

cout<

class="cpp" name="code"> free(a);

view plain

copy to clipboard

print?

}  快速排序

它是對氣泡排序的一種改進。它的基本思想是:通過一趟排序將待排序元素分成兩個部分,其中一部分元素比另一部分元素小。再分別對這兩部分元素進行排序。以達到整個元素序列有序。時間複雜度:o(nlogn),空間複雜度o(1),是不穩定的演算法。

**:view plain

copy to clipboard

print?

#include 

using

namespace std;  

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

*(a + low) = pivotkey;  

return low;  

}  void quicksort(int *a,int low,int high)  

}  int main()  

quicksort(a,0,n - 1);  

cout<

cout<

free(a);  

view plain

copy to clipboard

print?

}  

排序演算法總結

1 直接插入排序 1 穩定性 穩定 2 適用情況 待排記錄規模較小,或者記錄已經基本有序 2 希爾排序 1 穩定性 不穩定 2 特點 希爾排序的執行時間依賴於增量序列,它的效率比直接插入排序有較大的改進。3 氣泡排序 1 穩定性 穩定 2 特點 當待排記錄基本有序是,氣泡排序是不錯的選擇 但由於氣泡...

排序演算法總結

1 選擇排序 選擇排序的思想是依次從待排序數列中選擇最大 小 的 第二大 小 的等等,然後依次重新排列為有序數列。void selectionsort int a,int n if min i 時間複雜度o n 2 2 歸併排序 void merge int a,int left,int mid,i...

排序演算法總結

學習了這麼多的排序演算法,還沒有做個總結,呵呵 氣泡排序 氣泡排序是最慢的排序演算法。在實際運用中它是效率最低的演算法。它通過一趟又一趟地比較陣列中的每乙個元素,使較大的資料下沉,較小的資料上公升。它是 o n 2 的演算法。快速排序 快速排序是乙個就地排序,分而治之,大規模遞迴的演算法。從本質上來...