0% found this document useful (0 votes)
32 views14 pages

Quick Sort Algorithm Explained

The document provides an overview of the Quick Sort algorithm, including its history, methodology, and key components such as pivot selection and recursion. It outlines the advantages and disadvantages of Quick Sort, along with its time complexity and scenarios for its optimal use. Additionally, it includes pseudocode and a C++ implementation of the algorithm, demonstrating its practical application in sorting data.

Uploaded by

mekashfetene89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views14 pages

Quick Sort Algorithm Explained

The document provides an overview of the Quick Sort algorithm, including its history, methodology, and key components such as pivot selection and recursion. It outlines the advantages and disadvantages of Quick Sort, along with its time complexity and scenarios for its optimal use. Additionally, it includes pseudocode and a C++ implementation of the algorithm, demonstrating its practical application in sorting data.

Uploaded by

mekashfetene89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

....

BULE HORA UNIVERSITY


COLLEGE OF COMPUTING AND INFORMATICS
DEPARTMENT OF COMPUTER SCIENCE
COURSE TITLE : DATA STRUCTURE AND ALGORITHM
COURSE CODE:CoSc2091
ACADEMIC YEAR:2017/2025
PROGRAM: 2nd YEAR 2st SEMESTER(Regular )
GROUP(4) PROJECT
NAME:……………………………………...…….ID NO
1. MATIYOS EYASU…………………………...0132/16
2. MAHLET AYEMRO…………………………0096/16
[Link] LEMESA ………………………….. 0155/16
[Link] FETENE …………………………...0416/16
[Link] BIRA …………………..……………0341/16
[Link] DEJENE…………………..……………0266/16

Submission
Date 08/09/2017 E.C

Subm
Quick Sort Algorithm
Introduction
• The Quicksort algorithm was discovered by Sir Tony Hoare in 1959
while he was a visiting student at Moscow State University.
• Quick sort is one of the most widely used and efficient sorting
algorithms. Known for its speed and simplicity, the quick sort
algorithm is a fundamental concept for anyone learning DSA.
• This algorithm efficiently organizes data by dividing and conquering,
making it a preferred choice in various applications.
What is Quick Sort?
• Quick sort is a powerful sorting algorithm that uses the "divide and
conquer" strategy, recursively partitioning an array around a pivot
element until it's sorted.
• Quicksort is a divide-and-conquer algorithm. It works by selecting a
'pivot' element from the 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
• Quick sort is a method used to arrange a list of items, like numbers, in
order. It works by selecting one item from the list, called the "pivot,"
and then arranging the other items so that all the smaller items come
before the pivot and all the larger items come after it. This process is
repeated for the smaller groups of items until the entire list is sorted.
Key Data Structures and Algorithms
• Array:Quick sort operates on an array, a fundamental data structure for
storing sequential data.
• Pivot Selection:The algorithm's efficiency depends on choosing a good
pivot element. Strategies like randomly selecting a pivot or choosing the
first or last element are common.
• Partitioning:The core of quick sort is the partitioning step, where
elements are rearranged so that all elements less than the pivot are to
its left, and all elements greater are to its right.
• Recursion:Quick sort uses recursion, where the function calls itself to
sort smaller sub-arrays.
• Divide and Conquer:The algorithm follows the divide and conquer
paradigm, breaking down the problem into smaller subproblems (sub-
arrays) that are solved recursively, and then combining the solutions.
How QuickSort Works
• Algorithm
• Pick an element, called a pivot, from the array.
• Partitioning: reorder the array so that all elements with values less
than the pivot come before the pivot, while all elements with values
greater than the pivot come after it (equal values can go either way).
After this partitioning, the pivot is in its final position. This is called
the partition operation.
• Recursively apply the above steps to the sub-array of elements with
smaller values and separately to the sub-array of elements with
greater values.
• The base case of the recursion is arrays of size zero or one, which are
in order by definition, so they never need to be sorted.
How to Choice Pivot
• There are multiple variants of quick sort depending on the choice of
pivot. Some popular choices of pivots being:
• First element of the unsorted array
• Last element of the unsorted array
• Middle element of the unsorted array
• Random element from the unsorted array
• The choice of pivot determines the chances of the algorithm hitting
the worst case for the given array.
Pseudocode for Quick Sort Algorithm
function partition(arr, low, high):
pivot = arr[high] // Choose last element as pivot
i = low - 1 // Index of smaller element
for j = low to high - 1:
if arr[j] <= pivot:
i++
swap(arr[i], arr[j])
swap(arr[i + 1], arr[high]) // Place pivot in correct position
return i + 1 // Return pivot index
function quickSort(arr, low, high):
if low < high:
pivot_index = partition(arr, low, high)
quickSort(arr, low, pivot_index - 1) // Sort left subarray
quickSort(arr, pivot_index + 1, high) // Sort right subarray
Example of Quick Sort Algorthims
• Step-by-Step Sorting Process Step 3: Sort Right Subarray [7, 8, 6, 5] (Pivot = 5)
• Initial Array: [7, 2, 1, 8, 6, 3, 5, 4] Partitioning:
• Step 1: First Partition (Pivot = 4)
Elements ≤ 5: []
• Partitioning:
• Elements ≤ 4: [2, 1, 3] Elements > 5: [7, 8, 6]
Swap Pivot: [5, 7, 8, 6]
• Elements > 4: [7, 8, 6, 5]
• Swap Pivot: [2, 1, 3, 4, 7, 8, 6, 5] Pivot Index: 0
• Pivot Index: 3
Step 4: Sort Subarray [7, 8, 6] (Pivot = 6)
Partitioning:
• Step 2: Sort Left Subarray [2, 1, 3] (Pivot = 3)
• Partitioning: Elements ≤ 6: []
Elements > 6: [7, 8]
• Elements ≤ 3: [2, 1]
• Elements > 3: [] Swap Pivot: [6, 7, 8]
• Swap Pivot: [1, 2, 3]
Pivot Index: 0
• Pivot Index: 2
Final Sorted Array: [1, 2, 3, 4, 5, 6, 7, 8]
C++ Implementation
#include <iostream>
// QuickSort function
using namespace std; void quickSort(int arr[], int low, int high) {
// Function to swap two elements if (low < high) {
int pi = partition(arr, low, high); // Partitioning index
void swap(int* a, int* b) { quickSort(arr, low, pi - 1); // Sort left subarray
int temp = *a; quickSort(arr, pi + 1, high); // Sort right subarray
}}
*a = *b; // Function to print the array
*b = temp; } void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
// Partition function cout << arr[i] << " ";
}
int partition(int arr[], int low, int high) {
cout << endl;
int pivot = arr[high]; // Choose the last element as pivot }
int main() {
int i = (low - 1); // Index of smaller element
int arr[] = {7, 2, 1, 8, 6, 3, 5, 4};
for (int j = low; j <= high - 1; j++) { int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
if (arr[j] <= pivot) { printArray(arr, n);
i++; quickSort(arr, 0, n - 1);
cout << "Sorted array: ";
swap(&arr[i], &arr[j]);} } printArray(arr, n);
swap(&arr[i + 1], &arr[high]); return 0;
}
return (i + 1); }
Time Complexity

• Best Case: O(n log n) - when the pivot divides the array into nearly
equal parts.
• Average Case: O(n log n)
• Worst Case: O(n²) - when the pivot is always the smallest or largest
element.
• Space Complexity: O(log n) due to recursive [Link] can be O(n) in
the worst case.
Advantages of
QuickSort
1. Efficiency
- One of the fastest sorting algorithms in practice
- Average case time complexity of O(n log n)
- Often outperforms other O(n log n) algorithms like MergeSort due to smaller constant factors
2. Memory Efficiency
- Sorts in-place (only requires O(log n) additional space for recursion)
- Doesn't require auxiliary storage like MergeSort (which needs O(n) extra space)
3. Cache Performance - Excellent cache performance due to good locality of reference
- Accesses memory in a sequential manner during partitioning
4. Adaptability
- Can be easily parallelized for modern multi-core processors
- Works well with various data distributions
5. Practical Performance
- Typically 2-3 times faster than MergeSort and HeapSort for random data
Disadvantages of
QuickSort
1. Worst-case Performance
- Degrades to O(n²) in worst-case scenarios (already sorted/reverse sorted arrays)
- Though this can be mitigated with proper pivot selection strategies
2. Unstable Sort
- Does not maintain the relative order of equal elements
- Not suitable when stability is required (though stable variants exist)
3. Recursive Nature
- Uses recursion which can lead to stack overflow for very large arrays
- Though iterative implementations are possible
4. Pivot Selection Sensitivity
- Performance heavily depends on pivot selection strategy
- Poor pivot choices can lead to unbalanced partitions
5. Not Ideal for Small Arrays
- For very small arrays (n < 10-20), simpler algorithms like Insertion Sort may be faster
- Many implementations switch to Insertion Sort for small subarrays
When to Use QuickSort .
QuickSort is ideal when
- You need fast sorting for general-purpose data
- Memory is a concern (in-place sorting)
- Average-case performance is more important than worst-case
- Stability is not required
- The data is randomly ordered (not nearly sorted)
When to Avoid QuickSort
Consider other options when:
- You need a stable sort
- Worst-case performance is critical (use HeapSort)
- The data is already nearly sorted (use Insertion Sort or TimSort)
- Working with very large datasets where stack overflow might occur
- You're sorting linked lists (MergeSort works better)

You might also like