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

Java QuickSort Algorithm Example

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)
5 views1 page

Java QuickSort Algorithm Example

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

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 < [Link]; i++) {
[Link](arr[i] + " ");
}
[Link]();
}

public static void main(String[] args) {


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

[Link]("Original array:");
printArray(arr);

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

[Link]("Sorted array:");
printArray(arr);
}
}

Common questions

Powered by AI

The main method sets up an example array and demonstrates QuickSort's functionality by first printing the original unsorted array. After calling quickSort with appropriate parameters, it then prints the sorted array. This process showcases how QuickSort reorders the array elements in ascending fashion and serves as a straightforward verification of the algorithm's correct implementation and expected output.

The recursion in QuickSort terminates when the condition 'if (low < high)' is not met. This occurs when each subarray being considered has one or zero elements, indicating that the segments are fully sorted. At this point, no further partitioning or sorting is needed, allowing the recursion to cease naturally as it backtracks through completed partitions.

In the given QuickSort implementation, the pivot element is selected by choosing the last element of the array segment being sorted: 'int pivot = arr[high]'. This technique is often used due to its simplicity in implementation and helps in partitioning the array around this pivot, ensuring that all elements less than or equal to the pivot are on its left and all elements greater are on its right. However, this method can lead to poor performance if the pivot being chosen does not effectively split the array, such as in case of already sorted data.

The 'partition' method is critical in dividing the array into subarrays around a pivot element. It organizes the array so that all elements less than or equal to the pivot are moved to the left of the pivot and all elements greater than the pivot are moved to the right. This method returns the index of the pivot after placement, which is then used to recursively apply QuickSort on the two subarrays (left and right of the pivot) until the entire array is sorted.

Swapping elements in the partition function is crucial for organizing the array into sections that articulate the divide-and-conquer essence of QuickSort. By moving elements around the pivot such that items less than or equal to the pivot are on the left, and those greater are on the right, it ensures that subsequent recursive quicksSort calls operate on subarrays where elements are increasingly closer to their final position. This localized sorting minimizes the array segments' disorder, allowing for efficient array sorting in the aggregate.

QuickSort uses a divide-and-conquer approach to efficiently sort by recursively partitioning arrays, achieving an average time complexity of O(n log n). It focuses on recursively breaking down the problem, unlike Bubble Sort, which iterates through the array multiple times, comparing adjacent elements and swapping if out of order, achieving a worst-case time complexity of O(n^2). QuickSort generally performs better on average due to its efficient recursive partitioning strategy, while Bubble Sort is often considered inefficient for large datasets.

When the pivot is poorly chosen, such as when an extreme (smallest or largest) element is consistently chosen in a sorted or nearly sorted array, QuickSort can degrade to a time complexity of O(n^2). This happens because the partitions become highly unbalanced, effectively reducing the size of each recursive call by only one element, unlike the balanced partitions that lead to the average O(n log n) complexity.

QuickSort might be preferred over MergeSort because it is an in-place sort requiring no additional memory allocation, which can be crucial in memory-constrained environments. QuickSort usually has better cache performance, as it tends to access memory sequentially rather than the scattered accesses seen with MergeSort. Though both have average time complexities of O(n log n), QuickSort's practical speeds and memory considerations can make it more suitable in circumstances requiring lower memory footprint.

Choosing the last element as the pivot can cause unbalanced partitions, especially in already sorted or nearly sorted data. This leads to a worst-case time complexity of O(n^2) as the algorithm repeatedly operates on increasingly smaller subarrays. Unbalanced partitions diminish the divide-and-conquer effectiveness of QuickSort, making it akin to a simple, inefficient insertion sort in such scenarios.

Modifying the pivot selection strategy to select a median-of-three (choosing the median of the first, middle, and last elements) can improve the QuickSort algorithm by reducing the likelihood of encountering the worst-case scenario of O(n^2) time complexity. This approach aims to create a more balanced partition, evenly dividing the array into subarrays of similar size, thus maintaining the O(n log n) average complexity more consistently.

You might also like