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

Quick Sort Algorithm

The document describes quicksort, a divide and conquer algorithm that makes n log n comparisons on average to sort an array of n elements. It explains the divide, conquer, and combine steps and provides pseudocode for quicksort and partition algorithms. It also analyzes the time and space complexity of quicksort.

Uploaded by

raja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Quick Sort Algorithm

The document describes quicksort, a divide and conquer algorithm that makes n log n comparisons on average to sort an array of n elements. It explains the divide, conquer, and combine steps and provides pseudocode for quicksort and partition algorithms. It also analyzes the time and space complexity of quicksort.

Uploaded by

raja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Experiment 3

2. Develop a program and measure the running time for Quick Sort with Divide and
Conquer
Aim: To develop a program and measure the running time for Quick Sort with Divide and
Conquer.

Quick Sort:
Sorting is a way of arranging items in a systematic manner. Quicksort is the
widely used sorting algorithm that makes n log n comparisons in average
case for sorting an array of n elements. It is a faster and highly efficient
sorting algorithm. This algorithm follows the divide and conquer approach.
Divide and conquer is a technique of breaking down the algorithms into
subproblems, then solving the subproblems, and combining the results back
together to solve the original problem.

Divide: In Divide, first pick a pivot element. After that, partition or rearrange
the array into two sub-arrays such that each element in the left sub-array is
less than or equal to the pivot element and each element in the right sub-array
is larger than the pivot element.

Conquer: Recursively, sort two subarrays with Quicksort.

Combine: Combine the already sorted array.

Quicksort picks an element as pivot, and then it partitions the given array
around the picked pivot element. In quick sort, a large array is divided into
two arrays in which one holds values that are smaller than the specified value
(Pivot), and another array holds the values that are greater than the pivot.

After that, left and right sub-arrays are also partitioned using the same
approach. It will continue until the single element remains in the sub-array.
Choosing the pivot
Picking a good pivot is necessary for the fast implementation of quicksort.
However, it is typical to determine a good pivot. Some of the ways of
choosing a pivot are as follows -

o Pivot can be random, i.e. select the random pivot from the given array.
o Pivot can either be the rightmost element of the leftmost element of the
given array.
o Select median as the pivot element.

Algorithm

QUICKSORT (array A, start, end)


{
if (start < end)
{
p = partition(A, start, end)
QUICKSORT (A, start, p - 1)
QUICKSORT (A, p + 1, end)
}
}

Partition Algorithm:

The partition algorithm rearranges the sub-arrays in a place.

PARTITION (array A, start, end)


{
pivot ? A[end]
i ? start-1
for j ? start to end -1 {
do if (A[j] < pivot) {
then i ? i + 1
swap A[i] with A[j]
}}
swap A[i+1] with A[end]
return i+1
}

Quicksort complexity

1. Time Complexity
Case Time Complexity
Best Case O(n*logn)
Average Case O(n*logn)
Worst Case O(n2)
o Best Case Complexity - In Quicksort, the best-case occurs when the
pivot element is the middle element or near to the middle element. The
best-case time complexity of quicksort is O(n*logn).
o Average Case Complexity - It occurs when the array elements are in
jumbled order that is not properly ascending and not properly
descending. The average case time complexity of quicksort
is O(n*logn).
o Worst Case Complexity - In quick sort, worst case occurs when the
pivot element is either greatest or smallest element. Suppose, if the
pivot element is always the last element of the array, the worst case
would occur when the given array is sorted already in ascending or
descending order. The worst-case time complexity of quicksort
is O(n2).

2. Space Complexity
Space Complexity O(n*logn)

o The space complexity of quicksort is O(n*logn).


PROGRAM CODE:

#include <stdio.h>
int partition (int a[], int start, int end)
{
int pivot = a[end]; // pivot element
int i = (start - 1);
for (int j = start; j <= end - 1; j++)
{
// If current element is smaller than the pivot
if (a[j] < pivot)
{
i++; // increment index of smaller element
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}

/* function to implement quick sort */


void quick(int a[], int start, int end) /* a[] = array to be sorted, start = Starting in
dex, end = Ending index */
{
if (start < end)
{
int p = partition(a, start, end); //p is the partitioning index
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}

/* function to print an array */


void printArr(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
}
int main()
{
int a[] = { 24, 9, 29, 14, 19, 27 };
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArr(a, n);
quick(a, 0, n - 1);
printf("\nAfter sorting array elements are - \n");
printArr(a, n);
return 0;
}

You might also like