Question

write a code in python for COUNTING SORT. check the runtime for the algorithms for varying...

write a code in python for COUNTING SORT. check the runtime for the algorithms for varying input sizes.

Input size n (size of the list) the following list sizes

1. 10,000

2. 100,000

3. 1000,000

For each of these sizes we are going to have two types of input:

a. Sorted list (ascending order) of elements from range (1:n)

b. Sorted list (descending order) of elements from range (n:1)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 import time def countingSort(arr): n = len(arr) output = [0] * n count = [0] * 10 for i in range(0, n): count[arr[i]] += 1 for i in range(1, 10): count[i] += count[i - 1] i = n - 1 while i >= 0: output[count[arr[i]] - 1] = arr[i] count[arr[i]] -= 1 i -= 1 for i in range(0, n): array[i] = output[i] n = int(input("Enter size of array: ")) arr = []*n print("Sorted list (ascending order) of elements from range (1:n)") for i in range (0,n): arr[i] = int(input()) start = time.process_time() countingSort(arr) print(time.process_time() - start) print("Enter Sorted list (descending order) of elements from range (n:1)") for i in range (n-1,-1,-1): arr[i] = int(input()) start = time.process_time() countingSort(arr) print(time.process_time() - start) 

#Python program

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

for i in range(0, n):
count[arr[i]] += 1

for i in range(1, 10):
count[i] += count[i - 1]

i = n - 1
while i >= 0:
output[count[arr[i]] - 1] = arr[i]
count[arr[i]] -= 1
i -= 1

for i in range(0, n):
array[i] = output[i]


n = int(input("Enter size of array: "))
arr = []*n
print("Sorted list (ascending order) of elements from range (1:n)")
for i in range (0,n):
arr[i] = int(input())

start = time.process_time()
countingSort(arr)
print(time.process_time() - start)

print("Enter Sorted list (descending order) of elements from range (n:1)")

for i in range (n-1,-1,-1):
arr[i] = int(input())

start = time.process_time()
countingSort(arr)
print(time.process_time() - start)

Add a comment
Know the answer?
Add Answer to:
write a code in python for COUNTING SORT. check the runtime for the algorithms for varying...
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
  • please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection...

    please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 4 6 Initial List Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position: Sorted List...

  • Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort...

    Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 Initial List 4 6 18 4 6 Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position:...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • Consider the following python code that will sort the list of numbers using the Bubble Sort...

    Consider the following python code that will sort the list of numbers using the Bubble Sort algorithm def bubbleSort(alist): print(alist) for j in range (len(alist) - 1, 8, -1): for i in range(): if alist[i] > alist[i + 1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp print(alist) alist = [52, 25, 94, 17, 25, 52] bubbleSort (alist) print(alist) Sort the following series of values into ascending order using the Bubble Sort algorithm. Write out the complete row of...

  • need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge...

    need help!! java eclipse Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot - first index) algorithms. a) Compute the CPU processing time for all the algorithms for varying input sizes as follows: N-10, 103, 10, 10, and 106 b) Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 10, 1 -10, 1 - 10, 1 12 10 c) Write down your results...

  • [Code in C] Help me with this. Not sure what to do. 1 Couting Sort You...

    [Code in C] Help me with this. Not sure what to do. 1 Couting Sort You may have learned some sorting algorithms - such as bubble sort ad quicksort in CS 110 and CS 210. This homework is about counting sort. Let n be the number of elements to be sorted. Bubble sort and quicksort assume tha time, which one is larger and which one is smaller. They make no assumption on the values of the elements t we can...

  • q: Write a java code to Test the heap sort algorithm Test the algorithms on arrays...

    q: Write a java code to Test the heap sort algorithm Test the algorithms on arrays of size: .100,000 , 90,000 ,80,000 ,70,000 ,60,000 ,50,000 ,40,000 ,30,000 ,20,000 ,10,000 Plot the execution time vs array size. without using excel

  • I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to...

    I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...

  • 1. Write a Java program to implement Counting Sort and write a driver to test it....

    1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...

  • Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm...

    Implement in C SharpCreate a new algorithm based on the algorithm, selection sort. The new algorithm should be able to sort an array like this: Input: an array that has n elements, and the values of its elements are assigned randomly, for example: Index 0 1 2 3 4 5 value 7 3 6 2 1 5 Output: an array - its first n/2 elements are sorted in ascending order and its second n/2 elements sorted in descending order. That...

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
ADVERTISEMENT