插入排序基本原理

#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;

}

全部评论

相关推荐

07-07 14:30
复旦大学 Java
遇到这种人我也不知道说啥了
无能的丈夫:但我觉得这个hr语气没什么问题啊(没有恶意
点赞 评论 收藏
分享
哈哈哈哈哈哈哈哈哈哈这个世界太美好了
凉风落木楚山秋:毕业出路老师不管,你盖个章他好交差就完事了,等你盖完毕业了就不关他事情了
点赞 评论 收藏
分享
这是什么操作什么意思,这公司我服了...
斯派克spark:意思是有比你更便宜的牛马了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务