Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8977071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:17:22+00:00 2026-06-15T19:17:22+00:00

I have the Following code from http://algs4.cs.princeton.edu that implements Quick Sort with Fast three-way

  • 0

I have the Following code from http://algs4.cs.princeton.edu that implements Quick Sort with Fast three-way partitioning on a set of random numbers between 0 and 1, However i want to implement this on the following sets

  1. Ordered List
  2. Reverse order List
  3. A list containing the same value through out
  4. 25% of the List sorted

How do I create them in order to feed them to the method?

This is my current main() method invoking the tests:

public static void main(String[] args) {

    // generate array of N random reals between 0 and 1
    int N = Integer.parseInt(args[0]);
    Double[] a = new Double[N];
    for (int i = 0; i < N; i++) {
        a[i] = Math.random();
    }

    // sort the array
    sort(a);

    // display results
    for (int i = 0; i < N; i++) {
        System.out.println(a[i]);
    }
    System.out.println("isSorted = " + isSorted(a));

}

The rest of the code (less relevant):

public class QuickX {
private static final int CUTOFF = 8;  // cutoff to insertion sort, must be >= 1

public static void sort(Comparable[] a) {
    sort(a, 0, a.length - 1);
}

private static void sort(Comparable[] a, int lo, int hi) { 
    int N = hi - lo + 1;

    // cutoff to insertion sort
    if (N <= CUTOFF) {
        insertionSort(a, lo, hi);
        return;
    }

    // use median-of-3 as partitioning element
    else if (N <= 40) {
        int m = median3(a, lo, lo + N/2, hi);
        exch(a, m, lo);
    }

    // use Tukey ninther as partitioning element
    else  {
        int eps = N/8;
        int mid = lo + N/2;
        int m1 = median3(a, lo, lo + eps, lo + eps + eps);
        int m2 = median3(a, mid - eps, mid, mid + eps);
        int m3 = median3(a, hi - eps - eps, hi - eps, hi); 
        int ninther = median3(a, m1, m2, m3);
        exch(a, ninther, lo);
    }

    // Bentley-McIlroy 3-way partitioning
    int i = lo, j = hi+1;
    int p = lo, q = hi+1;
    while (true) {
        Comparable v = a[lo];
        while (less(a[++i], v))
            if (i == hi) break;
        while (less(v, a[--j]))
            if (j == lo) break;
        if (i >= j) break;
        exch(a, i, j);
        if (eq(a[i], v)) exch(a, ++p, i);
        if (eq(a[j], v)) exch(a, --q, j);
    }
    exch(a, lo, j);

    i = j + 1;
    j = j - 1;
    for (int k = lo+1; k <= p; k++) exch(a, k, j--);
    for (int k = hi  ; k >= q; k--) exch(a, k, i++);

    sort(a, lo, j);
    sort(a, i, hi);
}


// sort from a[lo] to a[hi] using insertion sort
private static void insertionSort(Comparable[] a, int lo, int hi) {
    for (int i = lo; i <= hi; i++)
        for (int j = i; j > lo && less(a[j], a[j-1]); j--)
            exch(a, j, j-1);
}


// return the index of the median element among a[i], a[j], and a[k]
private static int median3(Comparable[] a, int i, int j, int k) {
    return (less(a[i], a[j]) ?
           (less(a[j], a[k]) ? j : less(a[i], a[k]) ? k : i) :
           (less(a[k], a[j]) ? j : less(a[k], a[i]) ? k : i));
}

// is v < w ?
private static boolean less(Comparable v, Comparable w) {
    return (v.compareTo(w) < 0);
}

// does v == w ?
private static boolean eq(Comparable v, Comparable w) {
    return (v.compareTo(w) == 0);
}

// exchange a[i] and a[j]
private static void exch(Object[] a, int i, int j) {
    Object swap = a[i];
    a[i] = a[j];
    a[j] = swap;
}


 private static boolean isSorted(Comparable[] a) {
    for (int i = 1; i < a.length; i++)
        if (less(a[i], a[i-1])) return false;
    return true;
}



// test client
public static void main(String[] args) {

    // generate array of N random reals between 0 and 1
    int N = Integer.parseInt(args[0]);
    Double[] a = new Double[N];
    for (int i = 0; i < N; i++) {
        a[i] = Math.random();
    }

    // sort the array
    sort(a);

    // display results
    for (int i = 0; i < N; i++) {
        System.out.println(a[i]);
    }
    System.out.println("isSorted = " + isSorted(a));

}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:17:23+00:00Added an answer on June 15, 2026 at 7:17 pm

    You can use Arrays.sort() to sort your array before feeding it, each of the following describe how to achieve one thing you asked for – try to do each on the array (one at a time) before invoking sort(a)

    To sort the array in ascending order:

    Arrays.sort(a);
    

    To sort the array in descending order:

    Arrays.sort(a,Collections.reverseOrder());
    

    To set constant value to each element in the array:

    for (int i = 0; i < a.length; i++) a[i] = 0.25;
    

    To partially sort a list (25% sorted):

    Arrays.sort(a,0,a.length/4);
    

    Bonus: to print the array (for debugging purposese usually):

    System.out.println(Arrays.toString(a));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From this site: http://www.toymaker.info/Games/html/vertex_shaders.html We have the following code snippet: // transformations provided by
I have implemented the following code from this page: http://tympanus.net/codrops/2010/06/02/smooth-vertical-or-horizontal-page-scrolling-with-jquery/ $(document).ready(function() { $('ul.navone li
I have the following code modified from the kendoui demo http://jsbin.com/alupin/6/ I am trying
I have the following code which I got from http://css-tricks.com/startstop-slider/ I have it all
I have the following code which I got from http://css-tricks.com/startstop-slider/ I would like to
I have the following code which I got from http://css-tricks.com/startstop-slider/ I would like to
I have the following UrlRewrite code to change from http to https in the
I have the following code (I'm working from code at http://www.linuxhowtos.org/C_C++/socket.htm ) which I'm
I've have the following code example from Microsoft (http://msdn.microsoft.com/en-us/library/bb534869.aspx): String[] fruits = {apple, banana,
I have the following code, obtained from http://jakearchibald.com/scratch/alphavid/ $(#video).append('<video id=movie style=display:none autobuffer><source id=srcMp4 src=https://host-away-from-host.com/file.mp4

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.