插入排序(算法学习系列)

来源:互联网 发布:数码宝贝数据库 编辑:程序博客网 时间:2024/06/03 02:25

// InsertSort.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"void swap(int (&a),int (&b)){int temp = a;a = b;b = temp;}void insertSort(int *A){for (int j = 1; j <= 6; j++){for (int i = j-1; i>=0; i--){if (A[i] >= A[i+1]){ swap(A[i], A[i+1]);}}}}int _tmain(int argc, _TCHAR* argv[]){int A[7] = { 31, 41, 59, 26, 41 };insertSort(A);system("pause");return 0;}

其实就是黑书的练习题

注意到的是,因为需要一个位置来防止边界溢出,故排序时申请的数组和数据元素相差为一,即需要额外的O(1)空间


0 0
原创粉丝点击