Question

Sorting algorithm: quick sort

Exercise One (20 marks) Given the following program body for implementing Quick sort, complete the program by writing code wh

public static void main(String langs) QuickSort gs = new QuickSort(); int[] A = 9,0,1,3,4,5,29,8,7,6,5,9,1,0,9); System.out.p

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

/*
 *  QuickSort.java
 */

import java.util.Random;

public class QuickSort
{
  public void quickSort(int A[])
  {
    quickSort(A, 0, A.length - 1);
  }

  private void quickSort(int A[], int low, int high)
  {
    if (low < high)
    {
      int pi = partition(A, low, high);
      quickSort(A, low, pi-1);
      quickSort(A, pi+1, high);
    }
  }

  private void swap(int[] A, int index1, int index2)
  {
    int temp = A[index1];
    A[index1] = A[index2];
    A[index2] = temp;
  }

  private int getPivot(int low, int high)
  {
    Random rand = new Random();
    return rand.nextInt((high-low) + 1) + low;
  }

  private int partition(int A[], int low, int high)
  {
    int piv_indx = getPivot(low, high);
    int pivot = A[piv_indx];
    swap(A, high, piv_indx);
    int i = (low - 1);
    for (int j = low; j < high; j++)
    {
      if (A[j] < pivot)
      {
        i++;
        swap(A, i, j);
      }
    }
    swap(A, i+1, high);
    return i+1;
  }
}






/*
 *  Main.java
 */

class Main
{
  public static void main(String args[])
  {
    QuickSort qs = new QuickSort();

    int[] A = {9,0,1,3,4,5,2,9,8,7,6,5,9,1,0,9};

    System.out.println("The unsorted array is: ");
    for (int i = 0; i < A.length; i++)
      System.out.print(A[i] + " ");

    qs.quickSort(A);
    System.out.println();

    System.out.println("The sorted array using quick sort is: ");
    for (int i = 0; i < A.length; i++)
      System.out.print(A[i] + " ");

  }
}

@imtusharsharma/SoulfulwordyStartup No description invite 2+ share new repl run saved Main.java Files http://SoulfulWordyStar

Add a comment
Know the answer?
Add Answer to:
Sorting algorithm: quick sort Exercise One (20 marks) Given the following program body for implementing Quick...
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
  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • 6) Assume that we are using quick sort algorithm to sort the following elements in the...

    6) Assume that we are using quick sort algorithm to sort the following elements in the array 26, 15,30,11,8,17 22, 40, 4, 10. Use the first element in the array as pivot. (20 pts.) 1- How total iterations it would take to complete the sorting process? 2- Simulate the entire sorting process. (If you need additional space, complete it at the other side of the paper) public static void quick_sort(intl] a, int left, int right) if (left < right) (...

  • Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8...

    Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...

  • 7.11 LAB: Sorting user IDs Given a main() that reads user IDs (until -1), complete the...

    7.11 LAB: Sorting user IDs Given a main() that reads user IDs (until -1), complete the quicksort() and partition() methods to sort the IDs in ascending order using the Quicksort algorithm, and output the sorted IDs one per line. Ex. If the input is: kaylasimms julia myron1994 kaylajones -1 the output is: julia kaylajones kaylasimms myron1994 Code: import java.util.Scanner; import java.util.ArrayList; public class UserIDSorting { // TODO: Write the partitioning algorithm - pick the middle element as the // pivot,...

  • Answer please Exercice 3(25+ 20 pts); Sorting 1-Sorting is a classic subject in computer science. There...

    Answer please Exercice 3(25+ 20 pts); Sorting 1-Sorting is a classic subject in computer science. There are many sorting algorithms a. Complete the below code b.Add comments to the below code to explain each statement c. Write a main test then give the output screen of it 2- a-Write the following two generic methods using the below code. The first method sorts the elements using the Comparable interface and the second uses the Comparator interface. public static <E extends Comparable<E>>...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers:...

    JAVA- Trace the recursive quick sort and partition methods in Lab6.java for this list of numbers: 47 71 15 35 66 61 44 26 68 56 18 19 36 84 69 55 1. Find the value of pivot 2. Show the result after partitionIt() is called first time 3. Show the value of partition 4. Show the content of the array ///////////////////////////// Lab6.java class ArrayIns { private long[] theArray; // ref to array theArray private int nElems; // number of...

  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Jav...

    Your running times will probably be different than these. Please do a better job with the snipping tool than I did. Java program provided: // Student Name Today's Date import java.util.Arrays; import java.util.Random; public class SortTimer {    // Please expand method main() to meet the lab requirements.       // You have the following sorting methods available:    // insertionSort(int[] a);    // selectionSort(int[] a);    // mergeSort(int[] a);    // quickSort(int[] a);    // The array will be in sorted order after the routines are called!   ...

  • //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded....

    //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded. However, //for these algorithms to work correctly, the data objects must //be compared using the method compareTo and equals. //In other words, the classes implementing the list objects //must implement the interface Comparable. The type parameter T //is unbounded because we would like to use these algorithms to //work on an array of objects as well as on objects of the classes //UnorderedArrayList and...

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
ADVERTISEMENT