1 / 13

Sorting Arrays

Sorting Arrays. 8.3 Focus on Software Engineering: Introduction to Sorting Algorithms. Sorting algorithms are used to arrange random data into some order. The Bubble Sort. An easy way to arrange data in ascending or descending order. Pseudocode: Do Set count variable to 0

morgan
Download Presentation

Sorting Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sorting Arrays

  2. 8.3 Focus on Software Engineering: Introduction to Sorting Algorithms • Sorting algorithms are used to arrange random data into some order

  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. Bubble Sort Visual Presentation

  5. Program 8-4 // This program uses the bubble sort algorithm to sort an // array in ascending order. #include <iostream> 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. 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. 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. Program Output The unsorted values are: 4 5 1 2 The sorted values are: 1 2 4 5

  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. 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. Selection Sort Visual Presentation

  12. 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.

  13. Thank you!!! 

More Related