forked from huihut/interview
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
131 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,27 @@ | ||
/* | ||
选择排序思路: | ||
1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。 | ||
2. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。 | ||
3. 针对所有的元素重复以上的步骤,除了最后一个。 | ||
4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。 | ||
*/ | ||
|
||
// 冒泡排序 | ||
void BubbleSort(vector<int>& v) { | ||
if (v.size() <= 0) | ||
return; | ||
int temp; | ||
for (int i = 0; i < v.size() - 1; ++i) { | ||
for (int j = 0; j < v.size() - 1 - i; ++j) { | ||
if (v[j] > v[j + 1]) { // 从小到大 | ||
temp = v[j]; | ||
v[j] = v[j + 1]; | ||
v[j + 1] = temp; | ||
} | ||
} | ||
} | ||
int len = v.size(); | ||
for (int i = 0; i < len - 1; ++i) | ||
for (int j = 0; j < len - 1 - i; ++j) | ||
if (v[j] > v[j + 1]) | ||
swap(v[j], v[j + 1]); | ||
} | ||
|
||
// 模板实现冒泡排序 | ||
template<typename T> //整數或浮點數皆可使用,若要使用物件(class)時必須設定大於(>)的運算子功能 | ||
void bubble_sort(T 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]) | ||
swap(arr[j], arr[j + 1]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,35 @@ | ||
/* | ||
选择排序思路: | ||
1. 在未排序序列中找到最小(大)元素,存放到排序序列的起始位置 | ||
2. 从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾 | ||
3. 以此类推,直到所有元素均排序完毕 | ||
*/ | ||
|
||
// 选择排序 | ||
void SelectionSort(vector<int>& v) { | ||
if (v.size() <= 0) | ||
return; | ||
int min, temp; | ||
for (int i = 0; i < v.size() - 1; ++i) { | ||
int min, len = v.size(); | ||
for (int i = 0; i < len - 1; ++i) { | ||
min = i; | ||
for (int j = i + 1; j < v.size(); ++j) { | ||
for (int j = i + 1; j < len; ++j) { | ||
if (v[j] < v[min]) { // 标记最小的 | ||
min = j; | ||
} | ||
} | ||
if (i != min) { // 交换到前面 | ||
temp = v[i]; | ||
v[i] = v[min]; | ||
v[min] = temp; | ||
} | ||
if (i != min) // 交换到前面 | ||
swap(v[i], v[min]); | ||
} | ||
} | ||
|
||
// 模板实现 | ||
template<typename T> | ||
void Selection_Sort(std::vector<T>& arr) { | ||
for (int i = 0; i < arr.size() - 1; i++) { | ||
int min = i; | ||
for (int j = i + 1; j < arr.size(); j++) | ||
if (arr[j] < arr[min]) | ||
min = j; | ||
std::swap(arr[i], arr[min]); | ||
} | ||
} |