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

Quick Sort

quick sort

Uploaded by

rajkumar.rspb
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)
8 views

Quick Sort

quick sort

Uploaded by

rajkumar.rspb
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

Overview of Quick Sort

Quick Sort is a widely used sorting algorithm based on the divide-and-conquer paradigm. 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. This process leads to an overall time complexity of
O(nlog⁡n)O(n \log n) on average.

Description of Quick Sort


1. Pivot Selection: A pivot element is chosen. This can be the first, last, middle, or any
random element of the array.
2. Partitioning: The array is rearranged such that all elements less than the pivot are
placed on its left, and all elements greater than the pivot are placed on its right.
3. Recursion: The algorithm is applied recursively to the left and right sub-arrays until
the base condition (a single element or empty array) is reached.

Advantages:

● Efficient for large datasets.


● In-place sorting (requires minimal additional memory).

Disadvantages:

● Worst-case time complexity is O(n2)O(n^2) if the pivot selection is poor.

Steps of Quick Sort


1. Choose a pivot element from the array.
2. Partition the array such that elements smaller than the pivot go to the left and larger
elements go to the right.
3. Recursively apply the above steps to the left and right sub-arrays.
4. Combine the results to get the sorted array.

ALGORITHM

1. Algorithm QuickSort(A, low, high)


2. // A and output is an array of size n
3. {
4. if(low < high) then
5. { pivot_index := Partition(A, low, high)
6. QuickSort(A, low, pivot_index - 1) // Sort left sub-array
7. QuickSort(A, pivot_index + 1, high) // Sort right sub-array
8. }
9. }
10. Partition(A, low, high)
11. {
12. pivot := A[high] // Select pivot (last element in this case)
13. i:=low - 1 // Pointer for smaller element
14. for j := low to high - 1 dO
15. {
16. if( A[j] ≤ pivot) then
17. {
18. i := i + 1
19. swap(A[i], A[j]) // Swap elements
20. }
21. swap(A[i + 1], A[high]) // Place pivot in correct position
22. }
23.
24. returni + 1 // Return pivot index
25. }
26. swap(A[x], A[y])
27. {
28. temp:=A[x]
29. A[x] :=A[y]
30. A[y] := temp
31. }

Example with Demonstration


Initial Array:

29,10,14,37,13,88,23,5,67,129, 10, 14, 37, 13, 88, 23, 5, 67, 1

Step-by-Step Execution

1. Choose Pivot: Select 1 (last element).

○ Partition: Move all elements smaller than 1 to its left.


○ Result: 1,10,14,37,13,88,23,5,67,291, 10, 14, 37, 13, 88, 23, 5, 67, 29.
○ Pivot index = 0.
2. Left Sub-array: No elements (base case).
Right Sub-array: 10,14,37,13,88,23,5,67,2910, 14, 37, 13, 88, 23, 5, 67, 29.
3. Repeat recursively for the right sub-array:

○ Choose 29 as pivot.
○ Partition: 10,14,13,23,5,29,37,88,6710, 14, 13, 23, 5, 29, 37, 88, 67.
○ Pivot index = 5.
4. Apply quick sort on:

○ Left Sub-array: 10,14,13,23,510, 14, 13, 23, 5.


○ Right Sub-array: 37,88,6737, 88, 67.
5. Continue until all sub-arrays are sorted.

Final Sorted Array:

1,5,10,13,14,23,29,37,67,881, 5, 10, 13, 14, 23, 29, 37, 67, 88.

USAGE

Quick Sort is a highly efficient sorting algorithm with various use cases due to its speed and
flexibility. Here are the primary use cases:

1. Sorting Large Datasets


Quick Sort is particularly effective for sorting large datasets due to its average-case time
complexity of O(nlog⁡n)O(n \log n).

● Examples:
○ Sorting transaction records in databases.
○ Organizing large log files.

2. Applications in Divide-and-Conquer Algorithms


Quick Sort's divide-and-conquer approach makes it a foundation for other algorithms and
computational problems.

● Examples:
○ Merging and processing sorted arrays.
○ Used in hybrid sorting techniques (e.g., introsort).

3. Memory-Efficient Sorting
Quick Sort is an in-place sorting algorithm, meaning it requires very little additional
memory compared to merge sort.

● Examples:
○ Embedded systems where memory is constrained.
○ Sorting large datasets that cannot fit entirely in memory.

4. Sorting in Competitive Programming


Quick Sort’s performance and simplicity make it popular in coding competitions and
interviews.

● Examples:
○ Sorting problems with time constraints.
○ Problems requiring a custom partition logic.

5. Real-Time Applications
Quick Sort can be optimized for real-time systems with tight performance constraints.

● Examples:
○ Sorting packets in network routers.
○ Scheduling tasks in operating systems.

6. General Use in Libraries


Quick Sort is widely used in standard libraries of programming languages due to its
performance on average inputs.

● Examples:
○ Java’s Arrays.sort() (dual-pivot Quick Sort for primitives).
○ Python’s sorted() (uses Timsort, which incorporates Quick Sort principles).

7. Variants and Extensions


Quick Sort variants (e.g., 3-way Quick Sort) are used for specialized tasks like sorting
datasets with duplicate keys.

● Examples:
○ Optimizing sorting for datasets with frequent repeated values.

Let me know if you'd like more examples or a deeper dive into one of these use cases!

You might also like