Question: import java.util.Arrays; import java.security.SecureRandom; / * * * * @author yixin cao ( September 2 8 , 2 0 2 0 ) * * Simulating
import java.util.Arrays;
import java.security.SecureRandom;
@author yixin cao September
Simulating a linked list with an array.
public class ListOnArraydHeJimsonLiu
private int data;
int size;
The constructor is slightly different to Lab
But the difference is irrevalent to the task.
public ListOnArraydHeJimsonLiuint 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;
Insert a new element as the new head of the list.
public void insertFirstint x
if isFull err; return;
int i datasize ;
datasize datai ;
datai data;
data i;
datai x;
The inplace quicksort algorithm.
VERY IMPORTANT.
I've discussed this question with the following students:
I've sought help from the following Internet resources and books:
Running time: O
public void quicksort
Output the elements stored in the list in order.
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;
Todo: add at least ten more test cases to test your code.
Todo: modify the list removing and adding elements and sort it again.
The use of data structures from Java libraries is allowed here and only here.
public static void mainString args
int testData try different inputs.
;
for int a : testData
int n alength;
SecureRandom random new SecureRandom;
ListOnArraydHeJimsonLiu list new ListOnArraydHeJimsonLiun 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
System.out.printlnThe internal storage: Arrays.toStringlistdata;
list.quicksort;
System.out.printlnThe sorted list: list;
Try the question in the attached photo
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
