From 4ce04ab2724f15eb598bf7522ac9339aa9b91b82 Mon Sep 17 00:00:00 2001 From: Yuval Zach Date: Mon, 1 Oct 2018 22:48:07 -0700 Subject: [PATCH] Unify code style and resolve numerous compile error in sorting --- sorting/bubble_sort.cpp | 49 +++++++------ .../{insertionSort.cpp => insertion_sort.cpp} | 37 +++++----- sorting/merge_sort.cpp | 47 +++++++------ sorting/quick_sort.cpp | 69 ++++++++++--------- 4 files changed, 107 insertions(+), 95 deletions(-) rename sorting/{insertionSort.cpp => insertion_sort.cpp} (50%) diff --git a/sorting/bubble_sort.cpp b/sorting/bubble_sort.cpp index 3fb6af54..d133dbfb 100644 --- a/sorting/bubble_sort.cpp +++ b/sorting/bubble_sort.cpp @@ -13,34 +13,39 @@ void swap(int *x, int *y) } // Implement bubble sort -void bubbleSort(int arr[], int n) +void bubble_sort(int arr[], size_t n) { - int i, j; - for (i = 0; i < n-1; i++) - // last i elements are already in place - for (j = 0; j < n-i-1; j++) - if (arr[j] > arr[j+1]) - swap(&arr[j], &arr[j+1]); + for (size_t i = 0; i < n - 1; i++) + { + // last i elements are already in place + for (size_t j = 0; j < n-i-1; j++) + { + if (arr[j] > arr[j + 1]) + { + swap(&arr[j], &arr[j + 1]); + } + } + } } -// Function to print elements -void printArray(int arr[], int size) -{ - int i; - for (i=0; i < size; i++) - printf("%d ", arr[i]); - printf("\n"); -} +// A utility function to print an array of size n +void print_array(int arr[], int n) +{ + for (size_t i = 0; i < n; i++) + { + std::cout << arr[i] << " "; + } + std::cout << std::endl; +} -// test int main() { int arr[] = {46, 24, 33, 10, 2, 81, 50}; int n = sizeof(arr)/sizeof(arr[0]); - printf("Unsorted array: \n"); - printArray(arr, n); - bubbleSort(arr, n); - printf("Sorted array: \n"); - printArray(arr, n); - return 0; + std::cout << "Unsorted array:" << std::endl; + print_array(arr, n); + bubble_sort(arr, n); + std::cout << "Sorted array:" << std::endl; + print_array(arr, n); + return 0; } diff --git a/sorting/insertionSort.cpp b/sorting/insertion_sort.cpp similarity index 50% rename from sorting/insertionSort.cpp rename to sorting/insertion_sort.cpp index 6543970d..b6cec71d 100644 --- a/sorting/insertionSort.cpp +++ b/sorting/insertion_sort.cpp @@ -6,33 +6,35 @@ #include /* Function to sort an array using insertion sort*/ -void insertionSort(int arr[], int n) +void insertion_sort(int arr[], int n) { - int i, key, j; - for (i = 1; i < n; i++) + int key; + size_t j; + for (size_t i = 1; i < n; i++) { key = arr[i]; - j = i-1; + j = i - 1; /* Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position */ while (j >= 0 && arr[j] > key) { - arr[j+1] = arr[j]; - j = j-1; + arr[j + 1] = arr[j]; + j--; } - arr[j+1] = key; + arr[j + 1] = key; } } // A utility function to print an array of size n -void printArray(int arr[], int n) +void print_array(int arr[], int n) { - int i; - for (i=0; i < n; i++) - printf("%d ", arr[i]); - printf("\n"); + for (size_t i = 0; i < n; i++) + { + std::cout << arr[i] << " "; + } + std::cout << std::endl; } @@ -41,10 +43,11 @@ void printArray(int arr[], int n) int main() { int arr[] = {12, 11, 13, 5, 6}; - int n = sizeof(arr)/sizeof(arr[0]); - - insertionSort(arr, n); - printArray(arr, n); - + size_t n = sizeof(arr) / sizeof(arr[0]); + std::cout << "Unsorted array:" << std::endl; + print_array(arr, n); + insertion_sort(arr, n); + std::cout << "Sorted array:" << std::endl; + print_array(arr, n); return 0; } diff --git a/sorting/merge_sort.cpp b/sorting/merge_sort.cpp index 4c86dd3c..3865c2b9 100644 --- a/sorting/merge_sort.cpp +++ b/sorting/merge_sort.cpp @@ -5,8 +5,7 @@ // abraham@abranhe.com //**************************************** -#include -#include +#include // Merge the two half into a sorted data. void merge(int arr[], int l, int m, int r) @@ -16,8 +15,8 @@ void merge(int arr[], int l, int m, int r) int n2 = r - m; /* create temp arrays */ - int L[n1], R[n2]; - + int* L = new int[n1]; + int* R = new int[n2]; /* Copy data to temp arrays L[] and R[] */ for (i = 0; i < n1; i++) L[i] = arr[l + i]; @@ -60,48 +59,48 @@ void merge(int arr[], int l, int m, int r) j++; k++; } + delete[] L; + delete[] R; } /* l is for left index and r is right index of the sub-array of arr to be sorted */ -void mergeSort(int arr[], int l, int r) +void merge_sort(int arr[], int l, int r) { if (l < r) { // Same as (l+r)/2, but avoids overflow for // large l and h - int m = l+(r-l)/2; + int m = l + (r - l) / 2; // Sort first and second halves - mergeSort(arr, l, m); - mergeSort(arr, m+1, r); + merge_sort(arr, l, m); + merge_sort(arr, m + 1, r); merge(arr, l, m, r); } } /* UTILITY FUNCTIONS */ -/* Function to print an array */ -void printArray(int A[], int size) -{ - int i; - for (i=0; i < size; i++) - printf("%d ", A[i]); - printf("\n"); -} +// A utility function to print an array of size n +void print_array(int arr[], int n) +{ + for (size_t i = 0; i < n; i++) + { + std::cout << arr[i] << " "; + } + std::cout << std::endl; +} /* Driver program to test above functions */ int main() { int arr[] = {12, 11, 13, 5, 6, 7}; int arr_size = sizeof(arr)/sizeof(arr[0]); - - printf("Given array is \n"); - printArray(arr, arr_size); - - mergeSort(arr, 0, arr_size - 1); - - printf("\nSorted array is \n"); - printArray(arr, arr_size); + std::cout << "Unsorted array:" << std::endl; + print_array(arr, arr_size); + merge_sort(arr, 0, arr_size - 1); + std::cout << "Sorted array:" << std::endl; + print_array(arr, arr_size); return 0; } diff --git a/sorting/quick_sort.cpp b/sorting/quick_sort.cpp index 3e72d734..91407403 100644 --- a/sorting/quick_sort.cpp +++ b/sorting/quick_sort.cpp @@ -1,51 +1,56 @@ #include #include -using namespace std; -void quickSort(vector&,int,int); -int partition(vector&, int,int); +void quick_sort(std::vector&, size_t, size_t); +int partition(std::vector&, size_t, size_t); -int main() -{ - vector A = {10,9,8,7,6,5,4,3,2,1}; - int start = 0; - int end = (int)A.size(); - cout << "Before:" << endl; - for(auto value : A) - cout << value <<" "; - cout << endl; - quickSort(A, start, end); - cout << "After: " << endl; - for(auto value : A) - cout << value <<" "; - cout << endl; -} - -void quickSort(vector& A, int start,int end) +void quick_sort(std::vector& arr, size_t start, size_t end) { - int pivot; if(start < end) { - pivot=partition(A, start, end); - quickSort(A, start, pivot); - quickSort(A, pivot+1, end); + int pivot = partition(arr, start, end); + quick_sort(arr, start, pivot); + quick_sort(arr, pivot + 1, end); } } -int partition(vector& A, int start,int end) +int partition(std::vector& arr, size_t start, size_t end) { - int x = A[start]; + int x = arr[start]; int i = start; - int j; - for(j = start+1; j < end; j++) + for(size_t j = start + 1; j < end; j++) { - if(A[j]<=x) - { + if(arr[j]<=x) + { i=i+1; - swap(A[i],A[j]); + std::swap(arr[i], arr[j]); } } - swap(A[i],A[start]); + std::swap(arr[i], arr[start]); return i; } + + +void print_vector(std::vector& arr) +{ + for (size_t i = 0; i < arr.size(); i++) + { + std::cout << arr[i] << " "; + } + std::cout << std::endl; +} + + +int main() +{ + std::vector arr = {10,9,8,7,6,5,4,3,2,1}; + int start = 0; + int end = arr.size(); + std::cout << "Unsorted array:" << std::endl; + print_vector(arr); + quick_sort(arr, start, end); + std::cout << "Sorted array:" << std::endl; + print_vector(arr); +} +