Question

Can I get help with implementing a quick sort in my program? Starter Code: import java.util.Random;...

Can I get help with implementing a quick sort in my program?

Starter Code:

import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;


import java.util.Collections;
import java.util.ArrayList;
public class RQS {
public static int[] prepareData(int[] patients){
/*
Data is prepared by inserting random values
between 1 and 1000. Data items may be assumed to
be unique.
Please refer to lab spec for the problem definiton.
*/
// what is our range?
int max = 1000;
// create instance of Random class
Random randomNum = new Random();

ArrayList<Integer> list = new ArrayList<Integer>();
for (int i=0; i<patients.length; i++) {
int ids = randomNum.nextInt(max);
list.add(ids);
}
Collections.shuffle(list);

for (int i=0; i<patients.length; i++) {
patients[i] = list.get(i);
}


return patients;
}
public static int[] sortArray(int[] patient_ids){
/*
Add your logic below to sort the array.*/

return patient_ids;
}

public static void main(String[] args) {
System.out.println("Enter the no of patients:");
Scanner scan = new Scanner(System.in);
int patients = scan.nextInt();
int[] patient_ids = new int[patients];
int[] patients_populated = prepareData(patient_ids);

/* The line below will print the output.
Do not uncomment this lines. */
System.out.println("Unsorted:\t" + Arrays.toString(patients_populated));

/* Implement the sortArray method, so as to
get the correct results.*/
int[] sorted_array = sortArray(patients_populated);

/* The line below will print the output.
Do not uncomment this lines. */
System.out.println("Sorted:\t" + Arrays.toString(sorted_array));

}
}

The output of the program should be generated using the ascending order format. Make necessary code modifications to this starter-code file. Read through the comments in the code file for additional information on the structure of the code. I could really use some help with this will give a thumbs up for anybody that can help.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


import java.util.Random;
import java.util.Scanner;
import java.util.Arrays;


import java.util.Collections;
import java.util.ArrayList;
public class RQS {
public static int[] prepareData(int[] patients){
/*
Data is prepared by inserting random values
between 1 and 1000. Data items may be assumed to
be unique.
Please refer to lab spec for the problem definiton.
*/
// what is our range?
int max = 1000;
// create instance of Random class
Random randomNum = new Random();

ArrayList<Integer> list = new ArrayList<Integer>();
for (int i=0; i<patients.length; i++) {
int ids = randomNum.nextInt(max);
list.add(ids);
}
Collections.shuffle(list);

for (int i=0; i<patients.length; i++) {
patients[i] = list.get(i);
}


return patients;
}
public static int[] sortArray(int[] patient_ids){
/*
Add your logic below to sort the array.*/
   qsort(patient_ids,0,patient_ids.length-1);
return patient_ids;
}

private static void qsort(int [] ar,int l,int r){
   int i=l,j=r;
   int x=0,y=0;
  
   // store middle element in x
   x=ar[(l+r)/2];
   do{
       // get position of x within array
       while(ar[i]<x && i<r){i++;}
       while(x<ar[j] && j>l ){j--;}
          
       // replace i and j
       if(i<=j){
           y=ar[i];
           ar[i]=ar[j];
           ar[j]=y;
           i++;
           j--;
       }      
   }while(i<=j);
  
   // sort within l and j
   if(l<j) qsort(ar,l,j);
  
   // sort within i and r
   if(i<r) qsort(ar,i,r);
}

public static void main(String[] args) {
System.out.println("Enter the no of patients:");
Scanner scan = new Scanner(System.in);
int patients = scan.nextInt();
int[] patient_ids = new int[patients];
int[] patients_populated = prepareData(patient_ids);

/* The line below will print the output.
Do not uncomment this lines. */
System.out.println("Unsorted:\t" + Arrays.toString(patients_populated));

/* Implement the sortArray method, so as to
get the correct results.*/
int[] sorted_array = sortArray(patients_populated);

/* The line below will print the output.
Do not uncomment this lines. */
System.out.println("Sorted:\t" + Arrays.toString(sorted_array));

}
}

Add a comment
Know the answer?
Add Answer to:
Can I get help with implementing a quick sort in my program? Starter Code: import java.util.Random;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Look for some finshing touches java help with this program. I just two more things added...

    Look for some finshing touches java help with this program. I just two more things added to this code. A loop at the end that will ask the user if they want to quit if they do want to quit the program stops if they don't it loads a new number sequence. import java.util.Random; import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.Collections; public class main { public static void main(String[] args) { List < Sequence > list =...

  • Need help with this binary tree program. I will give a thumbs up for anybody who...

    Need help with this binary tree program. I will give a thumbs up for anybody who helps. Source Code: import java.util.Random; import java.util.Scanner; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class Trees { public static int[] prepareData(int[] data){ /* Data is prepared by inserting random values between 1 and data.length. Data items may be assumed to be unique. Please refer to lab spec for the problem definiton. */ ArrayList list = new ArrayList(); for (int i=0; i< data.length; i++) {...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Hi All, Can someone please help me correct the selection sort method in my program. the...

    Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • How can I make this program sort strings that are in a text file and not...

    How can I make this program sort strings that are in a text file and not have the user type them in? I need this done by 5:00 AM EST tommorow please. import java.util.*; public class MergeDemo { public static void main(String args[]) { Scanner input=new Scanner(System.in); System.out.print("How many lines to be sorted:"); int size=input.nextInt(); String[] lines=new String[size]; lines[0]=input.nextLine(); System.out.println("please enter lines..."); for(int i=0;i { lines[i]=input.nextLine(); } System.out.println(); System.out.println("Lines Before Sorting:"); System.out.println(Arrays.toString(lines)); mergeSort(lines); System.out.println(); System.out.println("Lines after Sorting:"); System.out.println(Arrays.toString(lines)); } public...

  • This is my current output for my program. I am trying to get the output to...

    This is my current output for my program. I am trying to get the output to look like This is my program Student.java import java.awt.GridLayout; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.swing.JFileChooser; public class Student extends javax.swing.JFrame {     BufferedWriter outWriter;     StudentA s[];     public Student() {         StudentGUI();            }     private void StudentGUI() {         jScrollPane3 = new javax.swing.JScrollPane();         inputFileChooser = new javax.swing.JButton();         outputFileChooser = new javax.swing.JButton();         sortFirtsName = new...

  • Can someone help me with my code.. I cant get an output. It says I do...

    Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...

  • Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

    Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car {    private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...

  • Please help me to solve the problem with java language! An implementation of the Merge Sort...

    Please help me to solve the problem with java language! An implementation of the Merge Sort algorithm. Modify the algorithm so that it splits the list into 3 sublists (instead of two). Each sublist should contain about n/3 items. The algorithm should sort each sublist recursively and merge the three sorted sublists. The traditional merge sort algorithm has an average and worst-case performance of O(n log2 n). What is the performance of the 3-way Merge Sort algorithm? Merge Sort algorithm...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT