90 likes | 258 Views
Sorting: why? . We do it A LOT! Makes finding the largest and smallest value easier Makes finding how many of a certain value are in a list easier Using binary search, makes finding anything much quicker Plus it’s a nice way to show differences in efficiency in algorithms.
E N D
Sorting: why? • We do it A LOT! • Makes finding the largest and smallest value easier • Makes finding how many of a certain value are in a list easier • Using binary search, makes finding anything much quicker • Plus it’s a nice way to show differences in efficiency in algorithms
First: SelectionSort • Idea: Go through and find the smallest value in a list. • Switch it with the first value on the list • Or remove it and append it to a new list. • Repeat steps 1 and 2 until the entire list is sorted Analysis: worst case = n2 (with n being the length of the list) • Regardless of whether the list is already sorted or not. • Best case is also n2 • This algorithm is very easy to conceive of and to implement, but it isn’t terribly efficient.
Next: BubbleSort • Idea: compare the first and second number in the list. If the first is greater than the second, switch them (this is known as “bubbling up”). • Compare the (possibly new) second and third number in the list. If the second is greater than the third, switch them. • Compare the third and fourth numbers in the list. If the third is greater than the fourth, switch them. • Continue like this to the end of the list. • Repeat steps 1-4 again and again, until there are no more switches (bubbles). Analysis: Worst case: n2 (with n being the length of the list) • Best case: when the list is already sorted • Then you only do steps 1-4 one time and there are no bubbles, so analysis is only n • This algorithm is a bit more efficient than selection sort, but also just a bit more difficult to write in code (not much!)
Next: InsertionSort Idea: Always inserting into an already sorted portion of the list. The first number in the list by itself can be considered a sorted list. • Take the second number in a list and compare it to the first number. Make sure the first two numbers are in order by possibly switching the first and second number. • Take the third number and compare it to the second and possibly the first number, placing it into the correct order so the first, second, and third number are sorted. • Repeat steps 1-3 with each number in the list until the entire list is sorted. Analysis: Worst case: n2 (with n being the length of the list) • Best case: when the list is already sorted • Then you do one comparison for each number in the list, so analysis is only n • In terms of efficiency, about the same as bubblesort
Next: MergeSort • Idea: pretend each number in the list is its own list. • Merge each neighboring list into a list of two sorted elements. • Merge each sorted list of two elements into sorted lists of 4 elements. • Merge each sorted list of 4 elements into sorted lists of 8 elements. • Continue to merge neighboring lists until there’s only one list left. Analysis: Worst case: nlog2n (with n being the length of the list) • Each time we’re merging half the list. So we’re doing log2n merges. • We may need to compare each number when we merge, so that’s the n • This is the most efficient of the sorting algorithms we’ve seen!
defSelectionSort(ls): for i in range(len(ls)): s=ls[i] si = i for j in range(i, len(ls)): if (ls[j]<s): s=ls[j] si=j ls[si]=ls[i] ls[i]=s return ls a=[3,5,2,7,1] print (a) print ("=>", SelectionSort(a))
def bubblesort(list2): i = 0; bubble_test= False while ((i < len(list2)-1) and (bubble_test== False)): bubble_test= True for j in range(0, len(list2) - i - 1): if list2[j] > list2[j + 1]: x = list2[j] list2[j] = list2[j+1] list2[j+1] = x bubble_test= False i += 1 return(list2) ls = [3,2,4,7,1] print(bubblesort(ls))
def insertionsort(ls): for i in range(0,len(ls)): x = ls[i] j = i-1 while (j >= 0) and (ls[j] > x): ls[j+1] = ls[j] j = j-1 ls[j+1] = x return(ls) ls=[3,1,2,6,4,7] insertionsort(ls) print(ls)
MergeSort in Python: def mergesort(lista): if len(lista) <=1: return lista else: middle = len(lista) / 2 #keep dividing left side into 2 #lists half the size of the original left = mergesort(lista[:middle]) #keep dividing right side into 2 #lists half the size of the original right = mergesort(lista[middle:]) #now we have a bunch of small lists #that we need to keep merging #back into larger, sorted lists return merge(left, right) listb = [8,2,3,1,9,5,7,0,4,6] print(mergesort(listb)) def merge(left, right): result = [] i ,j = 0, 0 while i < len(left) and j < len(right): if left[i] <= right[j]: result.append(left[i]) i += 1 else: result.append(right[j]) j += 1 result += left[i:] result += right[j:] return result