Download presentation
Presentation is loading. Please wait.
Published byMaryann Bond Modified over 9 years ago
1
Starting Out with C++, 3 rd Edition 1 Sorting Arrays
2
Starting Out with C++, 3 rd Edition 2 8.3 Focus on Software Engineering: Introduction to Sorting Algorithms Sorting algorithms are used to arrange random data into some order
3
Starting Out with C++, 3 rd Edition 3 The Bubble Sort An easy way to arrange data in ascending or descending order. Pseudocode: Do Set count variable to 0 For count is set to each subscript in Array from 0 to the next-to-last subscript If array[count] is greater than array[count+1] swap them set swap flag to true end if End for While any elements have been swapped.
4
Starting Out with C++, 3 rd Edition Bubble Sort Visual Presentation 4
5
Starting Out with C++, 3 rd Edition 5 Program 8-4 // This program uses the bubble sort algorithm to sort an // array in ascending order. #include using namespace std; // Function prototypes void sortArray(int [], int); void showArray(int [], int); int main() { int values[4] = {4,5,1,2}; cout << "The unsorted values are:\n"; showArray(values, 4); sortArray(values, 4); cout << "The sorted values are:\n"; showArray(values, 4); system("pause"); return 0; }
6
Starting Out with C++, 3 rd Edition 6 Program continues // Definition of function showArray. // This function displays the contents of array. elems is the // number of elements. void showArray(int array[], int elems) { for (int count = 0; count < elems; count++) { cout << array[count] << " "; } cout << endl; }
7
Starting Out with C++, 3 rd Edition 7 Program continues // Definition of function sortArray. This function performs an ascending // order bubble sort on Array. elems is the number of elements in the array. void sortArray(int array[], int elems) { int swap=1, temp; do { swap = 0; for (int count = 0; count < (elems - 1); count++) { if (array[count] > array[count + 1]) { temp = array[count]; array[count] = array[count + 1]; array[count + 1] = temp; swap = 1; } } while (swap != 0); }
8
Starting Out with C++, 3 rd Edition 8 Program Output The unsorted values are: 4 5 1 2 The sorted values are: 1 2 4 5
9
Starting Out with C++, 3 rd Edition 9 The Selection Sort The bubble sort is inefficient for large arrays because items only move by one element at a time. The selection sort moves items immediately to their final position in the array so it makes fewer exchanges.
10
Starting Out with C++, 3 rd Edition 10 Selection Sort Pseudocode: For Start is set to each subscript in Array from 0 through the next-to-last subscript Set Index variable to Start Set minIndex variable to Start Set minValue variable to array[Start] For Index is set to each subscript in Array from Start+1 through the next-to-last subscript If array[Index] is less than minValue Set minValue to array[Index] Set minIndex to Index End if Increment Index End For Set array[minIndex] to array[Start] Set array[Start] to minValue End For
11
Starting Out with C++, 3 rd Edition Selection Sort Visual Presentation 11
12
Starting Out with C++, 3 rd Edition Activity: Make a program using selection sort algorithm to sort an arrays. Note: You must enter 10 unsorted values of an array then display the sorted value of an arrays. 12
13
Starting Out with C++, 3 rd Edition Thank you!!! 13
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.