The code below serves as a template where the base case and the recursive calls are done. The specific task is to implement the partitioning where elements are either put to the left or right of the pivot depending on their relative values.   Input Format The first line contains an integer n, the number of elements in the list. The next n lines would be the unsorted elements of the list. Constraints 1 ≤ n ≤ 10000 Each element i is: -1000 ≤ i ≤ 1000 Minimize auxiliary space usage. Output Format n lines that display the n elements of the sorted list. Smallest element first, largest element last.   Example Input 15 3 44 38 5 47 15 36 26 27 2 46 4 19 50 48   Example Output 2 3 4 5 15 19 26 27 36 38 44 46 47 48 50

New Perspectives on HTML5, CSS3, and JavaScript
6th Edition
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Patrick M. Carey
Chapter14: Exploring Object-based Programming: Designing An Online Poker
Section14.1: Visual Overview: Custom Objects, Properties, And Methods
Problem 7QC
icon
Related questions
Question

The code below serves as a template where the base case and the recursive calls are done. The specific task is to implement the partitioning where elements are either put to the left or right of the pivot depending on their relative values.

 

Input Format

The first line contains an integer n, the number of elements in the list. The next n lines would be the unsorted elements of the list.

Constraints

1 n 10000

Each element i is: -1000 ≤ i ≤ 1000

Minimize auxiliary space usage.

Output Format

n lines that display the n elements of the sorted list. Smallest element first, largest element last.

 

Example Input

15

3

44

38

5

47

15

36

26

27

2

46

4

19

50

48

 

Example Output

2

3

4

5

15

19

26

27

36

38

44

46

47

48

50

def quick_sort(arr):
# helper function
quick_sort_r(arr, e, len(arr))
1
2
3
4.
def quick_sort_r(arr, start, end):
if end - start < 2:
# single element base case
7
8.
return
9.
# choose a pivot
pivot = start
10
11
# you may choose other elements
12
13
# START CODE FOR PARTITIONING HERE
14
15
# END CODE FOR PARTITIONING BEFORE THIS LINE
16
17
# Recursive Calls
# normally you wouldn't need to edit these but you may if want.
quick_sort_r(arr, start, pivot)
quick_sort_r(arr, pivot+1, end)
18
19
20
21
return
22
# input handling
n = int(input())
arr = []
for i in range(n):
arr.append (int(input()))
23
24
25
26
27
28
# quick sort function call
quick_sort(arr)
29
30
31
32
# output loop
for i in arr:
33
34
print(i)
Transcribed Image Text:def quick_sort(arr): # helper function quick_sort_r(arr, e, len(arr)) 1 2 3 4. def quick_sort_r(arr, start, end): if end - start < 2: # single element base case 7 8. return 9. # choose a pivot pivot = start 10 11 # you may choose other elements 12 13 # START CODE FOR PARTITIONING HERE 14 15 # END CODE FOR PARTITIONING BEFORE THIS LINE 16 17 # Recursive Calls # normally you wouldn't need to edit these but you may if want. quick_sort_r(arr, start, pivot) quick_sort_r(arr, pivot+1, end) 18 19 20 21 return 22 # input handling n = int(input()) arr = [] for i in range(n): arr.append (int(input())) 23 24 25 26 27 28 # quick sort function call quick_sort(arr) 29 30 31 32 # output loop for i in arr: 33 34 print(i)
Expert Solution
steps

Step by step

Solved in 3 steps with 3 images

Blurred answer
Knowledge Booster
Merge Sort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning