mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
cpplint and msvc fixes for - sorting
(cherry picked from commit 0d8e015d15
)
This commit is contained in:
parent
887bff8a9e
commit
77e781f709
@ -34,8 +34,8 @@ void beadSort(int *a, int len) {
|
||||
// Put sorted values in array using beads
|
||||
for (int i = 0; i < len; i++) {
|
||||
int j;
|
||||
for (j = 0; j < max && BEAD(i, j); j++)
|
||||
;
|
||||
for (j = 0; j < max && BEAD(i, j); j++) {
|
||||
}
|
||||
|
||||
a[i] = j;
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ optimized bubble sort algorithm. It's right down there.
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
short swap_check = 1;
|
||||
bool swap_check = true;
|
||||
std::cout << "Enter the amount of numbers to sort: ";
|
||||
std::cin >> n;
|
||||
std::vector<int> numbers;
|
||||
@ -56,11 +56,11 @@ int main() {
|
||||
}
|
||||
|
||||
// Bubble Sorting
|
||||
for (int i = 0; (i < n) && (swap_check == 1); i++) {
|
||||
swap_check = 0;
|
||||
for (int i = 0; (i < n) && (swap_check); i++) {
|
||||
swap_check = false;
|
||||
for (int j = 0; j < n - 1 - i; j++) {
|
||||
if (numbers[j] > numbers[j + 1]) {
|
||||
swap_check = 1;
|
||||
swap_check = true;
|
||||
std::swap(numbers[j],
|
||||
numbers[j + 1]); // by changing swap location.
|
||||
// I mean, j. If the number is
|
||||
|
@ -9,28 +9,28 @@
|
||||
|
||||
// Iterative Version
|
||||
|
||||
void CocktailSelectionSort(std::vector<int> &vec, int low, int high) {
|
||||
void CocktailSelectionSort(std::vector<int> *vec, int low, int high) {
|
||||
while (low <= high) {
|
||||
int minimum = vec[low];
|
||||
int minimum = (*vec)[low];
|
||||
int minimumindex = low;
|
||||
int maximum = vec[high];
|
||||
int maximum = (*vec)[high];
|
||||
int maximumindex = high;
|
||||
|
||||
for (int i = low; i <= high; i++) {
|
||||
if (vec[i] >= maximum) {
|
||||
maximum = vec[i];
|
||||
if ((*vec)[i] >= maximum) {
|
||||
maximum = (*vec)[i];
|
||||
maximumindex = i;
|
||||
}
|
||||
if (vec[i] <= minimum) {
|
||||
minimum = vec[i];
|
||||
if ((*vec)[i] <= minimum) {
|
||||
minimum = (*vec)[i];
|
||||
minimumindex = i;
|
||||
}
|
||||
}
|
||||
if (low != maximumindex || high != minimumindex) {
|
||||
std::swap(vec[low], vec[minimumindex]);
|
||||
std::swap(vec[high], vec[maximumindex]);
|
||||
std::swap((*vec)[low], (*vec)[minimumindex]);
|
||||
std::swap((*vec)[high], (*vec)[maximumindex]);
|
||||
} else {
|
||||
std::swap(vec[low], vec[high]);
|
||||
std::swap((*vec)[low], (*vec)[high]);
|
||||
}
|
||||
|
||||
low++;
|
||||
@ -40,29 +40,29 @@ void CocktailSelectionSort(std::vector<int> &vec, int low, int high) {
|
||||
|
||||
// Recursive Version
|
||||
|
||||
void CocktailSelectionSort_v2(std::vector<int> &vec, int low, int high) {
|
||||
void CocktailSelectionSort_v2(std::vector<int> *vec, int low, int high) {
|
||||
if (low >= high) return;
|
||||
|
||||
int minimum = vec[low];
|
||||
int minimum = (*vec)[low];
|
||||
int minimumindex = low;
|
||||
int maximum = vec[high];
|
||||
int maximum = (*vec)[high];
|
||||
int maximumindex = high;
|
||||
|
||||
for (int i = low; i <= high; i++) {
|
||||
if (vec[i] >= maximum) {
|
||||
maximum = vec[i];
|
||||
if ((*vec)[i] >= maximum) {
|
||||
maximum = (*vec)[i];
|
||||
maximumindex = i;
|
||||
}
|
||||
if (vec[i] <= minimum) {
|
||||
minimum = vec[i];
|
||||
if ((*vec)[i] <= minimum) {
|
||||
minimum = (*vec)[i];
|
||||
minimumindex = i;
|
||||
}
|
||||
}
|
||||
if (low != maximumindex || high != minimumindex) {
|
||||
std::swap(vec[low], vec[minimumindex]);
|
||||
std::swap(vec[high], vec[maximumindex]);
|
||||
std::swap((*vec)[low], (*vec)[minimumindex]);
|
||||
std::swap((*vec)[high], (*vec)[maximumindex]);
|
||||
} else {
|
||||
std::swap(vec[low], vec[high]);
|
||||
std::swap((*vec)[low], (*vec)[high]);
|
||||
}
|
||||
|
||||
CocktailSelectionSort(vec, low + 1, high - 1);
|
||||
@ -85,9 +85,9 @@ int main() {
|
||||
std::cin >> method;
|
||||
|
||||
if (method == 0) {
|
||||
CocktailSelectionSort(v, 0, n - 1);
|
||||
CocktailSelectionSort(&v, 0, n - 1);
|
||||
} else if (method == 1) {
|
||||
CocktailSelectionSort_v2(v, 0, n - 1);
|
||||
CocktailSelectionSort_v2(&v, 0, n - 1);
|
||||
} else {
|
||||
std::cerr << "Unknown method" << std::endl;
|
||||
return -1;
|
||||
|
Loading…
Reference in New Issue
Block a user