-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
63 lines (53 loc) · 1.53 KB
/
Copy pathmain.cpp
File metadata and controls
63 lines (53 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// main.cpp
// BubbleSort
//
// Created by Dewen Chao on 15/10/19.
// Copyright © 2015年 Dewen Chao. All rights reserved.
//
#include <iostream>
#define N 10
using namespace std;
// 冒泡排序相邻进行比较,大的向数组尾部浮,每趟可以确定当前比较区间的最大值
// 使用改进后的方法,即当一次遍历中没有出现交换时可以确定已经成功排序,此时最好时间复杂度为O(N)
// 正常情况下最差时间复杂度O(N^2)
// 冒泡排序是稳定的
void bubbleSort(int arr[], int len) {
for(int i = 0; i < len - 1; i ++) {
for(int j = 0; j < len - 1 - i; j ++) {
if(arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main(int argc, const char * argv[]) {
int arr[N];
srand((unsigned int) time(NULL));
for(int i = 0; i < N; i ++) {
arr[i] = rand() % 100;
cout << arr[i] << " ";
}
cout << endl;
bubbleSort(arr, N);
for(int i = 0; i < N; i ++) {
cout << arr[i] << " ";
}
cout << endl;
// 正确性验证
// for(int i = 0; i < 10000; i ++) {
// for(int j = 0; j < N; j ++) {
// arr[j] = rand() % 100;
// }
// bubbleSort(arr, N);
// for(int j = 0; j < N - 1; j ++) {
// if(arr[j] > arr[j + 1]) {
// cout << "Error!" << endl;
// break;
// }
// }
// }
return 0;
}