diff --git a/Chapter-8-Searching-and-Sorting-Arrays/8.3 Focus on Software Engineering: Introduction to Sorting Algorithms/8-4a-9th-ed.cpp b/Chapter-8-Searching-and-Sorting-Arrays/8.3 Focus on Software Engineering: Introduction to Sorting Algorithms/8-4a-9th-ed.cpp index 099fdcd..306adfd 100644 --- a/Chapter-8-Searching-and-Sorting-Arrays/8.3 Focus on Software Engineering: Introduction to Sorting Algorithms/8-4a-9th-ed.cpp +++ b/Chapter-8-Searching-and-Sorting-Arrays/8.3 Focus on Software Engineering: Introduction to Sorting Algorithms/8-4a-9th-ed.cpp @@ -31,16 +31,22 @@ void bubbleSort(int array[], int size) index; for (maxElement = size - 1; maxElement > 0; maxElement--) - { + // { + // for (int i = 0; i < size; i++) + // { + // cout << array[i] << " "; + // } + // cout << endl; + for (index = 0; index < maxElement; index++) { if (array[index] > array[index + 1]) { swap(array[index], array[index + 1]); + } } - } } diff --git a/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/04.cpp b/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/04.cpp index 267f2e0..40827b4 100644 --- a/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/04.cpp +++ b/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/04.cpp @@ -11,31 +11,31 @@ using namespace std; int inputValidation(int); -void selectionSort(int [], const int); -bool binarySearch(const int[], const int, const int); +bool binarySearch(const int[], + const int, + const int); +void selectionSort(int[], int); +void swap(int &, int &); int main() { const int NUMBER_OF_ACCOUNTS = 18; - int account_numbers[NUMBER_OF_ACCOUNTS] = { 5658845, 4520125, 7895122, 8777541, 8451277, 1302850, - 8080152, 4562555, 5552012, 5050552, 7825877, 1250255, - 1005231, 6545231, 3852085, 7576651, 7881200, 4581002 }; + int account_numbers[NUMBER_OF_ACCOUNTS] = { 5658845, 4520125, 7895122, + 8777541, 8451277, 1302850, + 8080152, 4562555, 5552012, + 5050552, 7825877, 1250255, + 1005231, 6545231, 3852085, + 7576651, 7881200, 4581002 }; cout << "Enter an account #: " << endl; int user_account_number = inputValidation(user_account_number); - // for (int i = 0; i < NUMBER_OF_ACCOUNTS; i++) - // cout << account_numbers[i] << ", "; - // cout << endl; - selectionSort(account_numbers, NUMBER_OF_ACCOUNTS); - // for (int i = 0; i < NUMBER_OF_ACCOUNTS; i++) - // cout << account_numbers[i] << ", "; - // cout << endl; - - bool user_account_number_is_valid = binarySearch(account_numbers, NUMBER_OF_ACCOUNTS, user_account_number); + bool user_account_number_is_valid = binarySearch(account_numbers, + NUMBER_OF_ACCOUNTS, + user_account_number); if (user_account_number_is_valid) cout << "Your account number is valid." << endl; @@ -56,19 +56,19 @@ int inputValidation(int user_number) return user_number; } -void selectionSort(int array[], const int ARRAY_SIZE) +void selectionSort(int array[], int ARRAY_SIZE) { int min_index, min_value; - for (int start_index = 0; start_index < (ARRAY_SIZE - 1); start_index++) + for (int start_index = 0; start_index < (ARRAY_SIZE - 1); start_index++) // 0 - 17 { min_index = start_index; min_value = array[start_index]; - for (int index = start_index + 1; index < ARRAY_SIZE; index ++) + for (int index = start_index + 1; index < ARRAY_SIZE; index++) // 1 - 18 { - if (array[index] < min_value) + if (array[index] < min_value) // Ascending { min_value = array[index]; min_index = index; @@ -78,33 +78,33 @@ void selectionSort(int array[], const int ARRAY_SIZE) } } -void swap(int &a, int &b) -{ - int temp = a; - a = b; - b = temp; -} - bool binarySearch(const int array[], const int ARRAY_SIZE, const int user_number) { - int first_index = 0, middle_index, - last_index = ARRAY_SIZE - 1; + last_index = ARRAY_SIZE - 1; // 18 - 1 = 17 bool found = false; while (!found && first_index <= last_index) { - middle_index = (first_index + last_index) / 2; + middle_index = (first_index + last_index) / 2; // 8 - if (array[middle_index] == user_number) + if (array[middle_index] == user_number) // Mid point found = true; - else if (array[middle_index] > user_number) - last_index = middle_index - 1; - else + else if (array[middle_index] > user_number) // Lower half + last_index = middle_index - 1; // 8 - 1 = 7 + else // Upper Half first_index = middle_index + 1; + } return found; -} \ No newline at end of file +} + +void swap(int &a, int &b) +{ + int temp = a; + a = b; + b = temp; +} diff --git a/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/05.cpp b/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/05.cpp index a8fd30f..a9edb28 100644 --- a/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/05.cpp +++ b/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/05.cpp @@ -31,23 +31,15 @@ int main() { double rainfall[ARRAY_SIZE]; int months[ARRAY_SIZE] = { 1, 2, 3, 4, 5, 6, - 7, 8, 9, 10, 11, 12 }; - string month_names[ARRAY_SIZE] = { "January", "February", "March", "April", "May", "June", - "July", "August", "September", "October", "November", "December" }; + 7, 8, 9, 10, 11, 12 }; + string month_names[ARRAY_SIZE] = { "January", "February", "March", + "April" , "May" , "June", + "July" , "August" , "September", + "October", "November", "December" }; cout << "Enter average rainfall for each month: " << endl; getValues(rainfall); - selectionSort(rainfall, months, ARRAY_SIZE); - - cout << endl; - for (int i = 0; i < ARRAY_SIZE; i++) - cout << month_names[months[i] - 1] << "(" - << months[i] << ")" << ": " - << rainfall[i] - << endl; - cout << endl; - double total_rainfall = calculateTotal(rainfall); double avarage_rainfall = average(total_rainfall); @@ -57,6 +49,16 @@ int main() vectorlowest_months; findLowestMonths(rainfall, lowest_months); + selectionSort(rainfall, months, ARRAY_SIZE); + + cout << endl; + for (int i = 0; i < ARRAY_SIZE; i++) + cout << month_names[months[i] - 1] << "(" + << months[i] << ")" << ": " + << rainfall[i] + << endl; + cout << endl; + display(total_rainfall, avarage_rainfall, highest_months, lowest_months); return 0; @@ -229,4 +231,4 @@ void swap(int &a, int &b) int temp = a; a = b; b = temp; -} \ No newline at end of file +} diff --git a/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/09.cpp b/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/09.cpp index 666c83d..8b41f40 100644 --- a/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/09.cpp +++ b/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/09.cpp @@ -17,12 +17,11 @@ using namespace std; void displayArray(int[], int, string); void bubbleSort(int[], int, int &); -void selectionSort(int[], int, int &); void swap(int &, int &); +void selectionSort(int[], int, int &); int main() { - // 1. two identical arrays of at least 20 integers. const int ARRAY_SIZE = 20; int array_1[ARRAY_SIZE] = {6, 5, 90, 33, 45, @@ -35,75 +34,63 @@ int main() 55, 60, 2, 9, 11, 91, 65, 45, 8, 3}; - // 2. call a function that uses the bubble sort - // algorithm to sort one of the arrays in ascending order. - // The function should keep a count of the number of - // exchanges it makes. int number_of_exchanges = 0; - displayArray(array_1, ARRAY_SIZE, "array_1 UNSORTED:\n----------------"); + displayArray(array_1, ARRAY_SIZE, "UNSORTED-------------------|"); bubbleSort(array_1, ARRAY_SIZE, number_of_exchanges); - displayArray(array_1, ARRAY_SIZE, "\narray_1 SORTED:\n----------------"); + displayArray(array_1, ARRAY_SIZE, "SORTED-------------------|"); cout << "\nNumber of exchanges = " << number_of_exchanges << endl; - // 3. call a function that uses the selection sort - // algorithm to sort the other array. - // It should also keep count of the number of - // exchanges it makes. number_of_exchanges = 0; - displayArray(array_2, ARRAY_SIZE, "\narray_2 SORTED:\n-----------------"); + displayArray(array_2, ARRAY_SIZE, "UNSORTED-------------------|"); selectionSort(array_2, ARRAY_SIZE, number_of_exchanges); - displayArray(array_2, ARRAY_SIZE, "\narray_2 SORTED:\n-----------------"); + displayArray(array_2, ARRAY_SIZE, "SORTED-------------------|"); cout << "\nNumber of exchanges = " << number_of_exchanges << endl << endl; - // 4. Display these values on the screen. return 0; } -void displayArray(int array[], int array_size, string prompt) +void displayArray(int array[], int ARRAY_SIZE, string prompt) { cout << prompt << endl; - for (int i = 0; i < array_size; i++) + for (int i = 0; i < ARRAY_SIZE; i++) cout << array[i] << ", "; - cout << endl; + cout << endl; } -void bubbleSort(int array[], int array_size, int &number_of_exchanges) +void bubbleSort(int array[], int ARRAY_SIZE, int &number_of_exchanges) { int max_element, index; - - for (max_element = array_size - 1; max_element > 0; max_element--) + // <--------- + for (max_element = ARRAY_SIZE - 1; max_element > 0; max_element--) { + // ---------> for (index = 0; index < max_element; index++) { + // array[0] > array[1]; if (array[index] > array[index + 1]) { swap(array[index], array[index + 1]); number_of_exchanges++; } - - } + } } } -void swap(int &a, int &b) -{ - int temp = a; - a = b; - b = temp; -} - -void selectionSort(int array[], int array_size, int &number_of_exchanges) +void selectionSort(int array[], int ARRAY_SIZE, int &number_of_exchanges) { int min_index, min_value; - for (int start_index = 0; start_index < (array_size - 1); start_index++) + // 0 -> 18 + for (int start_index = 0; start_index < (ARRAY_SIZE - 1); start_index++) { min_index = start_index; min_value = array[start_index]; - for (int index = start_index + 1; index < array_size; index++) + // 1 -> 19 + // array[0] array[1] + for (int index = start_index + 1; index < ARRAY_SIZE; index++) { if (array[index] < min_value) { @@ -114,4 +101,12 @@ void selectionSort(int array[], int array_size, int &number_of_exchanges) } swap(array[min_index], array[start_index]); } + } + +void swap(int &a, int &b) +{ + int temp = a; + a = b; + b = temp; +} \ No newline at end of file diff --git a/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/10.cpp b/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/10.cpp index 576e5ee..ff75856 100644 --- a/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/10.cpp +++ b/Chapter-8-Searching-and-Sorting-Arrays/Review Questions and Exercises/Programming Challenges/10.cpp @@ -14,23 +14,105 @@ * **/ #include +#include + using namespace std; +void displayArray(int[], int, string); +void bubbleSort(int[], int); +void swap(int &, int &); +void selectionSort(int[], int); + int main() { - // 1. two identical arrays of just eight integers. - // 2. display the contents of the first array, - - // 3. then call a function - // to sort the array using an ascending order bubble - // sort modified to print out the array contents after - // each pass of the sort. - - // 4. Next, the program should - // display the contents of the second array, then - // call a function to sort the array using an ascending - // order selection sort modified to print out the array - // contents after each pass of the sort. + const int ARRAY_SIZE = 8; + int array_1[ARRAY_SIZE] = {8, 100, 55, 2, 99, 12, 1, 0}; + int array_2[ARRAY_SIZE] = {8, 100, 55, 2, 99, 12, 1, 0}; + + cout << "<-----BUBBLE SORT----->" << endl; + displayArray(array_1, ARRAY_SIZE, "Unsorted:-------------|"); + bubbleSort(array_1, ARRAY_SIZE); + displayArray(array_1, ARRAY_SIZE, "Sorted:---------------|"); + + //---------------------------------------------------------| + + cout << "\n<-----SELECTION SORT----->" << endl; + displayArray(array_2, ARRAY_SIZE, "Unsorted:-------------|"); + selectionSort(array_2, ARRAY_SIZE); + displayArray(array_2, ARRAY_SIZE, "\nSorted:---------------|"); + cout << endl; return 0; +} // End int main() + +void displayArray(int array[], int ARRAY_SIZE, string prompt) +{ + cout << prompt << endl; + for (int i = 0; i < ARRAY_SIZE; i++) + cout << array[i] << " "; + cout << endl; +} + +void bubbleSort(int array[], int ARRAY_SIZE) +{ + int max_element, + index, + pass = 0; + + // <-----------| 19 + for (max_element = ARRAY_SIZE - 1; max_element > 0; max_element--) + { + // 0 |-------------> + for (index = 0; index < max_element; index++) + { // 9, 2 + if (array[index] > array[index + 1]) + swap(array[index], array[index + 1]); + } + + cout << "\nPass #" << pass + 1 << ": --> "; + for (int i = 0; i < ARRAY_SIZE; i++) + cout << array[i] << " "; + + pass++; + } + cout << endl << endl; +} + +void swap(int &a, int &b) +{ + int temp = a; + a = b; + b = temp; +} + +void selectionSort(int array[], int ARRAY_SIZE) +{ + int min_index, + min_value, + pass = 0; + + // 0 -> 18 + for (int start_index = 0; start_index < (ARRAY_SIZE - 1); start_index++) + { + min_index = start_index; + min_value = array[start_index]; + + // 1 -> 19 + for (int index = start_index + 1; index < ARRAY_SIZE; index++) + { + if (array[index] < min_value) + { + min_index = index; + min_value = array[index]; + } + } + swap(array[min_index], array[start_index]); + + cout << "\nPass #" << (pass + 1) << ": --> "; + for (int i = 0; i < ARRAY_SIZE; i++) + cout << array[i] << " "; + + pass++; + } + cout << endl; } \ No newline at end of file