老钟古

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
直接插入排序实现(by C++)
代码
 1 #include <iostream>
 2 using namespace std;
 3 
 4 /*
 5 InsertSort::InsertSort( vector<int> _list, int _len ) {
 6     for ( int i = 0; i < _len; ++i )
 7         list.push_back( _list[i] );
 8     this->len = _len;
 9 }
10 
11 void InsertSort::insert_sort() {
12     int insertNum;
13     for ( int i = 1; i < len; ++i ) {
14         insertNum = list[i];
15         int j = i;
16         while ( j > 0 && insertNum < list[j - 1] ) {
17             list[j] = list[j - 1];
18             j--;
19         }
20         list[j] = insertNum;
21     }
22 }
23 
24 void InsertSort::out() {
25     for ( int i = 0; i < len; ++i ) {
26         cout << list[i] << " ";
27         if ( ( i + 1 ) % 18 == 0 )
28             cout << endl;
29     }
30     cout << endl;
31 }
32 
33 */
34 int a[8= { 4658154590181062 };
35 
36 void InsertSort( ) {
37     int i, j;
38     for ( i = 1; i < 8++i ) {
39         int temp = a[i];
40         for ( j = i; j > 0 && temp < a[j - 1]; --j )
41             a[j] = a[j - 1];
42         a[j] = temp;
43     }
44 }
45 
46 int main() {
47     InsertSort( );
48     for ( int i = 0; i < 8++i )
49         cout << a[i] << " ";
50     cout << endl;
51 }
52 

 

posted on 2010-09-24 21:54  老钟古  阅读(202)  评论(0编辑  收藏  举报