0% found this document useful (0 votes)
35 views

Java Quick Sort

This Java code implements the quicksort algorithm to sort an integer array. It takes user input for the array size and elements, calls the quicksort method with partitioning to recursively sort the array, and outputs the sorted array. The quicksort method first calls the partition method to choose a pivot element and rearrange the array, then recursively calls itself on the subarrays smaller than and greater than the pivot.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Java Quick Sort

This Java code implements the quicksort algorithm to sort an integer array. It takes user input for the array size and elements, calls the quicksort method with partitioning to recursively sort the array, and outputs the sorted array. The quicksort method first calls the partition method to choose a pivot element and rearrange the array, then recursively calls itself on the subarrays smaller than and greater than the pivot.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;

public class Main {

static int partition(int arr[], int low, int high) {

int pivot = arr[low];

int start = low;

int end = high;

while (start < end) {

while (arr[start] <= pivot && start < high)

start++;

while (arr[end] > pivot && end > low)

end--;

if (start < end) {

int temp = arr[start];

arr[start] = arr[end];

arr[end] = temp;

int temp = arr[low];

arr[low] = arr[end];

arr[end] = temp;

return end;

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

if (low < high) {

int m = partition(arr, low, high);


quickSort(arr, low, m - 1);

quickSort(arr, m + 1, high);

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter size of array: ");

int size = sc.nextInt();

int arr[] = new int[size];

for (int i = 0; i < arr.length; i++) {

arr[i] = sc.nextInt();

quickSort(arr, 0, size - 1);

for (int i = 0; i < arr.length; i++) {

System.out.print(arr[i] + " ");

OUTPUT:-

You might also like