插入排序基本原理

#include <iostream>

using namespace std;

int main(){

//插入排序基本原理

int a[] = { 9,8,1,4,2,3,0,6,7,5 };

int n=sizeof(a)/sizeof(a[0]);

for (int j = 0; j < n; j++) {

cout << a[j] << " ";

}

cout << endl;

int value=a[1];

int index=1-1;

while(index>=0 && a[index]>value){

a[index+1]=a[index];

index--;

}

a[index+1]=value;

for (int j = 0; j < n; j++) {

cout << a[j] << " ";

}

cout << endl;

value=a[2];

index=2-1;

while(index>=0 && a[index]>value){

a[index+1]=a[index];

index--;

}

a[index+1]=value;

for (int j = 0; j < n; j++) {

cout << a[j] << " ";

}

cout << endl;

value=a[3];

index=3-1;

while(index>=0 && a[index]>value){

a[index+1]=a[index];

index--;

}

a[index+1]=value;

for (int j = 0; j < n; j++) {

cout << a[j] << " ";

}

cout << endl;

value=a[4];

index=4-1;

while(index>=0 && a[index]>value){

a[index+1]=a[index];

index--;

}

a[index+1]=value;

for (int j = 0; j < n; j++) {

cout << a[j] << " ";

}

cout << endl;

value=a[5];

index=5-1;

while(index>=0 && a[index]>value){

a[index+1]=a[index];

index--;

}

a[index+1]=value;

for (int j = 0; j < n; j++) {

cout << a[j] << " ";

}

cout << endl;

value=a[6];

index=6-1;

while(index>=0 && a[index]>value){

a[index+1]=a[index];

index--;

}

a[index+1]=value;

for (int j = 0; j < n; j++) {

cout << a[j] << " ";

}

cout << endl;

value=a[7];

index=7-1;

while(index>=0 && a[index]>value){

a[index+1]=a[index];

index--;

}

a[index+1]=value;

for (int j = 0; j < n; j++) {

cout << a[j] << " ";

}

cout << endl;

value=a[8];

index=8-1;

while(index>=0 && a[index]>value){

a[index+1]=a[index];

index--;

}

a[index+1]=value;

for (int j = 0; j < n; j++) {

cout << a[j] << " ";

}

cout << endl;

value=a[9];

index=9-1;

while(index>=0 && a[index]>value){

a[index+1]=a[index];

index--;

}

a[index+1]=value;

for (int j = 0; j < n; j++) {

cout << a[j] << " ";

}

cout << endl;

getchar();

return 0;

}

全部评论

相关推荐

预计下个星期就能开奖吧,哪位老哥来给个准信
华孝子爱信等:对接人上周说的是这周
点赞 评论 收藏
分享
牛客963010790号:为什么还要收藏
点赞 评论 收藏
分享
贺兰星辰:不要漏个人信息,除了简历模板不太好以外你这个个人简介是不是太夸大了...
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务