ZANIB1
Uploaded by
7 SLIDES
74 VUES
70LIKES

Assignment 1

DESCRIPTION

Candle Boxes, Custom Candle Boxes, Candle Packaging Wholesale, Candle Packaging, Candle Box Packaging, Candle Packaging Boxes, Candle Box Suppliers, Custom Printed Candle Boxes

1 / 7

Télécharger la présentation

Assignment 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. THE SUPERIOR UNIVERSITY LAHORE Analysis of Algorithm Course Title: Submitted To: DR. Imran Khan Department of CS & IT Submitted By: Zanib Adrees Reg. No. MCSW-S20-003 Department of CS Jan 31, 2022 Date of Submission:

  2. Marge Sort if len(arr) > 1: # Finding the mid of the array mid = len(arr)//2 # Dividing the array elements L = arr[:mid] # into 2 halves R = arr[mid:] # Sorting the first half mergeSort(L) # Sorting the second half mergeSort(R) i = j = k = 0 # Copy data to temp arrays L[] and R[] while i < len(L) and j < len(R): if L[i] < R[j]: arr[k] = L[i] i += 1

  3. else: arr[k] = R[j] j += 1 k += 1 # Checking if any element was left while i < len(L): arr[k] = L[i] i += 1 k += 1 while j < len(R): arr[k] = R[j] j += 1 k += 1 # Code to print the list def printList(arr): for i in range(len(arr)): print(arr[i], end=" ") print() # Driver Code if _name_ == '_main_':

  4. arr = [12, 11, 13, 5, 6, 7] print("Given array is", end="\n") printList(arr) mergeSort(arr) print("Sorted array is: ", end="\n") printList(arr) Output: Given array is: 12 11 13 5 6 7 Sorted array is: 5 6 7 11 12 13 Heap Sort def heapify(arr, n, i): largest = i # Initialize largest as root l = 2 * i + 1 # left = 2*i + 1 r = 2 * i + 2 # right = 2*i + 2 # See if left child of root exists and is # greater than root if l < n and arr[largest] < arr[l]: largest = l # See if right child of root exists and is # greater than root

  5. if r < n and arr[largest] < arr[r]: largest = r # Change root, if needed if largest != i: arr[i], arr[largest] = arr[largest], arr[i] # swap # Heapify the root. heapify(arr, n, largest) # The main function to sort an array of given size def heapSort(arr): n = len(arr) # Build a maxheap. for i in range(n//2 - 1, -1, -1): heapify(arr, n, i) # One by one extract elements for i in range(n-1, 0, -1): arr[i], arr[0] = arr[0], arr[i] # swap heapify(arr, i, 0)

  6. # Driver code arr = [12, 11, 13, 5, 6, 7] heapSort(arr) n = len(arr) print("Sorted array is") for i in range(n): print("%d" % arr[i],end=" ") Output Sorted array is: 5 6 7 11 12 13 Merge-sort recurrence by Master Theorem Formula for recurrences of the form T(n) = aT(n/b) + nc (T(1) = 1). –Merge-sort ⇒T(n) = 2T(n/2) + n (a = 2,b = 2, and c = 1). Proof (by iteration ) Recall geometric sum •a < bc

  7. bc • a = bc a= bc ⇔bac = 1 ⇒ Pklog=0b n−1 back = Plogk=0b n−1 1 = Θ(logb n) a = bc ⇔ logb a = logb bc = c T(n) = = ncΘ(logb n) + nlogb a = Θ(nc logb n) log n−1 •a > bc a> bc ⇔bac > 1 ⇒ Pklog=0b n−1 back = Θbaclogb n = Θ( baclog)logbbnn = Θ alogncbn T(n) = = Θ(nlogb a) + nlogb a = Θ(nlogb a)

More Related