Skip to content

Commit

Permalink
更新选择排序
Browse files Browse the repository at this point in the history
  • Loading branch information
huihut committed Feb 10, 2018
1 parent 9c123ec commit a7a6e38
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
1 change: 0 additions & 1 deletion Algorithm/BubbleSort.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// 冒泡排序

void BubbleSort(vector<int>& v) {
int temp;
for (int i = 0; i < v.size() - 1; ++i) {
Expand Down
8 changes: 3 additions & 5 deletions Algorithm/BubbleSort_orderly.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// 冒泡排序(跳过有序的改进版)

// 冒泡排序(改进版)
void BubbleSort_orderly(vector<int>& v) {
int temp;
bool orderly = false;
for (int i = 0; i < v.size() - 1 && !orderly; ++i) {
orderly = true;
for (int j = 0; j < v.size() - 1 - i; ++j) {
if (v[j] > v[j + 1])
{
orderly = false;
if (v[j] > v[j + 1]) { // 从小到大
orderly = false; // 发生交换则仍非有序
temp = v[j];
v[j] = v[j + 1];
v[j + 1] = temp;
Expand Down
17 changes: 17 additions & 0 deletions Algorithm/SelectionSort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 选择排序
void SelectionSort(vector<int>& v) {
int min, temp;
for (int i = 0; i < v.size() - 1; ++i) {
min = i;
for (int j = i + 1; j < v.size(); ++j) {
if (v[j] < v[min]) { // 标记最小的
min = j;
}
}
if (i != min) { // 交换到前面
temp = v[i];
v[i] = v[min];
v[min] = temp;
}
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
### 排序

* [冒泡排序](Algorithm/BubbleSort.h)
* [冒泡排序(跳过有序的改进版)](Algorithm/BubbleSort_orderly.h)
* [冒泡排序(改进版)](Algorithm/BubbleSort_orderly.h)
* [选择排序](Algorithm/BubbleSort_orderly.h)

## Problems

Expand Down

0 comments on commit a7a6e38

Please sign in to comment.