⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 l9_1.cpp

📁 《数据结构(C++描述)》-李根强-源代码
💻 CPP
字号:
//直接插入排序
#include<iostream.h>
typedef int ElemType;
const int n=10;
ElemType a[n]={2,6,4,8,10,7,1,5,3,9};
void insertsort(ElemType R[],int n)
//待排序元素用一个数组R表示,数组有n个元素
{
  for ( int i=1; i<n; i++)   //i表示插入次数,共进行n-1次插入
   {
 ElemType temp=R[i]; //把待排序元素赋给temp
    int j=i-1; 
    while ((j>=0)&& (temp<R[j]))
 {
      R[j+1]=R[j]; j--; // 顺序比较和移动
      }
    R[j+1]=temp;
}
}
void print(ElemType r[n])
{ int i;
for(i=0;i<n;i++)
cout<<r[i]<<" ";
cout<<endl;
}
void main()
{ 
  cout<<"排序前的结果为:"<<endl;
 print(a);
insertsort(a,n);
 cout<<endl<<"排序后的结果为:"<<endl;
print(a);
}

⌨️ 快捷键说明

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