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 6756481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:31:44+00:00 2026-05-26T13:31:44+00:00

Implementing the basic algorithm using last array as a pivot in Java, is it

  • 0

Implementing the basic algorithm using last array as a pivot in Java, is it normal for it take 5 hours for sorting a 100,000,000 element array of random numbers?

My system Specs:
Mac OS X Lion 10.7.2 (2011)
Intel Core i5 2.3 GHz
8GB ram

Update2: So I think I am doing something wrong in my other methods since Narendra was able to run the quicksort. Here is the full code I am trying to run.

import java.util.Random;

public class QuickSort {
public static int comparisons = 0;

public static void main(String[] args) {
    int size = 100000000;
    int[] smallSampleArray = createArrayOfSize(size);

    System.out.println("Starting QS1...");
    long startTime = System.currentTimeMillis();
    quickSort(smallSampleArray,0,size-1);
    System.out.println(  "Finished QS1 in " + (System.currentTimeMillis() - startTime)+ " seconds");
    System.out.println("Number of comparisons for QS1: " + comparisons);

}

public static int[] createArrayOfSize(int arraySize) {
    int[] anArray = new int[arraySize];
    Random random = new Random();

    for(int x=0; x < anArray.length; x++ ) {
        anArray[x] = random.nextInt(1000) + 1;;
    }
    return anArray;
}


public static void quickSort( int anArray[], int position, int pivot) {

    if( position < pivot ) {
        int q = partition(anArray, position, pivot);

        quickSort(anArray, position, q-1);
        quickSort(anArray, q+1, pivot);

    }

}

public static int partition(int anArray[], int position, int pivot ) {
    int x = anArray[pivot];
    int i = position - 1; 

    for(int j = position; j < (pivot-1); j++ ) {
        comparisons++;
        if(anArray[j] <= x) {
             i = i + 1;
             int temp =  anArray[i];
             anArray[i] = anArray[j];
             anArray[j] = temp;
        }

    }
    int temp = anArray[i+1];
    anArray[i+1] = anArray[pivot];
    anArray[pivot] = temp;



        return i+1;
    }

}
  • 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-05-26T13:31:45+00:00Added an answer on May 26, 2026 at 1:31 pm

    I’ve moved the old, now irrelevant answer to the end.

    Edit x2

    Aha! I think I’ve found the cause of your horrible performance. You told us you were using randomized data. That is true. But what you didn’t tell us is that you were using such a small range of possible random values.

    For me, your code is very performant if you change this line:

    anArray[x] = random.nextInt(1000) + 1;
    

    to this:

    anArray[x] = random.nextInt();    
    

    That goes against expectations, right? It should be cheaper to sort a smaller range of values, since there should be less swaps we need to do, right? So why does this happen? This happens because you have so many elements with the same value (on average, 100 thousand). So why does this lead to such horrible performance? Well, say at each point you chose a perfect pivot value: exactly halfway. Here’s what it would look like:

    1000 - Pivot: 500
     - 500+ - Pivot: 750
       - 750+ - Pivot: 875
       - 750- - Pivot: 625
     - 500- - Pivot: 250
    

    And so on. However (and here’s the critical part) you would eventually get to a partition operation where every single value is equal to the partition value. In other words, there will be a a big (100 thousand big) block of numbers with the same value that you will try to recursively sort. And how will that happen? It will recurse 100 thousand times, only removing the single pivot value at each level. In other words, it will partition everything to the left or everything to the right.

    Expanding on the breakdown above, it would look kind of like this (I’ve used 8–a power of 2–for simplicity, and forgive the bad graphical representation)

    Depth Min  Max  Pvt NumElements
    
    0     0     7    4   100 000 000
    1     0     3    2    50 000 000    
    2     0     1    1    25 000 000
    3     0     0    0    12 500 000 < at this point, you're
    4     0     0    0    12 499 999 < no longer dividing and
    5     0     0    0    12 499 998 < conquering effectively.
    3     1     1    1    12 500 000
    4     1     1    1    12 499 999
    5     1     1    1    12 499 998
    2     2     3    3    25 000 000
    3     ...    
    3     ...    
    1     4     7    6    50 000 000    
    2     4     5    5    25 000 000
    3     ...
    3     ...    
    2     6     7    7    25 000 000
    3     ...
    3     ... 
    

    If you want to counter this, you need to optimize your code to reduce the effects of this. More on that to come (I hope)…

    …and continued. An easy way to solve your problem is to check if the array is already sorted at each step.

    public static void quickSort(int anArray[], int position, int pivot) {
    
        if (isSorted(anArray, position, pivot + 1)) {
            return;
        }
    
        //...
    }
    
    
    private static boolean isSorted(int[] a, int start, int end) {
        for (int i = start+1; i < end; i++) {
            if (a[i] < a[i-1]) {
                return false;
            }
        }
        return true;
    }
    

    Add that and you won’t recurse unnecessarily and you should be golden. In fact, you get better performance than you do with values randomized over all 32 bits of the integer.


    Old answer (for posterity only)

    Your partitioning logic looks really suspect to me. Let’s extract and ignore the swap logic. Here’s what you have:

        int i = position - 1; 
    
        for(int j = position; j < pivot; j++ ) {
    
            if(anArray[j] <= x) {
                 i = i + 1;
                 swap(anArray, i, j);
            } 
    
        }
    

    I fail to see how this would work at all. For example, if the very first value were less than the pivot value, it would be swapped with itself?

    I think you want something like this (just a rough sketch):

    for ( int i = 0, j = pivot - 1; i < j; i++ ) {
    
       if ( anArray[i] > pivotValue ) {
          //i now represents the earliest index that is greater than the pivotValue,
          //so find the latest index that is less than the pivotValue
          while ( anArray[j] > pivotValue ) {
             //if j reaches i then that means that *all* 
             //indexes before i/j are less than pivot and all after are greater
             //and so we should break out here
             j--;
          }
    
          swap(anArray, i, j);
       }
    } 
    
    //swap pivot into correct position
    swap(anArray, pivot, j+1);
    

    Edit

    I think I understand the original partitioning logic now (I had confused the if-block to be looking at elements greater than the pivot). I’ll leave my answer up on the off chance that it delivers better performance but I doubt it would make a significant difference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

implementing publishActivity in PHP using the REST API using this code: $activity = array(
I'm currently playing with implementing various sorting algorithms in Java, mostly for fun, but
For a particular piece of homework, I'm implementing a basic data storage system using
I am implementing the huffman algorithm in C. I have got the basic functionality
I am implementing a basic chat server on Java. Actually I finished basic things
We are working implementing Single Sign On(SSO) using pingfederate . The basic implementation uses
I am currently implementing a basic raytracer in c++. Works pretty well so far,
I'm trying to grasp higher-order-polymophism in scala by implementing a very basic interface that
I have a basic form with controls that are databound to an object implementing
I am implementing a very simple file database. I have 2 basic operations: void

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.