[go: up one dir, main page]

0% found this document useful (0 votes)
10 views1 page

Quick Sort

The document contains a Java implementation of the QuickSort algorithm, which includes methods for partitioning the array and performing the sort. It also features a method to print the array before and after sorting. The main method demonstrates the sorting of a sample array.

Uploaded by

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

Quick Sort

The document contains a Java implementation of the QuickSort algorithm, which includes methods for partitioning the array and performing the sort. It also features a method to print the array before and after sorting. The main method demonstrates the sorting of a sample array.

Uploaded by

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

public class QuickSort {

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


int pivot = arr[high];
int i = (low - 1);

for (int j = low; j < high; 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;
}

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


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

public static void printArray(int[] arr) {


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

public static void main(String[] args) {


int[] arr = {10, 80, 30, 90, 40, 50, 70};

System.out.println("Original array:");
printArray(arr);

quickSort(arr, 0, arr.length - 1);

System.out.println("Sorted array:");
printArray(arr);
}
}

You might also like