qfedu-cpp-level/day2/d5.cpp

58 lines
1.1 KiB
C++

#include <iostream>
#define N 7
using namespace std;
void sort(int (&arr)[N])
{
for (int i = 0; i <= N - 1; i++)
{
for (int j = 0; j <= N - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
arr[j] ^= arr[j + 1];
arr[j + 1] ^= arr[j];
arr[j] ^= arr[j + 1];
// int temp = arr[j];
// arr[j] = arr[j + 1];
// arr[j + 1] = temp;
}
}
}
}
void sort(int arr[], int size)
{
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main()
{
int nums[] = {1, 5, 2, 0, 9, 10, 7};
int size = sizeof(nums) / sizeof(nums[0]);
sort(nums);
// sort(nums, size);
for (int i = 0; i < size; i++)
{
cout << nums[i] << "\t";
}
cout << endl;
return 0;
}