插入排序,insertSort
2018-02-17 19:50
225 查看
// 插入排序,insertSort // O(n^2),稳定 #include <cstdio> void insertSort(int *arr, int n) { for(int i = 1; i < n; i++) { int temp = arr[i]; int j = i-1; while(j >= 0 && arr[j] > temp) { arr[j+1] = arr[j]; j--; } arr[j+1] = temp; } } void insertSort2(int *arr, int n) { for(int i = 1; i < n; i++) { for(int j = i-1; j >= 0; j--) { int temp; if(arr[j] > arr[j+1]) { temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } void insertSort3(int *arr, int n) { int i,j,temp; for(i = 1; i < n; i++) { temp = arr[i]; for(j = i-1; j >= 0 && arr[j] > temp; j--) arr[j+1] = arr[j]; arr[j+1] = temp; } } int main() { int arr[] = {9,3,8,4,0,2,6,7,5,1}; int n = sizeof(arr)/sizeof(arr[0]); insertSort(arr, n); // insertSort2(arr, n); // insertSort3(arr, n); for(int i = 0; i < n; i++) printf("%d ",arr[i]); return 0; }
相关文章推荐
- 直接插入排序(insert sort)
- 插入排序 insert_sort1 与 insert_sort_while的区别 细节排查自己的习惯错在哪儿?
- 插入排序之直接插入(InsertSort)
- 插入排序(1)——直接插入排序(insert sort)
- 算法不会,尚能饭否之排序——直接插入排序(Insert sort)
- Java排序算法(四)--直接插入排序(InsertSort)
- recursion-insert-sort( 插入排序的递归实现 )
- 插入排序(Insert-Sort)
- C:C的排序算法:直接插入排序(InsertSort)
- 【数据结构】中的插入(InsertSort)排序
- 插入排序:InsertSort:no more than ten lines
- java插入排序 Insert sort实例
- (直接)插入排序INSERT_SORT
- insertSort - 插入排序
- 插入排序之折半插入排序-binaryinsertsort
- 直接插入排序(Straight Insert Sort)
- 插入排序(Insertsort)之Java实现
- MYSQL数据库(十)- 数据表的插入(insert)、删(delete)、改(update)、查(select)、group by 分组、having语句设置分组条件,order by查询结果排序,
- c语言数组五种排序法:bubble,choise,quick,insert,shell 和js 数组排序sort的区别
- 插入排序—直接插入排序(Straight Insertion Sort)