Lecture 40 Selection Sort Last time 1 Linear search 2 Binary search 3 Worst case time complexity 4 Selection sort Today 1 2 3 12 06 2006 Project 8 assigned Analyzing selection sort Trees and heaps CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Project 8 Assigned Project due 12 11 at 11pm Project is open Start before now Read entire assignment from beginning to end before starting to code Check out assignment now from CVS Follow the instructions exactly as much of grading is automated CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Recall from Last Time Sorting another basic operation in computer science Our version of the problem Given array ordering on base type of array Compute permutation reordering of array such that elements are in order Examples Ascending order Ordering is Array a is in ascending order if a 0 a 1 Descending order Ordering is Array a is in descending order if a 0 a 1 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 Selection Sort The selection sort idea for sorting Think of the array as consisting of two pieces Sorted part Unsorted part Elements in sorted part are all ordered less than the elements in unsorted part Initially sorted part is empty unsorted part is entire array To grow the sorted part Find smallest element in unsorted part Swap it with first element in unsorted part CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 Structure of Selection Sort Implementation Two auxiliary functions findMin a i returns index of least element in a i a i 1 a a length 1 swap a i j swaps a i a j selSort a does following for each i 0 n 2 n of elements in a Find position j of minimum value in a i a n 1 Swap a i a j CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Analyzing Selection Sort findMin findMin a i returns index of least element in a i a i 1 a a length 1 Code public static String int findMin String a int start String min a start Minimum value found so far int retVal start Index of minimum value for int i start 1 i a length i if min compareTo a i 0 New minimum min a i retVal i return retVal Complexity analysis Initialization two assignments For each loop iteration How many iterations One comparison Two assignments Depends on i n i 1 where n is number of elements in a Worst case i 0 So O 3 n i 1 2 O n i O n CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 5 Analyzing Selection Sort swap swap a i j swaps a i a j Code public static void swap String a int i int j String tmp a i a i a j a j tmp Complexity analysis Three assignments So complexity is O 3 O 1 constant time CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 Analyzing Selection Sort selSort Selection sort sorts a by repeatedly finding minimum in unsorted part and swapping Code public static void selSort String a int minIndex for int i 0 i a length 1 i minIndex findMin a i if minIndex i swap a i minIndex Complexity analysis Number of loop iteratons n 2 Complexity of iteration i One call to findMin O n One assignment One comparison One swap O 1 So O n 2 n 3 O n2 n 6 O n2 quadratic time CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 Other Quadratic Time Sorting Algorithms Bubble sort Insertion sort card sorting CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 There Are Better Sorting Algorithms O n log2n is possible We will study one such algorithm heapsort Explaining heapsort requires some background Binary trees Heaps CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 Binary Trees A data structure consisting of Nodes Edges root Every node has one parent node pointing to it except single root node which has no parent Every node has at most two children Nodes with no children are called leaves Nodes with children are called internal Data may be stored in nodes a 3 c y leaf CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland node J edge internal node 10 Node Levels and Complete and Perfect Binary Trees Nodes can be divided into levels based on distance number of edges from root The height of a tree is the longest path from root A binary tree is complete if Every level except the last is full In the last level every leaf is as far to the left as possible a Level 0 Level 2 A binary tree is perfect if every level including the last is full CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 c Level 1 y J Height of tree is 2 11 The Tree Quiz Let T be a perfect tree of height n 1 a How many leaves does T have 2n b How many internal nodes does T have 2n 1 c How many total nodes does T have 2n 1 1 Let T be a complete binary tree of height n 2 a How many leaves does T have give range 2n 1 to 2n inclusive b How many total nodes may T have give range 2n to 2n 1 1 inclusive 3 Let T be a complete binary tree with n nodes What is its height Floor rounding down of log2n CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12 Heaps Assume data stored in tree nodes is ordered by A heap is A complete binary tree such that Every parent s data is its child ren s data 17 10 15 14 11 Fact greatest value is stored at root CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 13
View Full Document