Question

In Python, perform experimental analysis to test the hypothesis that Python's sorted method runs in O(nlog...

In Python, perform experimental analysis to test the hypothesis that Python's sorted method runs in O(nlog n) time on average

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python Sorted method uses TimSort Algorithm.

TimSort

Timsort is a modified version of merge sort which uses insertion sort to arrange the list of items into conveniently mergeable sections.

  1. A stable sorting algorithm works in O(n Log n) time.
  2. First sort small pieces using Insertion Sort, then merges the pieces using merge of merge sort.

Working

Divide the Array into blocks known as Run. then sort those runs using insertion sort one by one and then merge those runs using combine function used in merge sort. If the size of the Array is less than run, then Array gets sorted just by using Insertion Sort. The size of run may vary from 32 to 64 depending upon the size of the array. Note that the merge function performs well when size subarrays are powers of 2. The idea is based on the fact that insertion sort performs well for small arrays.

Experimental Analysis

let's Understand it by implementing the Timsort algorithm.

# Python3 program to perform TimSort.
RUN = 32
  
# This function sorts array from left index to
# to right index which is of size atmost RUN
def insertionSort(arr, left, right):

   for i in range(left + 1, right+1):
  
       temp = arr[i]
       j = i - 1
       while arr[j] > temp and j >= left:
      
           arr[j+1] = arr[j]
           j -= 1
      
       arr[j+1] = temp
  
# merge function merges the sorted runs
def merge(arr, l, m, r):

   # original array is broken in two parts
   # left and right array
   len1, len2 = m - l + 1, r - m
   left, right = [], []
   for i in range(0, len1):
       left.append(arr[l + i])
   for i in range(0, len2):
       right.append(arr[m + 1 + i])
  
   i, j, k = 0, 0, l
   # after comparing, we merge those two array
   # in larger sub array
   while i < len1 and j < len2:
  
       if left[i] <= right[j]:
           arr[k] = left[i]
           i += 1
      
       else:
           arr[k] = right[j]
           j += 1
      
       k += 1
  
   # copy remaining elements of left, if any
   while i < len1:
  
       arr[k] = left[i]
       k += 1
       i += 1
  
   # copy remaining element of right, if any
   while j < len2:
       arr[k] = right[j]
       k += 1
       j += 1
  
# iterative Timsort function to sort the
# array[0...n-1] (similar to merge sort)
def timSort(arr, n):

   # Sort individual subarrays of size RUN
   for i in range(0, n, RUN):
       insertionSort(arr, i, min((i+31), (n-1)))
  
   # start merging from size RUN (or 32). It will merge
   # to form size 64, then 128, 256 and so on ....
   size = RUN
   while size < n:
  
       # pick starting point of left sub array. We
       # are going to merge arr[left..left+size-1]
       # and arr[left+size, left+2*size-1]
       # After every merge, we increase left by 2*size
       for left in range(0, n, 2*size):
      
           # find ending point of left sub array
           # mid+1 is starting point of right sub array
           mid = left + size - 1
           right = min((left + 2*size - 1), (n-1))
  
           # merge sub array arr[left.....mid] &
           # arr[mid+1....right]
           merge(arr, left, mid, right)
      
       size = 2*size
      
# utility function to print the Array
def printArray(arr, n):

   for i in range(0, n):
       print(arr[i], end = " ")
   print()

  
# Driver program to test above function
if __name__ == "__main__":

   arr = [5, 21, 7, 23, 19]
   n = len(arr)
   print("Given Array is")
   printArray(arr, n)
  
   timSort(arr, n)
  
   print("After Sorting Array is")
   printArray(arr, n)
  

Details of above implementation:

  • Consider the size of the run as 32.
  • one by one sort pieces of size equal to run
  • After sorting individual pieces, we merge them one by one. We double the size of merged subarrays after every iteration.

In the Average case, the size of the sorted sections of the list doubles after every iteration of merges. After M steps the size of the sorted sections is 2M. Once 2M is greater than N, the entire list is sorted. Thus, for a list of size N, we need M equals log2N iterations to sort the list.

The total number of operations required for the Timsort algorithm is the product of the number of operations in each pass and the number of passes – i.e. Nlog2N comparisons.

so the AverageComplexity of the python sorted method is O(nlogn).

Add a comment
Know the answer?
Add Answer to:
In Python, perform experimental analysis to test the hypothesis that Python's sorted method runs in O(nlog...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON Im kinda stuck here for this question, could use some help! Python please, and please...

    PYTHON Im kinda stuck here for this question, could use some help! Python please, and please follow the requirements. 09 5 Points Al and Bob are arguing about their algorithurs. Al claims his Oinlogn time method is always faster than Bob's O(n^2)-time method. To settle the issue they perform a set of experiments. To Al's dismay, they find that if n < 100 the On/2)-time algorithm runs faster and only when na 100. O(nlog n)-time one runs faster. Explain how...

  • Test runs with 6 models of an experimental engine showed that they operated for 24, 28,...

    Test runs with 6 models of an experimental engine showed that they operated for 24, 28, 21, 23, 32, and 22 minutes on a gallon of a certain kind of fuel.  If the probability of a Type I error is to be at most 0.01, is this evidence against a hypothesis that on the average this kind of engine will operate for at least 29 minutes per gallon with this kind of fuel?  Assume normality. - Perform a hypothesis test.

  • If n<30, we cannot perform a hypothesis test for Ho:p = 0.5. O True O False

    If n<30, we cannot perform a hypothesis test for Ho:p = 0.5. O True O False

  • Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct...

    Design and analysis of algorithms Type in answer Problem 5. Given a sorted array of distinct integers A[1- -n], you want to find out whether there is an index I for which Ai-i. Give a divide-and-conquer algorithm that runs in time O(log n)

  • QUESTION 4 Use the one- proportion z-test to perform the specified hypothesis test. Use the criti...

    QUESTION 4 Use the one- proportion z-test to perform the specified hypothesis test. Use the criticalvalue approach x®7, n.81 , H 0: p-0.08, H a: p # 0.08, α-0.10 O z = 0.21; critical values 1.645; do not reject Ho o z-1.87; critical values +1.645; reject Ho O z -0.41; critical values ±1.28; do not reject Ho O z. 0.21; critical values 1.28; do not reject Ho perform the specified hypothesis test. Use the critical-value approach QUESTION 4 Use the...

  • IDENTIFYING THE METHOD OF ANALYSIS In Evercises Ci to C8,identify the method of analysis needed to answer the question. Indicate whether we should conduct a hypothesis test or find a confidence...

    IDENTIFYING THE METHOD OF ANALYSIS In Evercises Ci to C8,identify the method of analysis needed to answer the question. Indicate whether we should conduct a hypothesis test or find a confidence interval and also indicate whether the analysis will be done on a proportion, a mean, a difference in proportions, a difference in means, or a matched pairs difference in means. C.2 Use results collected at a supermarket to see whether there is a difference in the average amount of...

  • For the following questions, indicate what kind of analysis you would perform and identify the hypothesis....

    For the following questions, indicate what kind of analysis you would perform and identify the hypothesis. 7. Suppose a sample of n students were given a diagnostic test before studying a particular module and then again after completing the module. We want to find out if, in general, ourteaching leads to improvements in students’ knowledge/skills (i.e. test scores).

  • set-up each hypothesis test (Steps 1-3) only. DO NOT PERFORM A COMPLETE HYPOTHESIS TEST! Be sure...

    set-up each hypothesis test (Steps 1-3) only. DO NOT PERFORM A COMPLETE HYPOTHESIS TEST! Be sure to properly label all variables (z,t, etc...) and parameters(1.0, etc..). Do not only write a numbers 3.) The average number of series streamed per year for households is reported to be 8.5 with a standard deviation of 2.1. In random sample of 30 households, the standard deviation in series streamed was 3.5. At a = 0.10, is there evidence to suggest that the standard...

  • A hypothesis test is to be performed. Determine the wall and alternate hypothesis. In 1993, the...

    A hypothesis test is to be performed. Determine the wall and alternate hypothesis. In 1993, the average duration of long-distance telephone calls originating is one town 6.6 minutes. A long-distance telephone company wants to perform a hypothesis test to dete whether the average duration of long-distance phone calls has changed from the 1993 me of minutes. A. Ho: -6.6 minutes H: > 6.6 minutes B. H: -6.6 minutes HA 66 minutes C. H: -6.6 minutes 6 H D. H: H...

  • If you perform a hypothesis test on the population slope Parameter (β1) in regression analysis and...

    If you perform a hypothesis test on the population slope Parameter (β1) in regression analysis and reject the Null hypothesis: Ho: β1= 0. Your conclusion would be: A.) The least squares sample regression equation should not be used because there is not sufficient evidence of a relationship between the independent variable and the dependent variable.. B.) The least squares sample regression equation should be used because there is sufficient evidence of a relationship between the independent variable and the dependent...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT