SlideShare a Scribd company logo
USING JAVA:
Implement the quicksort optimization median-of-three, i.e., first, middle, and last, as pivot for
partition. Into this working quicksort algorithm: ( the spacing is bad, sorry)
private static void quicksort(int low, int high) {
int i = low, j = high;
// Get the pivot element from the middle of the list
int pivot = arr[(high+low)/2];
// Divide into two lists
while (i <= j) {
// If the current value from the left list is smaller then the pivot
// element then get the next element from the left list
while (arr[i] < pivot) i++;
// If the current value from the right list is larger then the pivot
// element then get the next element from the right list
while (arr[j] > pivot) j--;
// If we have found a value in the left list which is larger than
// the pivot element and if we have found a value in the right list
// which is smaller then the pivot element then we exchange the
// values.
// As we are done we can increase i and j
if (i < j) {
exchange(i, j);
i++;
j--;
} else if (i == j) { i++; j--; }
}
// Recursion
if (low < j)
quicksort(low, j);
if (i < high)
quicksort(i, high);
}
Solution
Hi, Please find my program:
private static void quicksort(int low, int high) {
if (low >= high)
return;
// Selecting the pivot
int first = low;
int second = arr[(high+low)/2];
int third = arr[high];
// median for first three
int pivot = Math.max(
Math.min(
array[first],
array[second]),
Math.min(
Math.max(
array[first],
array[second]
),
array[third]
)
);
while (low <= high)
{
while (array[low] < pivot)
{
low++;
}
while (array[high] > pivot)
{
high--;
}
if (low <= high)
{
// swapping
int temp = array[low];
array[low] = array[high];
array[high] = temp;
low++;
high--;
}
}
// Recursion
if (low < j)
quicksort(low, j);
if (i < high)
quicksort(i, high);
}

More Related Content

PDF
Sorting Algorithms and their implementations
DOCX
DAA Lab Work.docx
PDF
Maxima Finding problem In the 2-dimension space, we shall say that .pdf
DOCX
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
PDF
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
PDF
All code should be in C++Using the UnsortedList class (UnsortedLis.pdf
PPT
linked list (c#)
Sorting Algorithms and their implementations
DAA Lab Work.docx
Maxima Finding problem In the 2-dimension space, we shall say that .pdf
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
All code should be in C++Using the UnsortedList class (UnsortedLis.pdf
linked list (c#)

Similar to USING JAVAImplement the quicksort optimization median-of-three, i.pdf (6)

PDF
helpInstructionsAdd the function max as an abstract function to .pdf
PPTX
Quick and Heap Sort with examples
PPTX
Randomized select algorithm and advance data structure
PDF
Stacks,queues,linked-list
PDF
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
PDF
As presented, the tree summation algorithm was always illustrated wi.pdf
helpInstructionsAdd the function max as an abstract function to .pdf
Quick and Heap Sort with examples
Randomized select algorithm and advance data structure
Stacks,queues,linked-list
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
As presented, the tree summation algorithm was always illustrated wi.pdf
Ad

More from info785431 (20)

PDF
Carbon dioxide (CO_2) is a non-polar molecule. Is this consistent wit.pdf
PDF
Compare and contrast the messages about science and integrity in Fra.pdf
PDF
Briely, what was Darwins explanation for the appearance of design.pdf
PDF
A router receives a message addressed 172.16.15.75. The relevant rou.pdf
PDF
A study of sandflies in Panama classified flies caught in light traps.pdf
PDF
___Tissue found in the brain, spinal cord, and peripheral nerves. The.pdf
PDF
You have been exposed to each of the 8 microbes below. One of them h.pdf
PDF
Write a snippet of C code that will enable the ADC to continuously r.pdf
PDF
Write a JAVA LinkedListRec class that has the following methods siz.pdf
PDF
Write a BFS algorithm using only arrays and no other data structure..pdf
PDF
Why has one prominent textbook author described IO management as the.pdf
PDF
which is true of the data shown in the histogram Which is true of th.pdf
PDF
What is the value of studying humanities in a business or technical .pdf
PDF
What are the two components of dynamic pressureVelocity and densi.pdf
PDF
There are 40 students in our class. How many ways they can be lined .pdf
PDF
The Task For this assignment you will write a rudimentary text edi.pdf
PDF
The SIP handles what functionsA.) establishes a call through the .pdf
PDF
The NBA decides to look into the use of meldonium in the league foll.pdf
PDF
The organelle that serves as the digestive system in the cell is the .pdf
PDF
The major type of interactive forces between molecules of NH_3 are .pdf
Carbon dioxide (CO_2) is a non-polar molecule. Is this consistent wit.pdf
Compare and contrast the messages about science and integrity in Fra.pdf
Briely, what was Darwins explanation for the appearance of design.pdf
A router receives a message addressed 172.16.15.75. The relevant rou.pdf
A study of sandflies in Panama classified flies caught in light traps.pdf
___Tissue found in the brain, spinal cord, and peripheral nerves. The.pdf
You have been exposed to each of the 8 microbes below. One of them h.pdf
Write a snippet of C code that will enable the ADC to continuously r.pdf
Write a JAVA LinkedListRec class that has the following methods siz.pdf
Write a BFS algorithm using only arrays and no other data structure..pdf
Why has one prominent textbook author described IO management as the.pdf
which is true of the data shown in the histogram Which is true of th.pdf
What is the value of studying humanities in a business or technical .pdf
What are the two components of dynamic pressureVelocity and densi.pdf
There are 40 students in our class. How many ways they can be lined .pdf
The Task For this assignment you will write a rudimentary text edi.pdf
The SIP handles what functionsA.) establishes a call through the .pdf
The NBA decides to look into the use of meldonium in the league foll.pdf
The organelle that serves as the digestive system in the cell is the .pdf
The major type of interactive forces between molecules of NH_3 are .pdf
Ad

Recently uploaded (20)

PPTX
Lesson notes of climatology university.
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Classroom Observation Tools for Teachers
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PPTX
master seminar digital applications in india
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Presentation on HIE in infants and its manifestations
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
Lesson notes of climatology university.
human mycosis Human fungal infections are called human mycosis..pptx
01-Introduction-to-Information-Management.pdf
RMMM.pdf make it easy to upload and study
O7-L3 Supply Chain Operations - ICLT Program
Cell Types and Its function , kingdom of life
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Supply Chain Operations Speaking Notes -ICLT Program
FourierSeries-QuestionsWithAnswers(Part-A).pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Classroom Observation Tools for Teachers
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
master seminar digital applications in india
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
2.FourierTransform-ShortQuestionswithAnswers.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Presentation on HIE in infants and its manifestations
O5-L3 Freight Transport Ops (International) V1.pdf

USING JAVAImplement the quicksort optimization median-of-three, i.pdf

  • 1. USING JAVA: Implement the quicksort optimization median-of-three, i.e., first, middle, and last, as pivot for partition. Into this working quicksort algorithm: ( the spacing is bad, sorry) private static void quicksort(int low, int high) { int i = low, j = high; // Get the pivot element from the middle of the list int pivot = arr[(high+low)/2]; // Divide into two lists while (i <= j) { // If the current value from the left list is smaller then the pivot // element then get the next element from the left list while (arr[i] < pivot) i++; // If the current value from the right list is larger then the pivot // element then get the next element from the right list while (arr[j] > pivot) j--; // If we have found a value in the left list which is larger than // the pivot element and if we have found a value in the right list // which is smaller then the pivot element then we exchange the // values. // As we are done we can increase i and j if (i < j) { exchange(i, j); i++; j--; } else if (i == j) { i++; j--; } } // Recursion if (low < j) quicksort(low, j); if (i < high) quicksort(i, high); } Solution
  • 2. Hi, Please find my program: private static void quicksort(int low, int high) { if (low >= high) return; // Selecting the pivot int first = low; int second = arr[(high+low)/2]; int third = arr[high]; // median for first three int pivot = Math.max( Math.min( array[first], array[second]), Math.min( Math.max( array[first], array[second] ), array[third] ) ); while (low <= high) { while (array[low] < pivot) { low++; } while (array[high] > pivot) { high--; } if (low <= high) { // swapping int temp = array[low];
  • 3. array[low] = array[high]; array[high] = temp; low++; high--; } } // Recursion if (low < j) quicksort(low, j); if (i < high) quicksort(i, high); }