0% found this document useful (0 votes)
3 views2 pages

Java QuickSort Algorithm Example

Uploaded by

vibesofcollege
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Java QuickSort Algorithm Example

Uploaded by

vibesofcollege
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

Scanner;

public class QuickSort {


int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] <= pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return (i + 1);
}

void sort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
sort(arr, low, pi - 1);
sort(arr, pi + 1, high);
}
}

public static void main(String args[]) {


Scanner sc = new Scanner([Link]);
[Link]("Enter number of elements: ");
int n = [Link]();
int arr[] = new int[n];
[Link]("Enter elements:");
for (int i = 0; i < n; i++) {
arr[i] = [Link]();
}

QuickSort ob = new QuickSort();


[Link](arr, 0, n - 1);

[Link]("Sorted array:");
for (int i = 0; i < n; i++) {
[Link](arr[i] + " ");
}

}
}
Enter number of elements: 6
Enter elements:
10 80 30 90 40 50
Sorted array:
10 30 40 50 80 90

You might also like