Chapter 9 Sorting Algorithm and Insertion Sort Median of Three Paper

User Generated

znevylaronffbhybh

Computer Science

Description

Please take a look at the attached document. The question is PART B.

Part A is only for reference.

Unformatted Attachment Preview

Objectives Implement, test, and compare insertionSort and quickSort we saw in class. Understand how to implement a divide-and-conquer strategy. Explain the difference in the running times when sorting large arrays. Part A - No coding is required here. The aim of this section is to help you understand how a divide-and-conquer strategy works. For that, check the code below (in Python) and take your time to read the comments (i.e. the sentences preceded by the symbol #). def mergeSort(myList): if len(myList) > 1: #middle point is denoted by mid mid = len(myList) // 2 #We divide the initial array into 2 subarrays: left & right left = myList[:mid] right = myList[mid:] # Recursively call mergeSort on each half mergeSort(left) mergeSort(right) # Combine the subarrays combine(myList, left, right) def combine(myList, left, right): # We define two iterators for traversing the two halves of the list i = 0 #Start of the left subarray j = 0 #Start of the right subarray # 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 is copied into the list myList[k] = left[i] # Increment i (as we saw in class) i += 1 else: myList[k] = right[j] j += 1 # Move k to the next slot in the resulting array k += 1 # Copy all the remaining values in case we finished processing one of the subarrays # and we have NOT done processing the other one while i < len(left): myList[k] = left[i] i += 1 k += 1 while j < len(right): myList[k]=right[j] j += 1 k += 1 #Testing your routine on a small input list_random = [54,26,93,17,77,31,44,55,20] print(list_random) mergeSort(list_random) print(list_random) Part B a) Write and test insertionSort(). b) Write and test quickSort(). You may follow the same strategy used to implement mergeSort() in Part A. a. Implement the partition function as we saw it in class. b. Use the median-of-three method to select the pivot. c) Compare these two sorting algorithms for inputs of size: 100, 1000, 10000, 100000 and display the results Caveat: Make sure to use as input the initial array for both sorting algorithms, otherwise you risk to pass to one of the sorting algorithms a sorted list! Sample output of my program written in Python. If the time to sort an array is less than 1/10 seconds, then we get 0.0 and consider the difference negligible Testing the sorting routines for an array of size 100 Time to sort the array using quickSort --- 0.0 seconds --- Time to sort the array using insertionSort--- 0.0 seconds --- Testing the sorting routines for an array of size 1000 Time to sort the array using quickSort --- 0.0 seconds --Time to sort the array using insertionSort--- 0.04 seconds --- Testing the sorting routines for an array of size 10000 Time to sort the array using quickSort --- 0.03 seconds --Time to sort the array using insertionSort--- 4.84 seconds --- Testing the sorting routines for an array of size 100000 Time to sort the array using quickSort --- 0.4 seconds --Time to sort the array using insertionSort--- 600.29 seconds ---
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Hey, I am uploading the c++ code in a zipped file, I have tested it, its working well and fine.Let me know if you need any oth...

Similar Content

Related Tags