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

Daa Asg

The document contains a Java program that implements the Quick Sort algorithm. It includes a partition method to rearrange elements and a quickSort method to recursively sort the array. The program outputs the original and sorted arrays for a given input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Daa Asg

The document contains a Java program that implements the Quick Sort algorithm. It includes a partition method to rearrange elements and a quickSort method to recursively sort the array. The program outputs the original and sorted arrays for a given input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

NAME VEMURI PRAVEENA

REGISTRATION NUMBER 22BCE7873


LAB SLOT L51+52
ASSIGNMENT NUMBER 2
DATE 19-1-2025

Exp 2: Java program to implement Quick Sort algorithm


CODE:
public class Main {

private 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;
}

]
private static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);


quickSort(arr, pi + 1, high);
}
}

public static void main(String[] args) {


int[] arr = {10, 7, 8, 9, 1, 5};
System.out.println("Original Array:");
for (int num : arr) {
System.out.print(num + " ");
}

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

System.out.println("\nSorted Array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
}
OUTPUT:
Original Array:

10 7 8 9 1 5

Sorted Array:

1 5 7 8 9 10

You might also like