Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS

Similar presentations


Presentation on theme: "Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS"— Presentation transcript:

1 Intro to CS – Honors I Basic 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)th smallest 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(int i, int j, int[] a) { int temp = a[i]; a[i] = a[j]; a[j] = temp; //original value of a[i] } private static int getIndexOfSmallest(int startIndex, int[] a) { int min = a[startIndex]; int indexOfMin = 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; } public static void selectionSort(int[] anArray) { for (int index = 0; index < anArray.length − 1; index++) { // Place the correct value in anArray[index] int indexOfNextSmallest = getIndexOfSmallest(index, anArray); interchange(index, indexOfNextSmallest, anArray); }

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 ≈ n 2 ◦Arithmetic progression This is indicated as O(n 2 )

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

11


Download ppt "Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS"

Similar presentations


Ads by Google