当前位置: 代码迷 >> 综合 >> 冒泡排序算法实例
  详细解决方案

冒泡排序算法实例

热度:49   发布时间:2024-01-31 20:34:04.0

案例描述:封装一个函数,利用冒泡排序,实现对整型数组的升序排序

例如数组:int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };

例程如下:

#include <iostream>//冒泡排序算法函数 
using namespace std;int main() {int a[10] = { 4,3,6,9,1,2,10,8,7,5 };void BubbleSort(int* a, int len);BubbleSort(a, 10);for (int k = 0; k < 10; k++) //输出排序后的数组cout << a[k] << " ";cout << endl;system("pause");return 0;
}void BubbleSort(int *a, int len) //int * a 也可以写为int a[]{for(int i=0;i<len-1;i++)for(int j=0;j<len-1-i;j++)if (a[j + 1] < a[j]){int temp = a[j];a[j] = a[j + 1];a[j + 1] = temp;}
}

 

  相关解决方案