Question

In: Computer Science

Hello so I have written this Quicksort code, but it only seems to work when the...

Hello so I have written this Quicksort code, but it only seems to work when the array size is 100 or less. If it goes over than it takes forever to run and never completes. Please help debug and fix.

public class Test {

public static void main(String[] args) {
int[] myArray = new int[]{5, 10, 12, 55, 24, 90, 52, 900};
System.out.println("Before Sort");
printArray(myArray);
System.out.println("Quicksorted");
quickSort(myArray, 0, myArray.length - 1);
printArray(myArray);

}

public static void quickSort(int array[], int low, int high) {

if (low < high) {
int partitionPoint = partition(array, low, high);
quickSort(array, low, partitionPoint - 1);
quickSort(array, partitionPoint + 1, high);
}
}

public static int partition(int array[], int low, int high) {

int pivot = array[low];
int i = low;
int j = high;

while (i < j) {
while (array[i] < pivot) {
i++;
}

while (array[j] > pivot) {
j--;
}

if (i < j) {
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}

int temp = array[j];
array[j] = pivot;
pivot = temp;

return j;

}

public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.println(array[i] + " ");
}
}
}

Thank you!

Solutions

Expert Solution

Code:

public class Test {
   public static void main(String[] args)
   {
       int[] myArray = new int[]{5, 10, 12, 55, 24, 90, 52, 900};
       System.out.println("Before Sort");
       printArray(myArray);
       System.out.println("Quicksorted");
       quickSort(myArray, 0, myArray.length - 1);
       printArray(myArray);
   }

   public static void quickSort(int array[], int low, int high)
   {
       if (low < high)
       {
           int partitionPoint = partition(array, low, high);
           quickSort(array, low, partitionPoint - 1);
           quickSort(array, partitionPoint + 1, high);
       }
   }

public static int partition(int array[], int low, int high)
{

   int pivot = array[low];
   int i ,p1=low+1,temp;
   for(i=low+1; i<=high;i++)
   {
       if(array[i]<pivot)
       {
           if(i!=p1)
           {
               temp = array[p1];
               array[p1] = array[i];
               array[i] = temp;
           }
           p1++;
       }
   }
   array[low]= array[p1-1];
   array[p1-1] = pivot;
   return p1-1;
}

   public static void printArray(int arr[])
   {
       for (int i = 0; i < arr.length; i++)
       {
           System.out.println(arr[i] + " ");
       }
   }
}

OutPut:

Before Sort
5
10
12
55
24
90
52
900
Quicksorted
5
10
12
24
52
55
90
900

//This Code will work for any size of Array.


Related Solutions

hello! So I have this CIS assignment lab but when I try to make the code...
hello! So I have this CIS assignment lab but when I try to make the code I don't really know where to start from. My professor is very hard and he likes to see the outcomes as they are shown in the problem. Please help me! Write a program that can be used as a math helper for an elementary student. The program should display two random integer numbers that are to be added, such as:     247 + 129...
hello! So I have this CIS assignment lab but when I try to make the code...
hello! So I have this CIS assignment lab but when I try to make the code I don't really know where to start from. My professor is very hard and he likes to see the outcomes as they are shown in the problem. Please help me! the program should be a C++ PLS Write a program that can be used as a math helper for an elementary student. The program should display two random integer numbers that are to be...
So I have written a code for it but i just have a problem with the...
So I have written a code for it but i just have a problem with the output. For the month with the highest temperature and lowest temperature, my code starts at 0 instead of 1. For example if I input that month 1 had a high of 20 and low of -10, and every other month had much warmer weather than that, it should say "The month with the lowest temperature is 1" but instead it says "The month with...
Hello, I have created the following code in C++. The goal of this code is to...
Hello, I have created the following code in C++. The goal of this code is to read 3 types of employee information(String/*Name*/, String/*Phone number*/,Int/*Office number*/, ) from a .txt file and store that information into an array which can be accessed by various functions. The code below is within a header file, and im trying to define some functions (within a .cpp file) of my class to carry out the following tasks. I have already managed to define a couple...
I have posted this earlier, but it seems that the code part was cutoff, therefore, this...
I have posted this earlier, but it seems that the code part was cutoff, therefore, this is re post with full code. In the program the "case: 'C'" doesn't seem to function correctly, We were told to write a book tracking program, and it takes inputs and once all the book info such as ISBN, date, author, course number, etc are put in then input, then we can print them out, by using the print functions that are defined in...
Hello I have this error in the code, I do not know how to fix it....
Hello I have this error in the code, I do not know how to fix it. It is written in C++ using a Eclipse IDE Error: libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: basic_string bus.h =========== #pragma once #include using namespace std; class Bus { private:    string BusId; // bus ID    string Manufacturer; // manufacturer of the bus    int BusCapacity; // bus capacity    int Mileage; // mileage of bus    char Status; // current status...
Code in python Hello I want a program to read only the price of each line...
Code in python Hello I want a program to read only the price of each line in a txt file in python. The price is present right after the 2nd comma within the txt file. The output that I want is also printed at the bottom. Inside the txt file: 2019-08-01 00:08:39,BTC/USD,10128.08 2019-08-01 00:08:21,BTC/USD,10117.54 2019-08-01 00:08:20,BTC/USD,10120.51 2019-08-01 00:08:19,BTC/USD,10118.13 2019-08-01 00:08:18,BTC/USD,10120.74 Python file output: Price 1: 10128.08 Price 2: 10117.54 Price 3: 10120.51 Price 4: 10118.13 Price 5: 10120.74
I need a randomized quicksort function written in c++ or java with dual pivots and a...
I need a randomized quicksort function written in c++ or java with dual pivots and a partition function
The code should be written in c++. It should be in only OpenGL // ***** ONLY...
The code should be written in c++. It should be in only OpenGL // ***** ONLY OpenGL PLEASE ******// write a program snippet that accepts the coordinates of the vertices of a rectangle centered around the origin, with edges that are parallel to the X and Y axes, and with aspect ratio W and converts it into a rectangle centered around the origin with aspect ratio of 1/W. The program has to figure W from the given points. You MUST...
***You can use a calculator but I just need the work written out so I can...
***You can use a calculator but I just need the work written out so I can see what you did and how you did it*** I need the answer for number 4 which is based on number 3. Please answer number 4 based on number 3. Number 4 can be found at the bottom. Thank you! 3. A business takes out a loan for $250,000 at 4.8% interest compounded monthly. If the business can afford to make monthly payments of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT