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

Quickshorting

The document contains a Java program that implements the QuickSort algorithm to sort an array of integers. It includes methods for partitioning the array, swapping elements, and printing the array before and after sorting. The main method initializes an array, calls the quicksort function, and displays the sorted result.

Uploaded by

Naru Sonawane
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)
2 views

Quickshorting

The document contains a Java program that implements the QuickSort algorithm to sort an array of integers. It includes methods for partitioning the array, swapping elements, and printing the array before and after sorting. The main method initializes an array, calls the quicksort function, and displays the sorted result.

Uploaded by

Naru Sonawane
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

package cdac.

in;

public class QuickSearch {


public static void main(String[] args) {
int[] numbers = {9, 5, 7, 2, 1, 8, 3, 6, 10, 4};
System.out.println("Before sorting:");
printArray(numbers);

quicksort(numbers, 0, numbers.length - 1);

System.out.println("After sorting:");
printArray(numbers);
}

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 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++;
swap(arr, i, j);
}
}

swap(arr, i + 1, high);
return i + 1;
}

public static void swap(int[] arr, int i, int j) {


int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

public static void printArray(int[] arr) {


for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}

You might also like