Lecture17 - Simple Sorting Algorithms EECS 233-2- Basics of...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Simple Sorting Algorithms EECS 233-2- Basics of Sorting Array data structure Ground rules: sort the values in increasing order sort “in place”, using only a small amount of additional storage Terminology: position: one of the memory locations in the array element: one of the data items stored in the array element i: the element at position i Goal: minimize the number of comparisons C and the number of moves M needed to sort the array. comparison = compare the keys of two elements move = copying an element from one position to another-3- Defning Methods For Sorting In C++, we can defne our sorting methods outside oF a class. In Java, we can put them in a Sort class that is simply a collection oF methods like Java ’ s Math class. public class Sort { static void bubbleSort(int arr) { ... } static void insertionSort(int arr) { ... } ... } We never create Sort objects. All oF the methods in the class must be static Outside the class, we invoke them using the class name: e.g., Sort.bubbleSort(arr);-4- Method 1: Selection Sort Basic idea: consider the positions in the array from left to right for each position, Fnd the element that belongs there and put it in place by swapping it with the element that ’ s currently there An example:-5- Selecting An Element When we consider position i, the elements in positions 0 through i – 1 are already in their fnal positions. To select an element For position i, consider elements i, i+1,i+2,…,arr.length – 1, and keep track oF indexMin, the index oF the smallest element seen thus Far when we fnish this pass, indexMin is the index oF the element that belongs in position i. swap arr[i] and arr[indexMin]:-6- Implementation of Selection Sort The sort method is very simple: static void selectionSort(int arr, int length) { for (int i = 0; i < length – 1; i++) { int j = indexSmallest(arr, i, length – 1); swap(arr, i, j); } } It uses a helper method to fnd the index oF the smallest element: static int indexSmallest(int arr, int lower, int upper) { int indexMin = lower; for (int i = lower+1; i <= upper; i++) if (arr[i] < arr[indexMin]) indexMin = i; return indexMin; }-7- Running Time Analysis Input size n: the # of elements in the array Time metrics: C(n) = number of comparisons M(n) = number of moves-8- Number of Comparisons To sort n elements, selection sort performs n - 1 passes: on 1st pass, it performs n - 1 comparisons to Fnd indexSmallest...
View Full Document

This note was uploaded on 04/07/2008 for the course EECS 233 taught by Professor Rabinovich during the Spring '08 term at Case Western.

Page1 / 22

Lecture17 - Simple Sorting Algorithms EECS 233-2- Basics of...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online