Quick Sort [End Element as Pivot]

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 116:-  Quick Sort [End Element as Pivot]

QuickSort is a widely used sorting algorithm that follows the Divide and Conquer approach. One common variation of QuickSort involves choosing the last element of the array as the pivot element. This choice simplifies the partitioning process and is easy to implement. The basic idea is to partition the array into two subarrays - one containing elements smaller than the pivot and another containing elements greater than the pivot.

Here's how QuickSort with the last element as the pivot works:

pythonCopy code

def partition(arr, low, high): pivot = arr[high] i = low - 1 for j in range(low, high): if arr[j] <= pivot: i += 1 arr[i], arr[j] = arr[j], arr[i] arr[i + 1], arr[high] = arr[high], arr[i + 1] return i + 1 def quick_sort(arr, low, high): if low < high: pivot_index = partition(arr, low, high) quick_sort(arr, low, pivot_index - 1) quick_sort(arr, pivot_index + 1, high) # Example usage arr = [10, 7, 8, 9, 1, 5] n = len(arr) quick_sort(arr, 0, n - 1) print("Sorted array:", arr)

In this example:

The partition function selects the last element (arr[high]) as the pivot and arranges the array in such a way that all elements smaller than the pivot are on the left side, and all elements greater than the pivot are on the right side.

The quick_sort function recursively applies the partitioning and sorting steps to the left and right subarrays.

The time complexity of QuickSort with the last element as the pivot is generally O(n log n) on average, with the worst-case time complexity being O(n^2) when the pivot selection consistently results in unbalanced partitions. However, various techniques, such as choosing a random pivot or using a median-of-three pivot, can be employed to mitigate the worst-case scenarios and improve the average performance of the algorithm.

16. Divide and Conquer | Backtracking - Assignments

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?