插入排序基本原理

#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-18 18:45
已编辑
中山职业技术学院 Java
投递TP-LINK等公司7个岗位
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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