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

Quick Sort

The quicksort algorithm works by first picking a pivot element in an array. It then partitions the array into three parts - elements less than the pivot, elements equal to the pivot, and elements greater than the pivot. It recursively applies this process to the subarrays until the entire array is sorted. The average time complexity is O(n log n) but the worst case is O(n^2) if the pivot element is always the smallest or largest. It is an in-place sorting algorithm requiring no additional memory.

Uploaded by

Varun Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Quick Sort

The quicksort algorithm works by first picking a pivot element in an array. It then partitions the array into three parts - elements less than the pivot, elements equal to the pivot, and elements greater than the pivot. It recursively applies this process to the subarrays until the entire array is sorted. The average time complexity is O(n log n) but the worst case is O(n^2) if the pivot element is always the smallest or largest. It is an in-place sorting algorithm requiring no additional memory.

Uploaded by

Varun Sharma
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 3

QUICK SORT

ALGORITHM:

1. Pick an element in the array as the pivot element.

2. Make a pass to the array, called the PARTITION step, which rearranges the elements in the
array:

a. The pivot element is in the proper place

b. The elements less than pivot element are on the left of it

c. The elements greater than pivot element are on the right of it

3. Recursively apply the above process to the left and right part of the pivot element.

CODE:

public class QuickSortAlgorithm {

protected static int A[];

public void sort(int[] A) {

if (A == null || A.length == 0)

return;

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

public void quicksort(int[] A, int left, int right) {

int pivot = A[left + (right - left) / 2];

int i = left;

int j = right;

while (i <= j) {

while (A[i] < pivot) {

i++;

while (A[j] > pivot) {


j--;

if (i <= j) {

exchange(i, j);

i++;

j--;

if(left < j)

quicksort(A,left,j);

if(i < right)

quicksort(A,i,right);

public void exchange(int i, int j){

int temp=A[i];

A[i]=A[j];

A[j]=temp;

public String toString() {

String s = "";

s += "[" + A[0];

for (int i = 1; i < A.length; i++) {

s += ", " + A[i];

s += "]";
return s;

Complexity of QuickSort

Worst Case : O(N^2)

This happens when the pivot is the smallest or the largest element. Then one of the partition is
empty and we repeat the recursion for N-1 elements

Best Case: O(NlogN)

This is when the pivot is the median of the array and the left and right part are the of the same
size.There are logN partitions and to compare we do N comparisions

Advantages

* One of the fastest algorithm on average.

* Doesn't need additional memory i.e. it's an in-place sorting algorithm.

Disadvantages

Worst Case complexity is O(N^2).

Comparision to Merge Sort:

* Merge Sort guarantee O(NlogN) time, however it requires additional memory with size N.

* Quick Sort doesn't require additional memory but the running time is not guaranteed.

* Usually Merge Sort is not used for Memory Sorting. Its used only for external memory sorting.

You might also like