插入排序(C语言版)

版权声明: https://blog.csdn.net/ITlanyue/article/details/79765698
#include<iostream>


using namespace std;


int n;


void lan(int a[],int size){

for(int i = 0;i < size;i++){

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

if(a[i] < a[j]){

int temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
}


int main(int argc,char** argv){

cout << "请输入要排序的数组的项数" << endl;
cin >> n;
int a[n]; 
for(int i = 0;i < n;i++){

cout << "请输入数组的第" << i + 1 << "个元素的值" << endl;
cin >> a[i];

}

cout << "该数组所有的值如下" << endl;
for(int i = 0;i < n;i++)
cout << a[i] << "   ";
lan(a,n);
cout << endl;
cout << "经排序后的数组各元素如下" << endl;
for(int i = 0;i < n;i++)
cout << a[i] << "   ";

展开阅读全文

没有更多推荐了,返回首页