冒泡排序的性能基本上算是最差的, 所以在实际应用中, 不要使用它.
#include <iostream> using namespace std;
void bubbleSort(int arr[], int n) { int i, j; int tmp; for (i = 0; i < n-1; i++) { for (j = n-1; j > i; j--) { if (arr[j-1] > arr[j]) { tmp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = tmp; } } } }
int main() { int arr[15] = {10, 21, 2, 1, 3, 45, 2, 932, 32, 27, 86, 65, 576, 434, 76753};
int i; cout << "Original array" << endl; for (i = 0; i < 15; i++) cout << arr[i] << " "; cout << endl << endl;
bubbleSort(arr, 15);
cout << "Sorted array" << endl; for (i = 0; i < 15; i++) cout << arr[i] << " "; cout << endl;
return 0; }
|
阅读(1191) | 评论(0) | 转发(1) |