500 likes | 657 Views
Data Structures and Algorithms CSE 246. Fall-2012 Lecture#14. The Selection Sort Description.
E N D
Data Structures and AlgorithmsCSE 246 Fall-2012 Lecture#14 Muhammad Usman Arif
The Selection SortDescription The Selection Sort searches (linear search) all of the elements in a list until it finds the smallest element. It “swaps” this with the first element in the list. Next it finds the smallest of the remaining elements, and “swaps” it with the second element. Repeat this process until you compare only the last two elements in the list. Muhammad Usman Arif
The Selection SortAlgorithm • For each index position i • Find the smallest data value in the array from positions i through length - 1, where length is the number of data values stored. • Exchange (swap) the smallest value with the value at position i. Muhammad Usman Arif
A Selection Sort Example Smallest ? We start by searching for the smallest element in the List. Muhammad Usman Arif
A Selection Sort Example Smallest ? Muhammad Usman Arif
A Selection Sort Example Smallest ! Muhammad Usman Arif
A Selection Sort Example ! Smallest ? Muhammad Usman Arif
A Selection Sort Example ! Smallest ? Muhammad Usman Arif
A Selection Sort Example ! Smallest ? Muhammad Usman Arif
A Selection Sort Example Swap Muhammad Usman Arif
A Selection Sort Example Swapped Muhammad Usman Arif
A Selection Sort Example Smallest ? After the smallest element is in the first position, we continue searching with the second element and look for the next smallest element. Muhammad Usman Arif
A Selection Sort Example Smallest ! In this special case, the next smallest element is in the second position already. Swapping keeps it in this position. Muhammad Usman Arif
A Selection Sort Example Swapped Swapping keeps it in this position. Muhammad Usman Arif
A Selection Sort Example Smallest ? After the next smallest element is in the second position, we continue searching with the third element and look for the next smallest element. Muhammad Usman Arif
A Selection Sort Example Smallest ! Muhammad Usman Arif
A Selection Sort Example Swap Muhammad Usman Arif
A Selection Sort Example Swapped Muhammad Usman Arif
A Selection Sort Example Smallest ? Muhammad Usman Arif
A Selection Sort Example Smallest ? Muhammad Usman Arif
A Selection Sort Example Smallest ! Muhammad Usman Arif
A Selection Sort Example Swap Muhammad Usman Arif
A Selection Sort Example Swapped Muhammad Usman Arif
A Selection Sort Example The last two elements are in order, so no swap is necessary. Muhammad Usman Arif
What “Swapping” Means TEMP 6 Place the first element into the Temporary Variable. Muhammad Usman Arif
What “Swapping” Means TEMP 6 Replace the first element with the value of the smallest element. Muhammad Usman Arif
What “Swapping” Means TEMP 6 Replace the third element (in this example) with the Temporary Variable. Muhammad Usman Arif
Java Source Code: Selection Sort Muhammad Usman Arif
Big - O Notation Big - O notation is used to describe the efficiency of a search or sort. The actual time necessary to complete the sort varies according to the speed of your system.Big - O notation is an approximate mathematical formula to determine how many operations are necessary to perform the search or sort. The Big - O notation for the Selection Sort is O(n2), because it takes approximately n2 passes to sort the elements. Muhammad Usman Arif
Insertion Sort Description The insertion sort takes advantage an array’s partial ordering and is the most efficient sort to use when you know the array is already partially ordered. On the kth pass, the kth item should be inserted into its place among the first k items in the vector. After the kth pass (k starting at 1), the first k items of the vector should be in sorted order. This is like the way that people pick up playing cards and order them in their hands. When holding the first (k - 1) cards in order, a person will pick up the kth card and compare it with cards already held until its sorted spot is found. Muhammad Usman Arif
Insertion Sort Algorithm For each k from 1 to n - 1 (k is the index of vector element to insert) Set item_to_insert to v[k] Set j to k - 1 (j starts at k - 1 and is decremented until insertion position is found) While (insertion position not found) and (not beginning of vector) If item_to_insert < v[j] Move v[j] to index position j + 1 Decrement j by 1 Else The insertion position has been found item_to_insert should be positioned at index j + 1 Muhammad Usman Arif
Java Code For Insertion Sort Muhammad Usman Arif
Insertion Sort Example The Unsorted Vector: For each pass, the index j begins at the (k - 1)st item and moves that item to position j + 1 until we find the insertion point for what was originally the kth item. We start with k = 1 and set j = k-1 or 0 (zero) Muhammad Usman Arif
The First Pass K = 2 40 80 80 Insert 40 Insert 40, compare & move 80 40 80 32 32 32 84 84 84 61 61 61 item_to_insert 40 Muhammad Usman Arif
The Second Pass K = 3 40 40 40 32 Compare & move Insert 32 80 80 40 40 Insert 32, compare & move 32 80 80 80 84 84 84 84 61 61 61 61 item_to_insert 32 Muhammad Usman Arif
The Third Pass K = 4 32 40 80 Insert 84? compare & stop 84 61 item_to_insert 84 Muhammad Usman Arif
The Fourth Pass K = 5 32 32 32 32 Compare & stop Insert 61 40 40 40 40 80 80 80 61 Compare & move 84 84 Insert 61, compare & move 80 80 61 84 84 84 item_to_insert 61 Muhammad Usman Arif
What “Moving” Means item_to_insert 40 Place the second element into the variable item_to_insert. Muhammad Usman Arif
What “Moving” Means item_to_insert 40 Replace the second element with the value of the first element. Muhammad Usman Arif
What “Moving” Means item_to_insert 40 Replace the first element (in this example) with the variable item_to_insert. Muhammad Usman Arif
Big - O Notation Big - O notation is used to describe the efficiency of a search or sort. The actual time necessary to complete the sort varies according to the speed of your system.Big - O notation is an approximate mathematical formula to determine how many operations are necessary to perform the search or sort. The Big - O notation for the Insertion Sort is O(n2), because it takes approximately n2 passes to sort the “n” elements. Muhammad Usman Arif
Comparison of Quadratic Sorts • None good for large arrays! Muhammad Usman Arif
Problems with insertion sort • Suppose a small item is on the far right, where the large items should be. To move this small item to its proper place on the left, all the intervening items (between where it is and where it should be) must be shifted one space right. • This is close to N copies, just for one item. Not all the items must be moved a full N spaces, but the average item must be moved N/2 spaces, which takes N times N/2 shifts for a total of N 2/2 copies. Thus the performance of insertion sort is O(N2). Muhammad Usman Arif
Shell Sort: A Better Insertion Sort • Shell sort is a variant of insertion sort • Average performance: O(n3/2) or better • Divide and conquer approach to insertion sort • Sort many smaller sub-arrays using insertion sort • Sort progressively larger arrays • Finally sort the entire array • These arrays are elements separated by a gap • Start with large gap • Decrease the gap on each “pass” Muhammad Usman Arif
Shell Sort: The Varying Gap 4-Sort (0,4,8), (1,5,9), (2,6), and (3,7) Notice that, in this particular example, at the end of the 4-sort no item is more than 2 cells from where it would be if the array were completely sorted. The interval is then repeatedly reduced until it becomes 1. Muhammad Usman Arif
Shell Sort Algorithm • Set gap to (n-1)/3 • while gap > 0 • for each element from gap to end, by gap • Insert element in its gap-separated sub-array • if gap is 2, set it to 1 Muhammad Usman Arif
Shell Sort Algorithm: Inner Loop 3.1 set nextPos to position of element to insert 3.2 set nextVal to value of that element 3.3 while nextPos > gap and element at nextPos-gap is > nextVal 3.4 Shift element at nextPos-gap to nextPos 3.5 Decrement nextPos by gap 3.6 Insert nextVal at nextPos Muhammad Usman Arif
Shell Sort Code Muhammad Usman Arif
Shell Sort Code (2) Muhammad Usman Arif
Analysis of Shell Sort • Intuition: Reduces work by moving elements farther earlier • Performance depends on sequence of gap values • Sequence 2k-1, performance is O(n3/2) • We start with n/2 and repeatedly divide by 2.2 • Empirical results show this is O(n5/4) or O(n7/6) • No theoretical basis (proof) that this holds Muhammad Usman Arif