Question

Quicksort of an array in PYTHON For this problem, you will need to generate arrays of...

Quicksort of an array in PYTHON

For this problem, you will need to generate arrays of 1000 to 10000 3 or 4 digit integers, sort them and also print out the execution time it took to compile.

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

from random import randint
import time

def partition(arr,start,end):#set the correct index of the last index of value and return the index
   x = arr[end]
   i=start-1
   for j in range(start,end):
       if(x>=arr[j]):
           i+=1
           temp=arr[i]
           arr[i]=arr[j]
           arr[j]=temp
   temp=arr[end]
   arr[end]=arr[i+1]
   arr[i+1]=temp
   return i+1


def quicksort(arr,start,end):
   if(start<end):
       mid=partition(arr,start,end)#the partition function set the value at the correct index
       quicksort(arr,start,mid-1)#call the same function for the left side
       quicksort(arr,mid+1,end)#call the same function for the right side

if __name__ == "__main__":
   L=[0]*1000 #create a array(list) of 1000
   for i in range(1000):
       L[i]=randint(100, 10000)#fill by 3 or 4 integers
   print(L)
   n = len(L)
   start_time = time.time()
   quicksort(L,0,n-1)
   end_time = time.time()
   print("After sort the L")
   print(L)
   print("the sort time of array is: ",end_time-start_time)

Output is so long so i print only the time to sort of the array

the sort time of array is: 0.006806135177612305 ***Repl closed***

Add a comment
Know the answer?
Add Answer to:
Quicksort of an array in PYTHON For this problem, you will need to generate arrays of...
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
  • IN PROLOG 1) quicksort on array For this problem, you will need to generate arrays of...

    IN PROLOG 1) quicksort on array For this problem, you will need to generate arrays of 1000 to 10000 3 or 4 digit integers. You should generate the array using the random() function in a C++ program. (try: "man random" at command line for reference on this)

  • C programming Strictly - Write a program to sort an array of integers via arrays of...

    C programming Strictly - Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. Problem 1 (68 points): Write a program to sort an array of integers via arrays of pointers to those integers as shown in the figure. The code should contain the following functions: 1)to randomly generate an array of integer numbers; 2) to allocate memory and build the arrays of pointers as shown in the figure...

  • You want to sort (in increasing order) the following array of integers using quicksort as we...

    You want to sort (in increasing order) the following array of integers using quicksort as we have described it and used it in class. You are asked to specifically show your steps and the resulting array after one pass of quicksort. Show and explain each of your steps. Note 1: in case you are not using the algorithm presented and traced in class, you are expected to show all your steps accompanied with algorithm instructions and variables' values. Note 2:...

  • HW60.1. Array Quicksort You've done partition so now it's time to finish Quicksort. Create a publ...

    HW60.1. Array Quicksort You've done partition so now it's time to finish Quicksort. Create a public non-final class named Quicksort that extends Partitioner. Implement a public static method void quicksort (int] values) that sorts the input array of ints in ascending order. You will want this method to be recursive, with the base case being an array with zero or one value. You should sort the array in place, which is why your function is declared to return void. If...

  • Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and...

    Write a java program to sort arrays using 3 different methods: Bubble Sort, Selection Sort and Insertion Sort. The numbers to be sorted will be obtained using a library function which generates pseudo-random numbers. TO Do 1. Fill an array with 140 real numbers between 0.00 and 945.00. Generate the numbers using the subroutine that generates random numbers. Make a spare copy of the array; you will need it later. 2. Call a subroutine to print the contents of the...

  • I currently have this but it doesn't work for the three item arrays public class Quicksort...

    I currently have this but it doesn't work for the three item arrays public class Quicksort extends Partitioner { public static void quicksort(int[] values) { if (values == null || values.length < 2) { return; } quicksort(values, 0, values.length - 1); } private static void quicksort(int[] values, int start, int end) { System.out.println(values); System.out.println(start); System.out.println(end); if (values == null || Math.abs(start - end) == 1) { return; } if (end > start) { int pivotValueIndex = partition(values, start, end); quicksort(values,...

  • 1. Which is the best sorting algorithm for larger arrays if all the items can not...

    1. Which is the best sorting algorithm for larger arrays if all the items can not fit in main memory? selection sort insertion sort quicksort Merge sort 2. Which sorting algorithm sorts items without comparing them? quick sort radix sort merge sort Insertion sort 3 What is the average running time for quicksort? O(n2) O(logn) O(nlogn) O(n) O(n2logn) 4. Examine the steps of the following algorithm and write the name of the algorithm described in the blank provided: Recursively divide...

  • 1.You are to write a program name search.java that will do the following: 2.You are to...

    1.You are to write a program name search.java that will do the following: 2.You are to create 3 arrays - prompt the user for a number that is greater than 100 that will serve as the size for the arrays (all 3 arrays will have the same user input size). Call them A, B & C. 3.Generate this amount of random numbers to fill these arrays – the random numbers must range from 1 to 99. 4.Write 1 sequential search...

  • Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays....

    Write a JAVA Program: Compare the performance of bubble sort and selection sort over several arrays. - Write two methods that sort arrays of doubles. One method should use selection sort, and the other should use bubble sort. - In each of the sort methods, add code that counts the total number of comparisons and total number of swaps performed while sorting the entire array (be careful; don't count each pass through the array separately) - Each time an array...

  • Please solve this problem using Python Write a quicksort method that accept a list of numbers...

    Please solve this problem using Python Write a quicksort method that accept a list of numbers and use list comprehension to construct the method. Note that you need to sort the numbers in descending order . You MUST use List Comprehension to do the sorting Your program should contain the function with format shown as below: def quicksort(a): # Your codes here. Use List Comprehension and return the result.

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