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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:37:36+00:00 2026-06-14T20:37:36+00:00

Possible Duplicate: Quick Sort with random pivot in Java The below written code of

  • 0

Possible Duplicate:
Quick Sort with random pivot in Java

The below written code of the Quicksort uses the first element of the array as the pivot and then sorts the array. Now I want to randomly pick up the pivot instead of first one and then sort the array and I am stuck please tell me what changes I can make in the below code to get the perfect results.

import java.util.*;
import javax.swing.JOptionPane;

public class Quicksort {

public static void main(String[] args) {
    String arraylength = JOptionPane.showInputDialog("Enter the length of the array.");

    int a = Integer.parseInt(arraylength);
    if (a == 0) {
        System.out.println("Null Length");
    } else {
        int[] list = new int[a];


        for (int i = 0; i < a; i++) {
            String input = JOptionPane.showInputDialog("Input the number.");
            int c = Integer.parseInt(input);
            list[i] = c;
        }

        System.out.println("Before");
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + " ");
        }
        partition(list, 0, list.length - 1);


        System.out.println("\nAfter partitionaing");
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + " ");
        }
        quickSort(list, 0, list.length - 1);

        System.out.println("\nAfter Sorting");
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + " ");
        }
    }
}

private static int partition(int[] list, int first, int last) {
    int pivot = list[first];
    int low = first + 1;
    int high = last;

    while (high > low) {

        while (low < high && list[low] < pivot) {
            low++;
        }


        while (low < high && list[high] >= pivot) {
            high--;
        }


        if (high > low) {
            int temp = list[high];
            list[high] = list[low];
            list[low] = temp;
        }
    }
    while (high > first && list[high] >= pivot) {
        high--;
    }

    if (pivot > list[high]) {
        list[first] = list[high];
        list[high] = pivot;
        return high;
    } else {
        return first;
    }

}

private static void quickSort(int[] list, int first, int last) {
    if (last > first) {
        int pivotIndex = partition(list, first, last);
        quickSort(list, first, pivotIndex - 1);
        quickSort(list, pivotIndex + 1, last);
    }
}
}
  • 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-14T20:37:38+00:00Added an answer on June 14, 2026 at 8:37 pm

    You can use the Random class from java:

    Random rand = new Random();
    int num = begin_sub_array + rand.nextInt(end_sub_array - begin_sub_array);
    

    This will generate a value from beginning of the sub array (begin_sub_array) to end of the sub array (end_sub_array). You just have to take the variable num, and pass as the pivot in your quicksort algorithm.

    int pivot = list[num];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to sort an array of javascript objects? Every time I mess
Possible Duplicate: quick selection of a random row from a large table in mysql
Possible Duplicate: Why java Arrays use two different sort algorithms for different types? So
Possible Duplicate: How to append data to a file? Quick question here, how can
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: Rails primary key and object id Very quick question. My server is
Possible Duplicate: class << self idiom in Ruby I have a quick Ruby question.
Possible Duplicate: How to profile my code? Whats are best practices and tools for
Possible Duplicate: best way to clear contents of .NET’s StringBuilder Is there a quick

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.