Skip to document
This is a Premium Document. Some documents on Studocu are Premium. Upgrade to Premium to unlock it.

Quick Sort Algorithm

NOTES ON QUICK SORT ALGORITHM IN DATA STRUCTURES WITH CLEAR STEPS,POIN...
Course

Btech (kcs-701)

478 Documents
Students shared 478 documents in this course
Academic year: 2021/2022
Uploaded by:
Anonymous Student
This document has been uploaded by a student, just like you, who decided to remain anonymous.
Rajasthan Technical University

Comments

Please sign in or register to post comments.

Preview text

Quick Sort Algorithm

In this article, we will discuss the Quicksort Algorithm. The working procedure of Quicksort is also simple. This article will be very helpful and interesting to students as they might face quicksort as a question in their examinations. So, it is important to discuss the topic.

Sorting is a way of arranging items in a systematic manner. Quicksort is the widely used sorting algorithm that makes n log n comparisons in average case for sorting an array of n elements. It is a faster and highly efficient sorting algorithm. This algorithm follows the divide and conquer approach. Divide and conquer is a technique of breaking down the algorithms into subproblems, then solving the subproblems, and combining the results back together to solve the original problem.

Divide: In Divide, first pick a pivot element. After that, partition or rearrange the array into two sub-arrays such that each element in the left sub-array is less than or equal to the pivot element and each element in the right sub-array is larger than the pivot element.

Conquer: Recursively, sort two subarrays with Quicksort.

32 691 C++ vs Java

Combine: Combine the already sorted array.

Quicksort picks an element as pivot, and then it partitions the given array around the picked pivot element. In quick sort, a large array is divided into two arrays in which one holds values that are smaller than the specified value (Pivot), and another array holds the values that are greater than the pivot.

After that, left and right sub-arrays are also partitioned using the same approach. It will continue until the single element remains in the sub- array.

Choosing the pivot

Picking a good pivot is necessary for the fast implementation of quicksort. However, it is typical to determine a good pivot. Some of the ways of choosing a pivot are as follows -

o Pivot can be random, i. select the random pivot from the given array. o Pivot can either be the rightmost element of the leftmost element of the given array. o Select median as the pivot element.

Algorithm

Algorithm:

  1. QUICKSORT (array A, start, end)

  2. {

  3. 1 if (start < end)

  4. 2 {

  5. 3 p = partition(A, start, end)

  6. 4 QUICKSORT (A, start, p - 1 )

  7. 5 QUICKSORT (A, p + 1 , end)

  8. 6 }

  9. }

Partition Algorithm:

The partition algorithm rearranges the sub-arrays in a place.

Now, a[pivot] < a[right], so algorithm moves forward one position towards left, i. -

Now, a[left] = 24, a[right] = 19, and a[pivot] = 24.

Because, a[pivot] > a[right], so, algorithm will swap a[pivot] with a[right], and pivot moves to right, as -

Now, a[left] = 19, a[right] = 24, and a[pivot] = 24. Since, pivot is at right, so algorithm starts from left and moves to right.

As a[pivot] > a[left], so algorithm moves one position to right as -

Now, a[left] = 9, a[right] = 24, and a[pivot] = 24. As a[pivot] > a[left], so algorithm moves one position to right as -

Now, a[left] = 29, a[right] = 24, and a[pivot] = 24. As a[pivot] < a[left], so, swap a[pivot] and a[left], now pivot is at left, i. -

Since, pivot is at left, so algorithm starts from right, and move to left. Now, a[left] = 24, a[right] = 29, and a[pivot] = 24. As a[pivot] < a[right], so algorithm moves one position to left, as -

Quicksort complexity

Now, let's see the time complexity of quicksort in best case, average case, and in worst case. We will also see the space complexity of quicksort.

1. Time Complexity

Case Time Complexity

Best Case O(n*logn)

Average Case O(n*logn)

Worst Case O(n 2 )

o Best Case Complexity - In Quicksort, the best-case occurs when the pivot element is the middle element or near to the middle element. The best-case time complexity of quicksort is O(n*logn). o Average Case Complexity - It occurs when the array elements are in jumbled order that is not properly ascending and not properly descending. The average case time complexity of quicksort is O(n*logn). o Worst Case Complexity - In quick sort, worst case occurs when the pivot element is either greatest or smallest element. Suppose, if the pivot element is always the last element of the array, the worst case would occur when the given array is sorted already in ascending or descending order. The worst-case time complexity of quicksort is O(n 2 ).

Though the worst-case complexity of quicksort is more than other sorting algorithms such as Merge sort and Heap sort , still it is faster in practice. Worst case in quick sort rarely occurs because by changing the choice of pivot, it can be implemented in different ways. Worst case in quicksort can be avoided by choosing the right pivot element.

2. Space Complexity

Space Complexity O(n*logn)

Stable NO

o The space complexity of quicksort is O(n*logn).

Was this document helpful?
This is a Premium Document. Some documents on Studocu are Premium. Upgrade to Premium to unlock it.

Quick Sort Algorithm

Course: Btech (kcs-701)

478 Documents
Students shared 478 documents in this course
Was this document helpful?

This is a preview

Do you want full access? Go Premium and unlock all 8 pages
  • Access to all documents

  • Get Unlimited Downloads

  • Improve your grades

Upload

Share your documents to unlock

Already Premium?
Quick Sort Algorithm
In this article, we will discuss the Quicksort Algorithm. The working
procedure of Quicksort is also simple. This article will be very helpful and
interesting to students as they might face quicksort as a question in their
examinations. So, it is important to discuss the topic.
Sorting is a way of arranging items in a systematic manner. Quicksort is
the widely used sorting algorithm that makes n log n comparisons in
average case for sorting an array of n elements. It is a faster and highly
efficient sorting algorithm. This algorithm follows the divide and conquer
approach. Divide and conquer is a technique of breaking down the
algorithms into subproblems, then solving the subproblems, and
combining the results back together to solve the original problem.
Divide: In Divide, first pick a pivot element. After that, partition or
rearrange the array into two sub-arrays such that each element in the left
sub-array is less than or equal to the pivot element and each element in
the right sub-array is larger than the pivot element.
Conquer: Recursively, sort two subarrays with Quicksort.
32.1M
691
C++ vs Java
Combine: Combine the already sorted array.
Quicksort picks an element as pivot, and then it partitions the given array
around the picked pivot element. In quick sort, a large array is divided into
two arrays in which one holds values that are smaller than the specified
value (Pivot), and another array holds the values that are greater than the
pivot.
After that, left and right sub-arrays are also partitioned using the same
approach. It will continue until the single element remains in the sub-
array.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.

Why is this page out of focus?

This is a Premium document. Become Premium to read the whole document.