// implementation of Bubble Sort (C++)
#include <iostream>
using namespace std;
void SwapTwo(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
void BubbleSort(int arr[], int size)
{
for (int i=0; i<size; i++)
{
int j = size-1;
while (j>i)
{
if (arr[j]<arr[j-1])
SwapTwo (arr[j], arr[j-1]);
j--;
}
}
}
int main()
{
int nums[10] = {5,3,7,2,1,9,14,8,7,4};
BubbleSort (nums, (sizeof(nums)/sizeof(int)));
for (int i=0; i<(sizeof(nums)/sizeof(int)); i++)
{
cout << nums[i] << " ";
}
cout << endl;
return 0;
}
C++冒泡排序法 (Bubble Sort)
最新推荐文章于 2021-08-21 21:41:03 发布