0% found this document useful (0 votes)
4 views

Lecture#5 (Design Algo Week 6)

Uploaded by

Malik Atif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture#5 (Design Algo Week 6)

Uploaded by

Malik Atif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Design and Analysis of

Algorithm
WEEK 6
Quiz
 Write some Properties of  Explain Divide and conquer
Algorithm? algorithms?
 Explain Parse Tree with  Explain Sorting with example?
example?  Briefly explain Selection sort
 Briefly explain bubble sort with example?
with example?  Sort the following according to
 Sort the following according to Selection, Insertion & Bubble
Selection, Insertion & Bubble sort:
sort:
 2.4 2.5 -2.6 2.06 -2.06
 3.4 3.5 -3.6 3.06 -3.06 2.60 2.55 2.555
3.60 3.55 3.555
Merge sort
 Merge sort is one of the most efficient sorting algorithms. It is based on the
divide-and-conquer strategy.
 Merge sort continuously cuts down a list into multiple sublists until each has
only one item, then merges those sublists into a sorted list.
 An example of merge sort.
 First, divide the list into the smallest unit (1 element), then compare each
element with the adjacent list to sort and merge the two adjacent lists.
 Finally, all the elements are sorted and merged.
Sort Formula
 The runtime of merge sort is given by the formula, 
 T(n) = 2*T(n/2) + n,
 where T(n) is the number of comparisons required to sort a list containing
n elements.
 The algorithm, repeatly, reduces the problem size by half (n/2) each time it
splits the unsorted list of numbers into two sublists.
Complexity
 Worst complexity: n*log(n)
 Average complexity: n*log(n)
 Best complexity: n*log(n)
 Space complexity: n
 Method: Merging
 Stable: Yes
Now sort the following according to Merge
sort:

19 34 11 1 49 21 41
Now sort the following according to Merge
sort:

83 91 4 52 95 71 36
 def mergeSort(myList):
 if len(myList) > 1:
 mid = len(myList) // 2
 left = myList[:mid]
 right = myList[mid:]
 # Recursive call on each half
 mergeSort(left)
 mergeSort(right)
 # Two iterators for traversing the two halves
 i=0
 j=0
 # Iterator for the main list
 k=0
 while i < len(left) and j < len(right):
 if left[i] <= right[j]:
 # The value from the left half has been used
 myList[k] = left[i]
 # Move the iterator forward
 i += 1
 else:
 myList[k] = right[j]
 j += 1
 # Move to the next slot
 k += 1
 # For all the remaining values
 while i < len(left):
 myList[k] = left[i]
 i += 1
 k += 1
 while j < len(right):
 myList[k]=right[j]
 j += 1
 k += 1
 myList = [54,26,93,17,77,31,44,55,20]
 mergeSort(myList)
 print(myList)

You might also like