L-3.1: How Quick Sort Works | Performance of Quick Sort with Example | Divide and Conquer

Gate Smashers
20 Jan 202013:26

Summary

TLDRIn this video from Gate Smashers, the instructor explains the quicksort algorithm, a key divide-and-conquer technique useful for competitive exams, university exams, and placements. The video covers how quicksort partitions arrays using pivot elements, how pointers are used to rearrange elements, and the importance of understanding time complexity in various cases. Key concepts such as recurrence relations and best, average, and worst-case time complexities are discussed. The video concludes by explaining the average case time complexity of quicksort as O(N log N).

Takeaways

  • πŸ˜€ Quicksort is a divide and conquer algorithm used for sorting arrays.
  • πŸ” The algorithm works by selecting a 'pivot' element and partitioning the array around this pivot.
  • πŸ“š It's important for competitive exams, college, university level exams, and placements.
  • πŸ› οΈ Quicksort is efficient and has an average time complexity of O(n log n), making it suitable for large datasets.
  • πŸ”„ The process involves two pointers, P and Q, which move through the array to partition elements relative to the pivot.
  • πŸ“‰ The worst-case time complexity of quicksort is O(n^2), which is discussed in a subsequent video.
  • πŸ”‘ The choice of pivot is crucial as it affects the efficiency of the sorting process.
  • πŸ”„ The script demonstrates the step-by-step process of how quicksort partitions an array and sorts it.
  • πŸ“ˆ The recurrence relation for quicksort in the average case is T(n) = 2T(n/2) + n.
  • 🎯 The final sorted array is achieved by recursively applying quicksort to sub-arrays divided by the pivot.

Q & A

  • What is quicksort and why is it important for competitive exams and interviews?

    -Quicksort is a highly efficient sorting algorithm that employs the divide and conquer strategy. It is crucial for competitive exams and interviews because it is widely used in computer science and software engineering, and understanding its principles can help in solving a variety of algorithmic problems.

  • Can you explain the divide and conquer approach in the context of quicksort?

    -The divide and conquer approach in quicksort involves breaking down a large problem into smaller sub-problems, solving each of those sub-problems, and then combining their solutions to solve the original problem. In quicksort, this is done by partitioning the array and then recursively sorting the partitions.

  • What is the role of the pivot element in quicksort?

    -The pivot element in quicksort is used as a reference point to partition the array into two parts: elements less than the pivot and elements greater than the pivot. The pivot's final position after the partitioning process is crucial for the efficiency of the algorithm.

  • How do the two pointers, P and Q, function in the quicksort algorithm?

    -Pointer P starts from the right side of the array and moves leftward, stopping when it finds an element greater than the pivot. Pointer Q starts from the left and moves rightward, stopping when it finds an element less than the pivot. They help in swapping elements to place the pivot in the correct position and to sort the elements around it.

  • Why is it necessary to use plus infinity and minus infinity in the quicksort algorithm?

    -Plus infinity is used to ensure that the rightmost element is compared with the pivot and stops the right pointer P when it reaches the end of the array. Minus infinity is not typically used because the left pointer Q checks for elements less than or equal to the pivot, and it naturally stops at the pivot element without needing minus infinity.

  • What happens when pointers P and Q cross each other during the quicksort process?

    -When pointers P and Q cross each other, it means that the partitioning step is complete, and the pivot element should be in its correct sorted position. At this point, the pivot element is swapped with the element pointed to by Q.

  • How does the quicksort algorithm handle the case when the pivot element is at the beginning or end of the array?

    -If the pivot element is at the beginning or end of the array, the pointers P and Q will eventually cross each other, indicating that all elements have been compared against the pivot. In this case, the pivot is swapped with the element pointed to by Q, ensuring that the pivot is in its correct position.

  • What is the recurrence relation for the average case of quicksort?

    -The recurrence relation for the average case of quicksort is T(N) = 2T(N/2) + N, which accounts for the recursive sorting of two halves of the array and the linear time complexity for the partitioning step.

  • What is the time complexity of quicksort in the average case?

    -The average case time complexity of quicksort is O(N log N), which is derived from solving the recurrence relation T(N) = 2T(N/2) + N using methods like the master method or substitution method.

  • What are the key points that might be asked in an interview about quicksort?

    -In an interview, one might be asked about the concept of quicksort, how it works, its time complexity in best, average, and worst cases, the role of the pivot, and the recurrence relation. Understanding these aspects is essential for explaining the algorithm and analyzing its performance.

Outlines

00:00

πŸ“š Introduction to Quicksort

The paragraph introduces the Quicksort algorithm, emphasizing its importance for competitive exams, college, university, and placements. It explains Quicksort as a divide and conquer method, where a large problem is divided into smaller sub-problems, solved individually, and then combined for a final solution. The concept of partitioning in Quicksort is highlighted, where an array is divided using a pivot element. The process involves two pointers, P and Q, moving in opposite directions to arrange elements relative to the pivot. The explanation includes a detailed walkthrough of how elements are compared to the pivot and how the pointers move and stop based on certain conditions. The paragraph concludes with a discussion on the types of questions that might be asked about Quicksort, such as its concept, time complexity in different cases, and recurrence relations.

05:05

πŸ” Deep Dive into Quicksort Mechanics

This paragraph delves deeper into the mechanics of Quicksort, illustrating how the pointers P and Q move and swap elements relative to the pivot. It explains the process of partitioning with an example, showing how elements are rearranged so that all elements less than the pivot are on one side and all elements greater are on the other. The paragraph clarifies the conditions under which P and Q stop moving and when they swap elements, including the special cases of encountering 'plus infinity'. It also addresses the scenario where P and Q meet or cross each other, explaining the swapping mechanism with the pivot element. The summary ends with a clear explanation of how Quicksort reduces the problem size after each pass, effectively sorting the array.

10:07

πŸ“‰ Analyzing Quicksort's Time Complexity

The final paragraph discusses the time complexity of Quicksort, focusing on the average or best case. It describes how the pivot element's position after the first pass affects the division of the problem into sub-problems. The explanation includes a recurrence relation that models the algorithm's performance and leads to the conclusion that the average case time complexity is O(N log N). The paragraph also mentions that the discussion of the worst-case scenario will be covered in a subsequent video, hinting at further analysis to come.

Mindmap

Keywords

πŸ’‘Quicksort

Quicksort is an efficient sorting algorithm that operates on the principle of divide and conquer. 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 continues until the base case of an array with zero or one element is reached. In the video, quicksort is the main theme, and its mechanism is explained in detail, including how it partitions the array and the role of the pivot element.

πŸ’‘Divide and Conquer

Divide and conquer is a problem-solving strategy that involves breaking down a large problem into smaller, more manageable sub-problems, solving each of those sub-problems, and then combining their solutions to solve the original problem. In the context of the video, quicksort exemplifies this strategy by dividing the array into sub-arrays around the pivot element and conquering the problem by sorting these sub-arrays.

πŸ’‘Pivot Element

The pivot element is a critical concept in quicksort, as it serves as the reference point around which the array is partitioned. In the video, the pivot is chosen from the array, and elements are rearranged such that those less than the pivot are placed before it, and those greater are placed after it. The script illustrates this with examples, showing how the pivot's position changes as the array is sorted.

πŸ’‘Partitioning

Partitioning is the process of rearranging the elements in the array based on the pivot element. Elements less than the pivot are moved to one side, and elements greater than the pivot are moved to the other side. The video script describes this process, emphasizing how partitioning is essential for the quicksort algorithm to work effectively.

πŸ’‘Time Complexity

Time complexity is a measure of the amount of time an algorithm takes to run as a function of the length of the input. The video discusses the time complexity of quicksort, explaining that in the average case, it is O(N log N), which is considered efficient for sorting algorithms. The script also mentions that the worst-case time complexity will be discussed in a subsequent video.

πŸ’‘Recurrence Relation

A recurrence relation is an equation that defines a sequence of values in terms of previous values in the sequence. In the context of the video, the recurrence relation for quicksort is given as T(N) = 2T(N/2) + N, which represents the time taken to sort an array of size N. The script explains how this relation is derived and how it is used to calculate the time complexity of the algorithm.

πŸ’‘Best Case

The best case refers to the most optimal scenario for an algorithm's performance. In the video, the best case for quicksort is when the pivot element is always chosen such that it divides the array into two equal halves, leading to the most efficient sorting. The script mentions that this results in a time complexity of O(N log N).

πŸ’‘Worst Case

The worst case is the least optimal scenario for an algorithm's performance. Although not fully discussed in the script, it is mentioned that the worst-case time complexity of quicksort will be covered in a future video. Typically, this occurs when the pivot element is either the smallest or largest element in the array, leading to an inefficient partitioning.

πŸ’‘Pointers

In the context of the video, pointers are used to traverse the array during the partitioning process. The script introduces two pointers, P and Q, which move through the array to compare elements with the pivot and to swap elements if necessary. The pointers play a crucial role in the quicksort algorithm's implementation.

πŸ’‘Plus Infinity

Plus infinity is a concept used in the video to illustrate how the partitioning process handles the end of the array. When the pointer P reaches the end of the array, it is compared to plus infinity to ensure it stops moving. This is a practical way to handle array boundaries and is used in the script to explain the mechanics of the partitioning process.

Highlights

Quicksort is a divide and conquer algorithm.

It's essential for competitive exams, college/university exams, and placements.

Quicksort partitions the problem into sub-problems, then combines their solutions.

The pivot element is crucial in the quicksort algorithm.

Two pointers, P and Q, are used to partition the array around the pivot.

P stops when it finds an element greater than the pivot; Q stops when it finds an element less.

The process ensures that all elements greater than the pivot are on the right and vice versa.

The pivot's final position after the first pass is critical to the algorithm's efficiency.

A well-chosen pivot can divide the problem into two nearly equal parts.

The average case time complexity of quicksort is N log N.

The recurrence relation for quicksort in the average case is T(N) = 2T(N/2) + N.

The video explains the concept of plus infinity to ensure the algorithm stops correctly.

The video demonstrates how to handle cases where P and Q cross each other during partitioning.

Quicksort's efficiency depends on the pivot's position after the first pass.

The video provides a step-by-step guide to understanding and implementing quicksort.

The video will cover the worst-case time complexity of quicksort in the next installment.

Transcripts

play00:00

Hello friends, welcome to Gate Smashers

play00:03

In this video we are going to discuss quicksort

play00:05

And in this video we are going to discuss all important points related to quicksort

play00:09

Which are very important for your competitive exams or college or university level exams

play00:14

And even for your placements

play00:16

So guys, like the video quickly, subscribe the channel if you haven't done it yet

play00:22

And please press the bell button so that you get all the latest notifications

play00:26

So let's start with quicksort

play00:28

The first important point is it is a divide and conquer technology

play00:32

Or divide and conquer method

play00:34

Now what is divide and conquer?

play00:36

Let's say I have a problem and the size of the problem is N

play00:40

So what do we do in divide and conquer?

play00:42

We divide the big problem into sub problems

play00:45

Like if this is a big problem, let's say I am dividing it into N/2 and N/2 in two parts

play00:49

Then N by 4, N by 4, this also N by 4

play00:52

Means we divide the big problem into small parts or sub problems

play00:57

Then we solve all those problems

play00:59

And finally we conquer their solution

play01:03

Conquer means we combine and give a final answer

play01:07

So quicksort is also partition based

play01:10

Or you can say it also divides the problem

play01:13

But how it divides, how it partitions

play01:17

This is the most important

play01:19

And if you have any sorting algorithm

play01:23

If you talk about any sorting algorithm or searching algorithm

play01:25

Then we don't have only how it works

play01:28

Or how its answer will come

play01:30

Let's say if I give you an unsorted array

play01:33

You have to put quicksort

play01:35

They will not ask you but it will be the final output

play01:38

What will be the final output?

play01:39

You sort it and write it

play01:41

That is your final answer

play01:43

But they will not ask you, then anyone will tell you

play01:45

But what can you be asked here?

play01:47

First of all concept, how quicksort works

play01:50

Second, time complexity

play01:53

In best case, average case, worst case

play01:56

How it works, related to this

play01:58

And third, recurrence relation

play02:01

Because you know how to write recurrence relation

play02:03

Only then you can find out time complexity

play02:06

Because we have already discussed substitution method or master method

play02:11

So let's start here first

play02:13

How it works

play02:14

We have taken a normal unsorted array

play02:18

So see how it works

play02:20

First element, what we give it

play02:23

We give it the name of the pivot element

play02:25

Means what is this?

play02:26

It is a pivot element

play02:28

Now we have reserved this pivot element

play02:31

And we have taken two pointers

play02:33

One is pointer P and one is pointer Q

play02:38

Now the P pointer moves RHS

play02:42

Right hand side

play02:43

And when does it stop?

play02:45

Means it will increment one by one

play02:47

But when will it stop?

play02:49

When it will get element greater than pivot

play02:53

You keep writing all these points

play02:55

When will it stop?

play02:55

It will move one by one like this

play02:57

Right hand side

play02:58

But when will it stop?

play03:00

When it will get element greater than the pivot element

play03:02

So it will keep moving like this

play03:04

Like this Q

play03:05

Q is getting decrement

play03:08

Means it is moving towards LHS

play03:10

And when will it stop?

play03:11

It will get element lesser than the pivot element

play03:16

Now why does it do this?

play03:17

Now what does P actually do?

play03:19

That all the big elements come on the right side of the pivot element

play03:23

And all the small ones come on the left side

play03:26

So why will Q help to bring small ones?

play03:28

And P will help to bring all the big elements on the right side

play03:32

You will understand further what it is actually doing

play03:35

Now see P will move one by one

play03:37

But remember here

play03:39

In the last we have to put plus infinite here

play03:42

It asks many times why do we put plus infinite

play03:45

See it is moving forward

play03:47

Let's say if your array is like this

play03:49

35

play03:50

5

play03:51

4

play03:52

3

play03:52

2

play03:53

1

play03:54

Like this

play03:54

For example now see 35

play03:56

This is my pivot

play03:57

5

play03:58

5 is greater than?

play03:59

No

play04:00

Next move

play04:00

When will it stop?

play04:01

When it will get greater

play04:02

4 is greater than?

play04:03

No

play04:04

Next move

play04:05

3, 2, 1, no!

play04:08

When will it stop?

play04:09

It has to stop too

play04:11

So to stop it we have written plus infinite here

play04:14

Because plus infinite will obviously be greater than

play04:17

Pivot element

play04:18

Means it will stop here

play04:20

It will give garbage value

play04:21

So we have written it to stop here

play04:24

Now many times the question arises that sir

play04:25

If we are increasing Q to the left side

play04:28

Then Q will also have to stop

play04:30

So should I write minus infinity here?

play04:33

No

play04:33

When Q will decrement here

play04:37

So what it is checking in actual

play04:39

It is checking less than equal to

play04:42

Means less than equal to than pivot element

play04:44

This is checking greater than equal to

play04:47

Now see greater than equal to

play04:48

Obviously pivot element is in its left

play04:50

And this is moving forward

play04:52

So the pivot will never come

play04:54

But Q is coming towards a pivot

play04:56

Now if Q is coming towards a pivot

play04:58

So see

play04:58

Less than equal to

play04:59

Less than equal to

play05:05

So it will be equal to pivot, so it will not move next to this.

play05:09

So at least it will automatically stop at the pivot.

play05:12

So this is the actual concept of how we move P and Q.

play05:17

Now let's start with the concept. Let's say we are starting first with p.

play05:21

P, 50 greater than 35, yes, stop, don't move ahead.

play05:27

Next come to Q, Q 45, 45 less than 35, no, so move Q ahead.

play05:33

90, 90 less than 35, no, so move ahead.

play05:38

20, 20 less than 35, yes, stop again.

play05:42

Q is stopped here, P is already stopped here.

play05:45

Now you have to check that P and Q have interchanged each other.

play05:50

Means P and Q have not crossed each other.

play05:52

No, P and Q have not crossed each other yet.

play05:55

So what you have to do is simply swap these two elements.

play05:59

Swap whatever element is there in their position.

play06:02

Means your output will come like this now.

play06:05

20 has come here, 15 as it is, 25 as it is, 80 as it is.

play06:12

What has come instead of 20? 50, 90, 45 plus infinite.

play06:17

It will come like this now.

play06:19

So now see P was here, Q was in this position.

play06:24

Now increase again, next P.

play06:26

P greater than means 15 greater than 35, no.

play06:30

25, 25 greater than 35, no.

play06:34

80, 80 greater than 35, yes, stop P.

play06:39

P has been stopped here.

play06:40

But you have to move Q along with it.

play06:42

But I am moving one by one, you have to move Q equally.

play06:45

Q, 50 less than 35, no, next.

play06:48

80, 80 less than 35, no, next.

play06:51

25, 25 less than 35, yes.

play06:54

So Q has stopped here, P is here.

play06:57

Now you have to check whether P and Q crossed each other.

play07:02

Here P was here, here Q was there, it was not done here.

play07:05

But now see P and Q have crossed each other.

play07:08

Q has come here, P has come there.

play07:10

Whenever this type of situation comes,

play07:12

that either P and Q are in the same position or Q crosses.

play07:17

Now what you have to do in this case,

play07:19

you simply have to replace the pivot element with Q element.

play07:25

Remember, whoever is pointing out Q,

play07:28

you swap with that element, meaning 25 has come here,

play07:34

20 here, 15 here, 35 here.

play07:39

Where is Q pointing out? 25.

play07:41

So swap 25 here, swap with the pivot element,

play07:44

do not swap with P, swap only when they did not cross or did not come equally.

play07:50

But if they came equally or crossed, then you have to do it with the pivot.

play07:54

Rest of the elements same, 80, 50, 90, 40, 5.

play08:00

Clear a point? So this is actually a concept here.

play08:03

Now see, 35 is your, this is called one pass.

play08:07

Means it was the first pass, first step.

play08:10

Now see, as soon as the first step is complete,

play08:12

your pivot element has reached the right position.

play08:16

See, 35 is the right position, all the small ones are here,

play08:20

all the big ones are here.

play08:23

There is no sort now, there is one step.

play08:25

But after this step, 35 has reached its right position.

play08:29

And the smaller ones are here, the bigger ones are here.

play08:33

Means you have divided this problem into two parts.

play08:36

25, 20, 15 came here, 80, 50, 90, 45 came here.

play08:44

And your 35 pivot is on your right position.

play08:48

Now your problem will be divided into two parts.

play08:51

Now apply quick sort here too.

play08:54

Now if I apply quick sort here, what will happen?

play08:56

25 is the pivot element.

play08:58

So 25 is my pivot element.

play09:01

P is yours, Q is yours.

play09:05

Simple, as we did earlier.

play09:07

Now see, P greater than 25, no, move ahead.

play09:11

15 greater than 25, no, move ahead.

play09:15

What is ahead? Plus infinite.

play09:16

So what stopped at plus infinite? P stopped.

play09:19

It moved from here, P stopped here.

play09:22

Check Q too.

play09:23

15 less than 25.

play09:25

Yes, stay here.

play09:27

Because Q element is less, so it will stop.

play09:30

Now check if P and Q have crossed each other.

play09:34

They have crossed.

play09:35

When they have crossed each other, what I told you is

play09:38

exchange the pivot element with Q element.

play09:42

So see, 15 came ahead, 20 as it is, 25 is here.

play09:47

And the pivot here, as it is, 35, don't do anything, as it is.

play09:52

So see, this sub-area has been sorted.

play09:54

Now we have to do the same here.

play09:56

80 was your pivot.

play09:59

P is yours, Q is yours, and plus infinity is here.

play10:03

This is actually divided into two parts, so this is separate.

play10:06

This is separate.

play10:07

So same here.

play10:08

P, 50 greater than 80, no.

play10:11

Move next.

play10:12

90 greater than 80, yes.

play10:15

So P will point to 90.

play10:18

Same like this.

play10:19

45 less than 80, yes.

play10:21

Q will stay here.

play10:23

So did P and Q cross each other?

play10:25

No.

play10:26

So in this case, we have to swap these two.

play10:29

It has nothing to do with the pivot.

play10:31

Means the swap of these two, I write 80, 50, 45, 90 here.

play10:38

That's it.

play10:39

Now P was here, Q was here, and plus infinity as it is.

play10:44

Now look at you, next 90.

play10:46

90 greater than 80, yes.

play10:49

So your P stopped at this point.

play10:52

Check Q.

play10:53

Q, 90 less than, no.

play10:55

Next will be Q.

play10:56

45 less than 80.

play10:58

45 less than 80, yes.

play11:00

So Q stopped here.

play11:02

Q is checking less, that is greater.

play11:03

So see, both positions have crossed each other.

play11:07

P and Q have crossed each other.

play11:09

If they have crossed, then what you have to do is

play11:12

write the pivot element instead of Q.

play11:15

So that means, Q is 45, it came instead of the pivot.

play11:18

50 as it is.

play11:20

And on the Q position, pivot.

play11:22

Which was the pivot? 80.

play11:23

80 came here, 90 as it is.

play11:26

So see, your overall array has been sorted.

play11:32

So in this way, quick sort works.

play11:35

This is a normal case.

play11:37

Means, call it average case or best case.

play11:39

We haven't talked about worst case here.

play11:42

So in normal case, if your problem is size N,

play11:46

then your pivot element,

play11:48

actually the whole story depends on the pivot element.

play11:51

Where will the pivot element sit after the first pass?

play11:56

If your pivot element is in the middle,

play11:58

means let's say this is my N size.

play12:00

And if this was your pivot, after the first pass,

play12:03

if the pivot reached the middle,

play12:05

then your problem will be divided.

play12:07

N by 2, N by 2 minus 1.

play12:10

Why did you do minus 1? Because the pivot element is here.

play12:13

Means, on an average you can say, it will be divided into two parts.

play12:16

Now N by 4, N by 4, it is dividing like this.

play12:21

So what can you say that my problem,

play12:24

if my problem is T of N,

play12:26

then it is dividing into N by 2 plus N by 2.

play12:29

It is dividing into two parts plus N time is here.

play12:34

What is N time?

play12:35

In every step, when you completed the first pass,

play12:38

when you completed this step, then you had to scan the whole array.

play12:43

Then your one pass is completed.

play12:46

You scanned the whole array, then your one pass is completed.

play12:49

So this N time is for the scanning entire array.

play12:53

Because how much is the size of the array? Maximum N.

play12:55

So what is your actual recurrence relation?

play12:58

2T N by 2 plus N.

play13:02

This is the final recurrence relation in the average case.

play13:05

We are talking about the normal case.

play13:07

So if you solve this, we have already done it.

play13:09

Master method also or even substitution method also,

play13:12

if you solve it, then the answer will be N log N.

play13:15

So that's why the average case time complexity of quick sort is N log N.

play13:20

So in the next video we will talk about worst case time complexity.

play13:24

Thank You.

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
QuicksortAlgorithmsDivide & ConquerSortingTime ComplexityRecurrence RelationCompetitive ExamsProgrammingData StructuresEducational Content