1 / 11

Intro to CS – Honors I Basic Sorting

Intro to CS – Honors I Basic Sorting. Georgios Portokalidis gportoka@stevens.edu. Sorting. Arranging a collection of items into a particular order If we are talking about integers a[0] ≤ a[1] ≤ a[2] ≤ ... ≤ a[ a.length - 1 ] The simplest algorithm in pseudocode

habib
Download Presentation

Intro to CS – Honors I Basic Sorting

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. Intro to CS – Honors IBasic Sorting Georgios Portokalidis gportoka@stevens.edu

  2. Sorting • Arranging a collection of items into a particular order • If we are talking about integers • a[0] ≤ a[1] ≤ a[2] ≤ ... ≤ a[a.length - 1] • The simplest algorithm in pseudocode • for (index = 0; index < a.length; index++) • Place the (index + 1)thsmallest element in a[index] • Can you expand this algorithm? What considerations should you take into account?

  3. Selection Sort • Some requirements • We want the algorithm to use only this one array a • There are other algorithms we could use, if we could use more memory • How can we move array elements under these conditions? • We will swap elements • All sorting algorithms that swap elements are called interchange sorting algorithm

  4. Find the smallest element and move it to its rightful place Can you write an algorithm in pseudocode to implement this?

  5. Pseudocode for SelectionSort for (index = 0; index <a.length − 1; index++) { // Place the correct value in a[index]: indexOfNextSmallest= the index of the smallest value among a[index], a[index+1],..., a[a.length- 1] Interchange the values of a[index] and a[indexOfNextSmallest]. //Assertion: a[0] <= a[1] <= ... <= a[index] and these //are the smallest of the original array elements. //The remaining positions contain the rest of the //original array elements. }

  6. /** Precondition: i and j are valid indices for the array a. Postcondition: Values of a[i] and a[j] have been interchanged. */ private static void interchange(inti, int j, int[] a) { inttemp = a[i]; a[i] = a[j]; a[j] = temp; //original value of a[i] } public static void selectionSort(int[] anArray) { for (int index = 0; index < anArray.length − 1; index++) { // Place the correct value in anArray[index] intindexOfNextSmallest = getIndexOfSmallest(index, anArray); interchange(index, indexOfNextSmallest, anArray); } } private static intgetIndexOfSmallest(intstartIndex, int[] a) { intmin = a[startIndex]; intindexOfMin = startIndex; for (int index = startIndex + 1; index < a.length; index++) { if (a[index] < min) { min = a[index]; indexOfMin= index; //min is smallest of a[startIndex] through a[index] } } return indexOfMin; }

  7. Selection Sort in Action

  8. Why Use Selection Sort • It is simple! • Not very efficient • Simple implies it is easier to implement and less prone to errors

  9. Complexity • What is the worst-case scenario? • What is the maximum number of comparisons that selection sort may require? • For example to swap the first element you require (n – 1) comparisons • (n − 1) + (n − 2) + ... + 2 + 1 = n(n − 1) / 2 ≈ n2 • Arithmetic progression • This is indicated as O(n2)

  10. Searching Arrays • What is the simplest way to look for an item in an array? • You can sequentially check each item • However, sorted arrays enable other algorithms • Example: binary search

More Related