#include <iostream>
using namespace std;
//升序
void test01(int *arr,int len)
{
for (size_t i = 0; i < len; i++)
{
for (size_t j = 0; j < len-i-1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "升序: ";
for (size_t i = 0; i < len; i++)
{
cout <<arr[i] << " ";
}
cout << endl;
}
//降序
void test02(int arr[],int len)
{
for (size_t i = 0; i < len; i++)
{
for (size_t j = 0; j < len - i - 1; j++)
{
if (arr[j] < arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
cout << "降序: ";
for (size_t i = 0; i < len; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int arr[10] = { 4,2,8,0,5,7,1,3,9,6 };
int arrLen = sizeof(arr) / sizeof(arr[0]);
test01(arr, arrLen);
test02(arr, arrLen);
return 0;
}
冒泡排序
最新推荐文章于 2024-07-16 23:19:20 发布