插入排序基本原理

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

}

全部评论

相关推荐

09-27 00:29
东北大学 Java
伟大的麻辣烫:查看图片
阿里巴巴稳定性 77人发布 投递阿里巴巴等公司10个岗位
点赞 评论 收藏
分享
09-29 17:44
已编辑
蔚来_测(准入职员工)
//鲨鱼辣椒:见不了了我实习了四个月上周再投筛选了一天就给我挂了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务