From 77e781f709308a393b8e0ab11ac2f6b5dec15a68 Mon Sep 17 00:00:00 2001 From: Krishna Vedala Date: Tue, 26 May 2020 12:15:12 -0400 Subject: [PATCH] cpplint and msvc fixes for - sorting (cherry picked from commit 0d8e015d15b87ea188b75f1f70b7be614cdf86cd) --- sorting/bead_sort.cpp | 4 +-- sorting/bubble_sort.cpp | 8 +++--- sorting/cocktail_selection_sort.cpp | 44 ++++++++++++++--------------- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/sorting/bead_sort.cpp b/sorting/bead_sort.cpp index d34706073..a1a54c1e0 100644 --- a/sorting/bead_sort.cpp +++ b/sorting/bead_sort.cpp @@ -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; } diff --git a/sorting/bubble_sort.cpp b/sorting/bubble_sort.cpp index 794f65198..c43e425fc 100644 --- a/sorting/bubble_sort.cpp +++ b/sorting/bubble_sort.cpp @@ -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 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 diff --git a/sorting/cocktail_selection_sort.cpp b/sorting/cocktail_selection_sort.cpp index a9abd86d5..ab6229996 100644 --- a/sorting/cocktail_selection_sort.cpp +++ b/sorting/cocktail_selection_sort.cpp @@ -9,28 +9,28 @@ // Iterative Version -void CocktailSelectionSort(std::vector &vec, int low, int high) { +void CocktailSelectionSort(std::vector *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 &vec, int low, int high) { // Recursive Version -void CocktailSelectionSort_v2(std::vector &vec, int low, int high) { +void CocktailSelectionSort_v2(std::vector *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;