Question

In: Computer Science

In Python please. Construct a large file of data in random order where the key is...

In Python please.

Construct a large file of data in random order where the key is a 13-digit number.
Then sort in two different ways. Use a Quick sort and then use a radix sort of some kind. Code both sorts in the same language and run experiments to see at what size of data does the radix run faster than the Quick sort.

Solutions

Expert Solution

"""
The time compexity of quicksort is nlogn
where as time complesitiy of radixsort is n+k
k->range of value max-min
n->size if array
i tested for different range value of k but quick sort always
out performed the radix sort
as k the value approachs large value it will become impossible for it to
outperform the quick sort
"""
import time
import random

def countingSort(arr, exp1):
n = len(arr)
output = [0] * (n)
count = [0] * (10)

for i in range(0, n):
index = int(arr[i]/exp1)
count[ index%10 ] += 1
  
for i in range(1,10):
count[i] += count[i-1]
  
i = n-1
while i>=0:
index = int(arr[i]/exp1)
output[ count[ (index)%10 ] - 1] = arr[i]
count[ (index)%10 ] -= 1
i -= 1
  
i = 0
for i in range(0,len(arr)):
arr[i] = output[i]

def radixSort(arr):
max1 = max(arr)
exp = 1
while max1/exp > 0:
countingSort(arr,exp)
exp *= 10

def partition(arr,low,high):
i = ( low-1 )
pivot = arr[high]
for j in range(low , high):
if(arr[j] <= pivot):
i = i+1
arr[i],arr[j] = arr[j],arr[i]
arr[i+1],arr[high] = arr[high],arr[i+1]
return ( i+1 )

def quickSort(arr,low,high):
if low < high:
pi = partition(arr,low,high)
quickSort(arr, low, pi-1)
quickSort(arr, pi+1, high)
  
limit =5
start = 1
end = 2
while(True):
arr = [random.randint(start,end) for _ in range(limit)]
t1 = time.time()
radixSort(arr)
t2 = time.time()
quickSort(arr,0,len(arr)-1)
t3 = time.time()
if(t2-t1<=t3-t2):
print(limit)
break
limit+=1
print(limit)


Related Solutions

PYTHON LANGUAGE Write the data in variable artist_songs into a csv file, 'songs.txt', where the first...
PYTHON LANGUAGE Write the data in variable artist_songs into a csv file, 'songs.txt', where the first column is singer name and second column is a song name. Each line should have a singer and a song name. Use "Name" and "Song" as headers. Do not include double quotation marks (") in your CSV but you should include apostrophes where necessary (for example, for the song "We Don't Talk Anymore"). In [110]: artist_songs = { 'Taylor Swift': ['Love Story', 'You need...
please answer this in a simple python code 1. Write a Python program to construct the...
please answer this in a simple python code 1. Write a Python program to construct the following pattern (with alphabets in the reverse order). It will print the following if input is 5 that is, print z one time, y two times … v five times. The maximum value of n is 26. z yy xxx wwww vvvvvv
how to export source data to excel file in python?
how to export source data to excel file in python?
Send this as a Python file. Note: this is an example where you have the original...
Send this as a Python file. Note: this is an example where you have the original file, and are writing to a temp file with the new information. Then, you remove the original file and rename the temp file to the original file name. Don't forget the import os statement.A file exist on the disk named students.txt The file contains several records and each record contains 2 fields :1. The student's name and 2: the student's score for final exam....
Please use Python! def getText(file): This function should open the file named file, and return the...
Please use Python! def getText(file): This function should open the file named file, and return the contents of the file formatted as a single string. During processing, you should (i) remove any blank lines, (ii) remove any lines consisting entirely of CAPITALIZED WORDS , and (iii) replace any explicit ’\n’ (newline) characters with spaces unless directly preceeded by a ’-’ (hyphen), in which case you should simply remove both the hyphen and the newline, restoring the original word. def getText(file):...
a. Construct a scatter plot of the data. Determine the order of the polynomial that is represented by this data.
  Consider the following data: x 1 4 5 7 8 12 11 14 19 20 y 1 54 125 324 512 5,530 5,331 5,740 7,058 7,945 Use Excel to resolve: a. Construct a scatter plot of the data. Determine the order of the polynomial that is represented by this data. b. Obtain an estimate of the model identified in part a. c. Conduct a test of hypothesis to determine if a third- order, as opposed to a first-order, polynomial...
In order to construct a confidence interval for the population variance, a random sample of n...
In order to construct a confidence interval for the population variance, a random sample of n observations is drawn from a normal population. Use this information to find χ2α/2,df and χ21- α/2,df under the following scenarios. Use Table 3. (Round your answers to 3 decimal places.) χ2α/2,df and χ21- α/2,df . A 95% confidence level with n = 18. b. A 95% confidence level with n = 30. c. A 99% confidence level with n = 18. d. A 99%...
In order to construct a confidence interval for the population variance, a random sample of n...
In order to construct a confidence interval for the population variance, a random sample of n observations is drawn from a normal population. Use this information to find χ2α/2,df and χ21-α/2,df under the following scenarios. (Round your answers to 3 decimal places. You may find it useful to reference the appropriate table: chi-square table or F table) χ2α/2,df χ21-α/2,df a. A 90% confidence level with n = 20. b. A 90% confidence level with n = 43. c. A 99%...
In order to construct a confidence interval for the population variance, a random sample of n...
In order to construct a confidence interval for the population variance, a random sample of n observations is drawn from a normal population. Use this information to find χ2α/2,df and χ21- α/2,df under the following scenarios. (Round your answers to 3 decimal places. You may find it useful to reference the appropriate table: chi-square table or F table) χ2α/2,df χ21- α/2,df a. A 95% confidence level with n = 18. b. A 95% confidence level with n = 30. c....
Language: Python Function name : sort_by_rating Parameters : file (.csv file), category (string), order (boolean) Returns:...
Language: Python Function name : sort_by_rating Parameters : file (.csv file), category (string), order (boolean) Returns: None Description : You want to see what order the items of a particular category in your file would be in if you sorted them by rating. Given a file, a category of clothing, and an order boolean (True for ascending order, False for descending order), create a function that writes a file called “sorted_items.csv” that includes all of the items in the specified...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT