Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I would like to ask help with this c programming task. Make a c program with the following details: Problem Statement Quicksort is one of

I would like to ask help with this c programming task. Make a c program with the following details:
image text in transcribed
image text in transcribed
image text in transcribed
Problem Statement Quicksort is one of the most popular sorting algorithms, and it follows the divide-and-conquer paradigm. Quicksort works by choosing a pivot element from an array or a list of elements, and partitioning the array such that the values of all elements to the left of the pivot are less than (or equal to) the pivot element, and the values all elements to the right of the pivot are greater than the pivot element. Quicksort then proceeds to recursively sort the elements to the left of the pivot and to the right of the pivot. The base cases for the recursion are 0-element and 1-element arrays, which are trivially sorted In this exercise, you will be implementing basic quicksort. Basic quicksort is called as such because it picks the first or last element as the pivot, and proceeds with the sorting. We will be using the last element as the pivot for this exercise. Here is pseudocode for basic quicksort. The quicksort method takes an array A, and sorts elements from index p until index g (inclusive). Note how the partition method works; this is important as it is able to partition the array in-place (no auxiliary arrays are needed). quicksort (A, D, r) if par q = partition (A, P. r) quicksort (A, P, Q - 1) quicksort (A, G + 1, r) partition (A, D, r) x = A[r] i = p - 1 for j = ptor - 1 if A[j] = x i-i + 1 exchange A[i] with Alil exchange Ai + 1] with A[r] return i +1 Initial Call: quicksort (A,1,n) Note that the value nn above corresponds to the number of elements in array AA. Also, array AA starts at index 11. This basic quicksort implementation has a downside in the worst case, the input is sorted/reverse sorted, and the algorithm will run in (na)(n2) time, as the recursive step is able to sort only one element at a time To try to address this problem, we randomly shuffle the input array at the beginning. We will be using a pseudorandom way of shuffling, using a prime number PP that is greater than the number of elements MM we have to sort. To shuffle the array, we generate a permutation of the values 0,1,2,...M-10,1.2...M-1 by getting PP mod MM, 2P2P mod MM, 3P3P mod MM. -, MPMP mod MM For example, P=13P-13 and M=8 13 mod 8 = 5 26 mod 8 = (5 + 13) mod 8 = 2 39 mod 8 = (2 + 13) mod 8 = 7 52 mod 8 = (7 + 13) mod 8 = 4 65 mod 8 = (4 + 13) mod 8 = 1 78 mod 8 = (1 + 13) mod 8 = 6 91 mod 8 = (6 + 13) mod 8 = 3 104 mod 8 = (3 + 13) mod 8 = 0 giving a sequence of 5 27 4 16.30. We then use this sequence as indices of the array to permute the array. This means in the updated array, the (new) element at index O takes the (old) element at index 5 in the original array. Similarly, new element at index 1 takes old element at index 2 new element at index 2 takes old element at index 7. and so on. Implement the basic quicksort algorithm as defined above, to shuffle an array of integers. Before running the sorting algorithm, shuffle the array using our pseudorandom method using the relatively prime pair (P.M)(P.M). After each call to partition (), print the values of the partitioned array/subarray. The input for this exercise will be lists of 1000 integers (M=1000M=1000). We will be using P=524287P=524287 Reference: CLRS Introduction to Algorithms (Third Edition). pp. 171-172 Input Format Input starts with an integer TT, denoting the number of test cases. TT lines follow, corresponding to each test case. Each test case contains one line, containing 1000 integers separated by spaces. Output Format Each test case will have multiple lines of output. The first line of output for each test case will contain the integers of the array after the shuffling, separated by spaces. After each call to partition () during the recursive execution of quicksort, print the values of the partitioned array/subarray on one line, separated by spaces. For the last line of output, print out the sorted array of integers. Sample Input For brevity, sample input only contains 8 integers (M=8M=8), using P=13P=13 for the shuffling. 1 3 2 5 6 8 4 7 1 Sample Output 4 51 8 2 7 6 3 1 2 3 8 5 7 6 4 1 2 4 5 768 5 7 6 8 5 6 7 1 2 3 4 5 6 7 8

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David M. Kroenke, David Auer

11th Edition

B003Y7CIBU, 978-0132302678

More Books

Students also viewed these Databases questions

Question

apply research strategies to writing.

Answered: 1 week ago