#include <iostream>
using namespace std;
int power[] = {15,25,90,23,10}, temp;
int numslen = sizeof(power) / sizeof(int); //计算实际长度
int min = power[0]; //假设最小值是数组的第一个元素
int minIndex = 0; //最小值的初始下标是0
void spot() //排序函数
{
for (int i = 0; i < numslen; i++)
{
min = power[i];//假设第i个元素是最小值
minIndex = i;
for (int j = i + 1; j < numslen; j++)
{
if(power[j]<min)
{
min = power[j];
minIndex = j;
}
}
if (minIndex > i)
{
temp = power[minIndex];
power[minIndex] = power[i];
power[i] = temp;
}
}
cout << "排序后:";
for (int i = 0; i < numslen; i++)
cout << power[i]<<",";
}
int main() //插入数后排序
{
int insert,insertIndex;
cout << "请输入要插入的数:" << endl;
cin >> insert;
for (int i = 0; i < numslen; i++)
{
if (insert > power[i])
{
insertIndex = i;
break;
}
}
for (int i = numslen - 1; i >= insertIndex; i--)
{
power[i + 1] = power[i];
}
power[insertIndex] = insert;
numslen++;
cout << "插入数值后排序结果为" <<spot(insert) << endl;
return 0;
}
红糖糍粑
相关分类