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

3 DSA Quick Sort

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)
5 views

3 DSA Quick Sort

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/ 4

Quick sort:

Quick Sort is a highly efficient sorting algorithm that uses a divide-and-


conquer strategy. It works by selecting a "pivot" element from an array
and partitioning the other elements into two sub-arrays according to
whether they are less than or greater than the pivot. The sub-arrays are
then sorted recursively.

1. Divide-and-Conquer Approach

 Divide: Choose a pivot and partition the array into two halves.

 Conquer: Recursively sort the sub-arrays.

 Combine: As the sub-arrays are sorted in place, no need for a


combining step.

2. Choosing a Pivot

The choice of pivot can greatly affect performance. Common strategies


include:

 First element: Simple but can lead to poor performance on already


sorted arrays.

 Last element: Similar issues as the first.

 Random element: Helps avoid worst-case scenarios.

 Median-of-three: Choose the median of the first, middle, and last


elements, providing a good pivot.

3. Partitioning

The process of rearranging elements around the pivot:

 Elements less than the pivot move to the left.

 Elements greater than the pivot move to the right.

 After partitioning, the pivot is in its correct position.

4. Recursive Sorting

Once partitioned:

 Apply Quick Sort to the left sub-array.

 Apply Quick Sort to the right sub-array.

5. Base Case
The recursion terminates when the size of the sub-array is one or zero, as
these are already sorted.

Key Characteristics:

 Stability:

o Not Stable: Quick sort is not a stable sort, meaning that equal
elements may not maintain their relative order after sorting.

 In-Place:

o Quick sort is an in-place sorting algorithm. It requires only a


small, constant amount of additional storage space, making it
memory efficient.

 Comparison-Based:

o Quick sort is a comparison-based sorting algorithm. It


compares elements to determine their order.

 Adaptive:

o Not Adaptive: Quick sort does not adapt to the order of input
data. Its performance remains consistent regardless of the
input order.

 Recursive:

o Quick sort is a recursive algorithm. It works by recursively


dividing the array into smaller sub-arrays until base cases are
reached (usually arrays of size 0 or 1).

Complexity:

 Time Complexity:
o Best Case: O(nlogn) (when the pivot divides the array into
two equal halves)
o Average Case: O(nlogn) (randomly selecting pivots leads to
balanced partitions)
o Worst Case: O(n2) (occurs when the smallest or largest
element is always chosen as the pivot, leading to unbalanced
partitions, e.g., sorted or reverse-sorted input)
 Space Complexity:
o Best/Average/Worst Case: O(logn) due to recursive stack
space in typical implementations.
Quick Sort Algorithm Steps

FUNCTION quicksort(arr, low, high)

IF low < high THEN

// Step 1: Take pivot as the last element in the array

pivotIndex = partition(arr, low, high)

// Step 2: Recursively call Quicksort on the left subarray

quicksort(arr, low, pivotIndex - 1)

// Step 3: Recursively call Quicksort on the right subarray

quicksort(arr, pivotIndex + 1, high)

Procedure partition(arr, low, high)

1. Set Initial Indices:

o Assign low to the starting index of the array.

o Assign high to the last index of the array.

2. Initialize Variables:

o Set j to low - 1.

o Initialize i to low.

3. Choose Pivot:

o Set pivot as the last element of the array (i.e., arr[high]).

4. Traverse the Array:

o Loop through the array using i from low to high - 1:

 If arr[i] < pivot, do the following:


 Increment j by 1.

 Swap arr[j] and arr[i].

5. Final Swap:

o After the loop, swap arr[j + 1] and arr[high] to place the pivot
in its correct position.

6. Return:

o Return j + 1, which is the final index of the pivot.

You might also like