Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching and Sorting.

Similar presentations


Presentation on theme: "Searching and Sorting."— Presentation transcript:

1 Searching and Sorting

2 Demonstrate song.py, song_app.py, album.py, and album_app.py

3 Search In our Album class, we developed a search by title method. The search starts at the very beginning and goes through the entire list, until we either find the song, or reach the end of the list without finding a match. Called sequential search. def searchByTitle(self, titleToSearch ): found = False i = 0 while found == False and i < len(self): if self.songs[i].title.lower() == titleToSearch.lower(): found = True else: i += 1 if found == True: return self.songs[i] return None

4 What if the titles are alphabetically ordered …?
"(Everything I Do) I Do It For You", Bryan Adams, 7/27/1991 "End Of The Road", Boyz II Men, 8/15/1992 "Endless Love", Diana Ross and Lionel Riche, 8/15/1981 "Eye Of The Tiger", Survivor, 7/24/1982 "Flashdance ... What A Feeling", Irene Cara, 5/28/1983 "How Deep Is Your Love", Bee Gees, 12/24/1977 "I Will Always Love You", Whitney Houston, 11/28/1992 "Night Fever", Bee Gees, 3/18/1978 "The Theme From 'A Summer Place'", Percy Faith and his orchstra, 2/27/1960 "You Light Up My Life", Debby Boone, 10/15/1977 If we found the current title is ‘I Will Always Love You’ and we are looking for ‘How Deep Is Your Love,’ do we need to continue? (Assume the song in red is not in the list.) The answer is NO, we know the titles are SORTED, if we saw ‘I …’ but not ‘How …’, we’d know the song ‘How …’ is not in the list!

5 Revised search Let’s revise our search method based on the above observation … def searchByTitle(self, titleToSearch ): # assume titles are sorted found = False i = 0 while found == False and i < len(self): if self.songs[i].title.lower() == titleToSearch.lower(): found = True elif self.songs[i].title.lower() > titleToSearch.lower(): # added check break else: i += 1 if found == True: return self.songs[i] return None

6 What’s the difference? In the first version of the search, roughly how long do we have to search to either find the one we look for, or conclude that the song is not in the list? n steps where n is the number of songs in the list In the second version, we’d know much earlier (on the average) that the song is not in the list!

7 Wait … we can even do better!
Do we really need to search one-by-one from the beginning? The answer is NO. Binary search is much more faster. def binarySearch(self, titleToSearch ): # assume titles are sorted found = False left = 0 right = len(self) mid = (left + right) // 2 while found == False and left <= right: if self.songs[mid].title.lower() == titleToSearch.lower(): found = True break # leave the loop elif self.songs[mid].title.lower() > titleToSearch.lower(): # search the left half right = mid - 1 else: # search the right half left = mid + 1 if found == True: return self.songs[mid] else: return None

8 How to sort a list? (We did it in hw3!)
def insertOne(element, aList): ''' Inserts element into its proper place in a sorted list aList. Input: element is an item to be inserted. aList is a sorted list. Output: A sorted list.''' if len(aList) == 0: return [element] elif element < aList[0]: return [element] + aList else: return aList[0:1] + insertOne(element, aList[1:]) def sort(aList): ''' sort returns a list of the elements of aList in ascending order. Input aList: a list ''‘ if aList == []: return [] else: sortedList = sort(aList[1:]) return insertOne(aList[0], sortedList)

9 Sorting Revisited How to do a selection sort in an Imperative style?
Develop a plan: Find index of smallest element in list. Swap that with the first element. Find index of 2nd smallest element in list. Swap that with the 2nd element. Repeat until we run out of elements. Imperative style – we use loops, break into subtasks, and change values of variables "in-place." Subtasks identified: Find index of minimum of a list Swap two elements in a list

10 Index of minimum in a list
def indexOfMinimum(aList, startIndex): ''' returns index of the minimum element at or after startIndex. ''' minIndex = startIndex for i in range(startIndex, len(aList)): if aList[i] < aList[minIndex]: minIndex = i return minIndex

11 Swap two elements in a list
def swap(a, b): ''' swaps the values of a and b ''' temp = a a = b b = temp Try it with def main(): aList = [5, 3, 4, 2, 7] swap(aList[0], aList[3]) print(aList) Draw the name space-data space model on board using arrows for the references. Doesn't work! Why?

12 Swap two elements in a list – 2nd Try
def swap(aList, i, j): ''' swaps the values of aList[i] and aList[j] ''' temp = aList[i] aList[i] = aList[j] aList[j] = temp Try it with def main(): aList = [5, 3, 4, 2, 7] swap(aList, 0, 3) print(aList) Draw the name space-data space model on board using arrows for the references. Works! Why?

13 Sorting Revisited Put the pieces together def selectionSort(aList):
''' sort aList in an imperative style: iteratively, subtasks, and in-place ''' for start in range(len(aList)): minIndex = indexOfMinimum(aList, start) swap(aList, start, minIndex) Summarize that imperative style uses iterations (loops), break into sub tasks and change variables in-place.


Download ppt "Searching and Sorting."

Similar presentations


Ads by Google