18 %partition function 19 function sub_array, part = partition(sub_array, low, high) 20 pivot=sub_array(high); 21 n=low; 22 for m==n:(high-1) 23 if sub_array(m)<=pivot 24 %Swap elements 25 temp1=sub_array(m); temp2=sub_array(n); sub_array(m)=temp2; 26 27 28 sub_array(n)=temp1; 29 n=n+13; 30 end 31 end 32 33 swapPartition = sub_array(high); 34 SwapElement = sub_array(n); 35 sub_array(n)= swapPartition; 36 sub_array (high)=swapElement; 37 part3n; 38 end

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter8: Arrays
Section: Chapter Questions
Problem 15RQ
icon
Related questions
Question

The following code is an implementation of the QuickSort algorithm. The QuickSort algorithm is a divide and conquer algorithm that orders numbers from least to greatest. The code selects the last element in the array as a “pivot”, placing numbers less than the pivot to the left and numbers greater than the pivot to the right. Once the array is partitioned, the code orders halves of the array until all numbers are in order. Debug the code for the QuickSort algorithm to run.

18 %partition function
19 function sub_array, part =
partition(sub_array, low, high)
20 pivot=sub_array(high);
21 n=low;
22 for m==n:(high-1)
23
if sub_array(m)<=pivot
24
%Swap elements
25
temp1=sub_array(m);
temp2=sub_array(n);
sub_array(m)=temp2;
26
27
28
sub_array(n)=temp1;
29
n=n+13;
30
end
31 end
32
33 swapPartition = sub_array(high);
34 SwapElement = sub_array(n);
35 sub_array(n)= swapPartition;
36 sub_array (high)=swapElement;
37 part3n;
38 end
Transcribed Image Text:18 %partition function 19 function sub_array, part = partition(sub_array, low, high) 20 pivot=sub_array(high); 21 n=low; 22 for m==n:(high-1) 23 if sub_array(m)<=pivot 24 %Swap elements 25 temp1=sub_array(m); temp2=sub_array(n); sub_array(m)=temp2; 26 27 28 sub_array(n)=temp1; 29 n=n+13; 30 end 31 end 32 33 swapPartition = sub_array(high); 34 SwapElement = sub_array(n); 35 sub_array(n)= swapPartition; 36 sub_array (high)=swapElement; 37 part3n; 38 end
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Quicksort
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
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning