6-7-21.cpp

来自「学习c++的ppt」· C++ 代码 · 共 25 行

CPP
25
字号
#include <iostream.h>
void InsertSort(int  slist[], int  n)
{      int  temp;
       int i, j, low, high, mid;
       for (i=1; i < n; i++)  {
               temp = slist[i];
               low = 0; high = i-1;
               while(low <= high)   {
                    mid = (low+high)/2;
                    if(temp < slist[mid])   high=mid-1;
                    else  low=mid+1;
               }
	   for(j=i-1; j>=low; j --) 
               slist[j+1]=slist[j];
	   slist[low]=temp;
       }
}      

void  main()
{   int  a[] =  {8, 6, 7, 9, 4, 5, 2};
     InsertSort(a,  7);  cout << "排序后的数据是:";
     for(int i=0; i < 7; i ++)  cout << a[i] << ",";
     cout << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?