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

Quick Sort Example

The document provides a step-by-step example of the Quick Sort algorithm using an array of integers. It details the process of selecting a pivot, partitioning the array, and recursively sorting the left and right subarrays. The final result is a sorted array: [1, 2, 3, 4, 6, 7, 8, 9, 11, 15].

Uploaded by

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

Quick Sort Example

The document provides a step-by-step example of the Quick Sort algorithm using an array of integers. It details the process of selecting a pivot, partitioning the array, and recursively sorting the left and right subarrays. The final result is a sorted array: [1, 2, 3, 4, 6, 7, 8, 9, 11, 15].

Uploaded by

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

Quick Sort Example

Example Array:
[9, 1, 8, 3, 7, 2, 15, 11, 6, 4]

We’ll do Quick Sort using the last element as pivot.

Step 1: Choose pivot


Pivot = 4 (last element)
Partition array so elements ≤ pivot go left, > pivot go right:
• Left: [1, 3, 2]
• Pivot: [4]
• Right: [9, 8, 7, 15, 11, 6]

Array now:
[1, 3, 2, 4, 9, 8, 7, 15, 11, 6]

Step 2: Recursively Quick Sort left [1, 3, 2]


Pivot = 2
• Left: [1]
• Pivot: [2]
• Right: [3]

Array now:
[1, 2, 3]

Step 3: Recursively Quick Sort right [9, 8, 7, 15, 11, 6]


Pivot = 6
• Left: []
• Pivot: [6]
• Right: [9, 8, 7, 15, 11]

Further sorting:
1. Pivot = 15 → Left: [9, 8, 7, 11], Pivot: 15, Right: []
2. Pivot = 11 → Left: [9, 8, 7], Pivot: 11, Right: []
3. Pivot = 7 → Left: [], Pivot: 7, Right: [9, 8]
4. Pivot = 8 → Left: [], Pivot: 8, Right: [9]

Sorted right:
[6, 7, 8, 9, 11, 15]

Step 4: Combine left, pivot, right


Left [1, 2, 3], Pivot [4], Right [6, 7, 8, 9, 11, 15]

Final Sorted Array:


[1, 2, 3, 4, 6, 7, 8, 9, 11, 15]

You might also like