Question: / / Partitions the list taking the first element as the pivot static Node partition ( Node head, Node tail ) { / / Select
Partitions the list taking the first element as the pivot
static Node partitionNode head, Node tail
Select the first node as the pivot node
Node pivot head;
pre and 'curr' are used to shift all
smaller nodes' data to the left side of the pivot node
Node pre head;
Node curr head;
Traverse the list until you reach the node after the tail
while curr tailnext
If current node's data is less than the pivot's data
if currdata pivotdata
int temp currdata;
curr.data prenext.data;
pre.next.data temp;
Move 'pre' to the next node
pre prenext;
Move 'curr' to the next node
curr currnext;
Swap the pivot's data with 'pre' data
int currData pivotdata;
pivot.data predata;
pre.data currData;
Return 'pre' as the new pivot
return pre;
Helper function for quick sort
static void quickSortHelperNode head, Node tail
Base case: if the list is empty or consists of a single node
if head null head tail
return;
Call partition to find the pivot node
Node pivot partitionheadtail;
Recursive call for the left part of
the list before the pivot
quickSortHelperheadpivot;
Recursive call for the right part of
the list after the pivot
quickSortHelperpivotnext, tail;
The main function for quick sort.
This is a wrapper over quickSortHelper
static Node quickSortNode head
Find the tail of the list
Node tail getTailhead
Call the helper function to sort the list
quickSortHelperheadtail;
return head;
In the previous answers, it doesn't work for my test data it only check empty and That is basically not doing anything...Please do the question properly. Also, Please only focus on quicksort, partition, and getNextIndex. You can modify other class but try not too and do not delete any lines from other classes. BUT please don't just copy my code. rewrite it in your own way.
use above code as reference to change the below code. Algorithm should change the links of the simulated nodes It is not allowed to modify the element of any node." Alsohas to be inplace quicksort.
import java.util.Arrays;
import java.security.SecureRandom;
public class ListOnArraydUNGSethyPagna
private int data;
int size;
public ListOnArraydUNGSethyPagnaint n
size n ;
data new intsize;
data; may be omitted in Java.
for int i ; i size ; i
datai i ;
datasize ;
datasize ;
public boolean isEmpty
return data;
public boolean isFull
return datasize ;
public void err
System.out.printlnOops;
public void insertFirstint x
if isFull err; return;
int i datasize ;
datasize datai ;
datai data;
data i;
datai x;
public void quicksort
public void quickSortListOnArraydUNGSethyPagna list, int start, int end
public int partitionListOnArraydUNGSethyPagna list, int start, int end
public String toString
if isEmpty return "The list is empty.";
StringBuilder sb new StringBuilder;
int i data;
sbappenddatai;
while datai
i datai;
sbappendappenddatai;
return sbappendutoString;
public static void mainString args
int testData try different inputs.
;
for int a : testData
int n alength;
SecureRandom random new SecureRandom;
ListOnArraydUNGSethyPagna list new ListOnArraydUNGSethyPagnan random.nextIntn ; you don't need to understand this line.
System.out.printlnThe original array: Arrays.toStringa;
for int i n ; i ; i list.insertFirstai;
System.out.printlnThe original list: list;
You can uncomment the following line to print out how the elements are stored
Systemout.printlnThe internal storage: Arrays.toStringlistdata;
list.quicksort;
System.out.printlnThe sorted list: list;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
