直接插入排序

来源:互联网 发布:交大知行大厦 邮编 编辑:程序博客网 时间:2024/06/06 18:45
#include<iostream>/**直接插入排序 -- 数组arr被分为有序区和无序区,刚开始时,有序区只有arr[0]一个元素。每趟排序将无序区第一个元素插入到有序区适当位置中。**/int main() {int arr[5] = {5,4,3,2,1};void InsertSort( int array[], int n );InsertSort(arr,5);for( int i = 0; i < 5; i++ ) {std::cout<<arr[i]<<std::endl;}return 0;}void InsertSort( int array[], int n ) {int i,j;int tmp;for( i = 1; i < n; i++ ){tmp = array[i];j = i-1;while( j >= 0 && tmp < array[j] ) {array[j+1] = array[j];j--;}array[j+1] = tmp;}}

0 0
原创粉丝点击