2 回答
TA贡献1825条经验 获得超4个赞
#include <iostream>
using namespace std;
//从小到大排序
template <typename T>
void Bubble(T arr[], int n)
{
int i,j;
for (i=0; i<n; i++)
for (j=0; j<n-i-1; j++)
{
if (arr[j] > arr[j+1])
{
T temp;
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
for (i=0; i<n; i++)
cout << arr[i] << endl;
}
//从小到大排序
template <typename T>
void Insert(T arr[], int n)
{
int i,j,pos;
T temp;
for (i=0; i<n; i++)
{
pos = i;
for (j=i; j<n; j++)
{
if (arr[j] < arr[pos])
{
pos = j;
temp = arr[j];
arr[j] = arr[pos];
arr[pos] = temp;
}
}
temp = arr[i];
arr[i] = arr[pos];
arr[pos] = temp;
}
for (i=0; i<n; i++)
cout << arr[i] << endl;
}
int main()
{
int i[7] = {2, 234, 234, 12, 1400, 345, 564};
float f[7] = {2.9, 234.2, 234.5, 12.73, 1400, 345.2, 564.1};
//Bubble(i, 7);
Insert(i, 7);
cout << endl;
//Bubble(f, 7);
Insert(f, 7);
return 0;
}
- 2 回答
- 0 关注
- 790 浏览
添加回答
举报