Thank you for printing our content at www.domain-name.com. Please check back soon for new
contents.
Get (https://www.programiz.com/learn-java?utm_campaign=programiz-
App homepage&utm_source=programiz-website-java-app-popup)
www.domain-name.com
miygvir
In this example, we will implement the quicksort algorithm in Java.
To understand this example, you should have the knowledge of the following Java
programming (/java-programming) topics:
» Java Methods (/java-programming/methods)
» Java for Loop (/java-programming/for-loop)
» Java Arrays (/java-programming/arrays)
Quicksort in Java
Quicksort algorithm is based on the divide and conquer approach where an array is
divided into subarrays by selecting a pivot element.
While dividing the array, the pivot element should be positioned in such a way that
elements less than pivot are kept on the left side and elements greater than pivot
are on the right side.
The same process is continued for both left and right subarrays. Finally, sorted
elements are combined to form a sorted array.
To learn more, visit Quicksort Algorithm (/dsa/quick-sort),
Example: Java Program to Implement Quick Sort AlgorithmThank you for printing our content at www.domain-name.com. Please check back soon for new
contents.
Get (https://www.programiz.com/learn-java2utm_campaign=programiz-
App homepage&utm_source=programiz-website-java-app-popup)
www.domain-name.com
Sede Ane parciuion(ame arrayts, ume dom, ane nigh) 4
// choose the rightmost element as pivot
int pivot = array(high];
// pointer for greater element
int i = (low - 1);
// traverse through all elements
// compare each element with pivot
for (int j = low; j < high; j++) ¢
if (array[j] <= pivot) {
// if element smaller than pivot is found
// swap it with the greater element pointed by i
is;
// swapping element at i with element at j
int temp = array[il;
arrayLi] = array(jl;
array[j] = temp;
Output
Unsorted Array
[8, 7, 2, 1, 0, 9, 6]
Sorted Array in Ascending Order
[0, 1, 2, 6, 7, 8, 91
Here, the elements of the array are sorted in ascending order. If we want to sort the
elements in descending order, then inside the ‘for loop, we can change the code
as:
// the less than sign is changed to greater than
if (array[j] >= pivot) {
Share on:
colhttps:/Awww.facebook.com/sharer/sharer.php? _(g\https://twitter.com/intent/tweet?Thank you for printing our content at www.domain-name.com. Please check back soon for new
contents.
Get (https://www.programiz.com/learn-java?utm_campaign=programiz-
App homepage&utm_source=programiz-website-java-app-popup)
www.domain-name.com
Related Examples
Jove Exomole
Implement Merge Sort Algorithm
(Gava-programming/examples/merge-sort)
ove Examole
Implement Bubble Sort algorithm
(ava-programming/examples/bubble-sort)
o Fxomole
Implement Binary Search Algorithm
(Gava-programming/examples/binary-search)
devo Boole
Find Largest Element of an Array
(/java-programming/examples/largest-array)Thank you for printing our content at www.domain-name.com. Please check back soon for new
contents.
Get (https://www.programiz.com/learn-java?utm_campaign=programiz-
® ~homepage&utm_source=programiz-website-java-app-popup)
www.domain-name.com