-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
66 lines (56 loc) · 1.62 KB
/
Copy pathmain.cpp
File metadata and controls
66 lines (56 loc) · 1.62 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
64
65
66
//
// main.cpp
// SelectionSort
//
// 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^2),但是交换次数很少,最差N-1次
// 由于交换所需CPU时间比比较更多,所以N值较小时,选择排序比冒泡排序快
// 稳定性要看具体代码,此处代码稳定
void selectionSort(int arr[], int len) {
for(int i = 0; i < len - 1; i ++) {
int minIndex = i;
for(int j = i + 1; j < len; j ++) {
if(arr[minIndex] > arr[j]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = 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;
selectionSort(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;
// }
// selectionSort(arr, N);
// for(int j = 0; j < N - 1; j ++) {
// if(arr[j] > arr[j + 1]) {
// cout << "Error!" << endl;
// break;
// }
// }
// }
return 0;
}