介紹
每次從無序表中取出第乙個元素,把它插入到有序表中的合適位置。
分析
第一趟:比較前兩個數的大小,然後把其中最小的插入排在第乙個位置。
第二趟:比較第三個數與前兩個數的大小,大的數後移,知道遇到小的,便插入到較小數的後面,數不再移動。
依次進行,進行了n-1躺後,就完成了整個排序過程。
**
#include
using
namespace
std;
void print(int a,int n)
void insert_sort(int
array,int n)
array[k+1]=temp;}}
}int main()
; cout
<<"初始值: ";
for(int j=0;j<8;j++)
cout
<" ";
cout
<8);
print(a,8);
system("pause");
return
0;}
遇到的問題
插入排序是穩定的排序演算法。最壞的時間複雜度是逆序o(n^2)。
插入排序演算法學習
插入排序就如同手裡放了一堆紙牌,把紙牌從左到右進行排序。insert sort a for j 2 to n n為陣列a的長度 do key a j insert a j into the sorted sequence a 1.j 1 i j 1 while i 0 and a i key do ...
演算法學習 插入排序
插入排序 insertion sort 的演算法描述是一種簡單直觀的排序演算法。它的工作原理是通過構建有序序列,對於未排序資料,在已排序序列中從後向前掃瞄,找到相應位置並插入 實現 public static void insertsort int array if array.length 0 r...
演算法學習 插入排序
演算法介紹 插入排序 insertion sort 是一種簡單直觀且穩定的排序演算法。如果有乙個已經有序的資料序列,要求在這個已經排好的資料序列中插入乙個數,但要求插入後此資料序列仍然有序,這個時候就要用到一種新的排序方法 插入排序法,插入排序的基本操作就是將乙個資料插入到已經排好序的有序資料中,從...