Hello Guys and welcome to Python programming tutorials by Amuls Academy.
This tutorial is the continuation of the previous tutorial, in that we discussed about the Quicksort
algorithm and how to sort the number in the ascending order by taking the last element
as the pivot element and in this tutorial we will see how to select the first element
as the pivot element and how to take the random element, as well as how to take the median
of three values as the pivot element, as well as we will see how to arrange the number in
the descending order ok.
so this is the previous example and in that we took the last element as the pivot element
and we arrange the number in the ascending order so today we will take another example.
In that we will take the first element as pivot element and we will arrange number in
the ascending order first ok, ok so this is the example here we will take
the first element as the pivot ok so here this is the pivot value, so this will be the
right mark so the variable "right" which will point towards the last value of the list
and here this will be the "left" variable ok.
In the previous example when we took the last value as the pivot element then we took "left"
as the first element of the list and "right" as the last -1.
But here we took pivot as the first value so we need to take the next value as the "left"
value and the last value as "right" value ok so now we need to do the same thing that
is, we need to compare the values with the pivot element.
First we will start from the "left" ok so first i need to check whether "left index
is less than or equal to right", yes left is 1, right is 5, so left is the smaller,
so i will check the "value present in the left whether it is less than or equal to pivot
element". so first i will check "whether 26 is less
than or equal to 54", YES it is true, if it is true then i need to move towards the
next value right?, so now, so left will point towards 93, the value of left index is 2 ok.
So now again i will check whether "left is less than or equal to right", so left
value is 2, right value is 5, so left is less than right.
so i can check the value now, so I'll check "whether 93 is less than or equal to 54"
NO it is false, right?, is not true so i will stop here, now i will go to the right side
and I'll check the values.
so first thing i need to do is, i need to check whether "left is less than or equal
to right" ok yes it is, right value is greater.
so i will check the value present in the "right" that is, "31 is greater than or equal to
pivot value that is 54", ok in the right side we want the value greater than the pivot,
so I'll check, so this will be the right side.
OK i will check "31 is greater than or equal to 54 that is the pivot value", no it is
not ok the condition become false so I'll stop here.
so in the left side we got a value which is greater than pivot and in the right side we
got the value which is less than Pivot, so we need to swap those numbers, so we need
to swap 93 and 31 ok, so here 31 come here, 93 will go here ok.
so now again i will check from the left side, i will check first whether "left value is
less than or equal to right" this condition so yes left value is 2, right value is 5.
So left is smaller so i can check the next condition, that is the value present in the
left index is less than or equal to 54, so i need to check "31 is less than or equal
to 54" true right?, so i need to move to the next value so now left is 17.
so now again i will check whether "left is less than or equal to right", yes left
value is 3, right value is 5.
So left is smaller so then i will check, "the value present in the left that is 17 is less
than or equal to 54", yes again it is true so I'll move towards the next value.
so now left value is this ok left value become 4, the value present in that is 77.
so now again i will check, "whether left is less than or equal to right" yes left
value is 4, right value is 5, so left is smaller than right.
so i can check the value so "77 is less than or equal to 54" no it is not condition
become false.
so i need to stop here ok. now i will move towards the right side, so
I'll check the value present in the right, so before that i need to check "whether
left is less than or equal to right", this condition, YES it is true so i will the value
present in the right that is "93 is greater than or equal to 54" yes it is true.
so i need to move towards the next value, so right will point here now.
so now again I need to check, "whether the left is less than or equal to right", here
left is equal to right, because both are pointing towards index 4, so that's ok so i need to
check "the value present in the right, that is 77 is greater than the pivot value that
is 54".
Yes it is true right?, 77 is a greater value.
So i need to move towards the next value, so right will come here so right index become
3, so now to check the next condition i need to check this condition "whether left is
less than or equal right" no it is not, because left value is 4, but right value is
3, ok left is greater than right.
so if this condition occurs i need to stop everything right?, this means we got the correct
position of the pivot element, to place that in the correct position what we need to do,
we need to swap pivot value and the value present in the right Index.
why because previously as i said, if you take last element as the pivot ok, then you need
to swap the value present in the left index and pivot.
if you take first element as the pivot ok then you need to swap the value present in
the right index as well as pivot ok.
so i need to swap the value present in the right index as well as pivot that is nothing
but i need to swap 17 and 54 ok here we took value 54 as the pivot value.
so 54 is here now, we got the correct position of the 54 .
so now what is the next step, we need to divide the list based on the pivot element right?.
Value present in the left side of this pivot elements are smaller than pivot element.
The value present in the right side of the pivot element are greater than the pivot element.
so to divide this, we will get sublist here 17 26 31 here 77 93.
54 is sorted and we are dividing this as like this, ok so 54 is sorted.
Now again i need to do the same thing i need to take the first value as pivot ok and i
need to do the same steps and i need to place the correct position of the pivot value ok.
so here I'll take 17 as pivot, this as left, this as right, right?.
so I'll check whether "left is less than or equal to right", yes it is.
so I will check the value present in the, "26 is less than or equal to 17", no it
is not, so I'll stop.
I will go towards the right and I'll check whether "left is less than or equal to right"
i need to check this condition every time ok .
I need to check the value present in right index that is, "31 is greater than or equal
to 17" yes it is true right?, so i need to move towards the next value if the condition
become true i need to move towards the next value right?, so now left and right are pointing
towards the same index that is 1.
So left is equal to right that's ok this condition satisfies, so i need to check the value present
in the right ok so that is nothing but, this is for left and this is right right?, so value
present in the right index that is "26 is greater than or equal to 17", YES it is
true right?, because "26 is greater than 17".
so i need to move towards a next value, now right will point towards the pivot value that
is zero index, to check the next condition first i will check this "left is less than
or equal to right" here right is smaller than left.
left value is 1, right value 0 right?, so this condition fails.
so i need stop, now i need to swap the value present in the right and pivot because both
are same here, right is pointing towards zero index and pivot is also at 0 index.
That is nothing but i need to swap 17 with the 17, this is the correct position that
means this is present in the correct position ok so, 17 is in the correct position that
means so here 17 is the pivot value and it will divide the sublist as 26 and 31 ok there
are no sublist here because there is no element present in the left side of the pivot value.
ok so again we need to do the same thing, that is i need to take 26 as pivot element
and i need to find out its correct position and i need to divide the list.
Here we will get 26 as the pivot value right?, next and we will get 31 when we got the single
element we need to stop ok. so here, from here we will get 26 and 31 this
is 17 26 31 ok . so this are sorted that's why we got this
list .
And here we will do the same thing that is, we will take the first as the pivot element
and we will divide the list and when we get a single element in the list we will stop
ok.
So we will get the final output like this, ok so it is 17 31 54 77 93 ok the values are
in the ascending order .
so in this way, by taking the first Value as pivot element you can do that so there
is nothing difference in the previous example and in this example right?, we need to compare
the values with pivot element.
Pivot element can be last element or first element or we can choose it randomly also.
As i said these two method that is selecting the first element and last element as the
pivot is not good choice when the sorted list is given or reverse sorted list is given right?.
So in that time you can choose random element as pivot element or we can go for the median
of three values . Suppose if i take a list ok, so it contains
few elements.
In the program we will use random function ok.
We don't know which element we are selecting as the pivot.
for example we can take this as the pivot element ok.
So then i need to compare all these elements with the pivot element, to make it easier
to compare you can do one thing, if this is the pivot element it is present in the middle
of the list right?, So what i can do is, i can swap this value
with the last index or with the first index ok so something like this 8 3 if i swap it
to last index, then 1 4 17 ok.
Now 17 will be present at last.
So now you can solve this right?, this is similar to the taking last element as the
pivot element.
So choose a random element from the list as the pivot element then swap it to the last
position or the first position of the list then continue the process.
it is easier right?, there is no need of copying it to last position or first position but
sometimes we can see if i take this as left and this is right ok, in some cases left will
point towards the index where the pivot element is present right?, in that time we need to
compare 17 with the 17 . That is pivot element with the pivot element.
if you want to avoid this then you can do this, you can place that in the corner of
the list and you can compare all other values with that ok.
so if you compare pivot with pivot nothing is wrong, you can do that ok.
And for that median of three values what we can do is, median of, i can take first index
and that is the value present in the zeroth index and Middle index and the last.
first element last element and middle element ok so you can find out the median of this
three and you can take that element as the pivot element ok.
so in the program we will use the median function itself but if you want to find out manually
then you can do like this ok I'll show you how to find out the median now, so here what
you need to do is, you need to take the first value present in the zeroth index that is
10 ok so it is the first element, next i need to take the last element that is 1 last element.
Next i need to take middle element ok so here we can see there are even numbers so to find
out the middle element i will use like this ok.
so if i take this index as low, if it is as high ok so I'll take "low plus high truncated
division 2",ok this truncated division always will give the integer value so here "(0
+ 5) / 2", so it will give 2.
So this value will become middle valuel ok so 3, in this case .
so i need to find out the median right?, so to find out the median first I'll write down
the values that is 10 3 and 1 right?,the first element middle element and the last element
so now i need to sort this ok so in the ascending order, if i sort this 1 3 10 or descending
order.
so the middle element will be the median ok so here 3 will be the median ok, so I'll take
this as the pivot value and i can compare all the values with this pivot value and i
can sort the values . Ok so these are the four ways to select the
pivot element.
Next is, here in all example we arrange the number in ascending order if i want to arrange
number in the descending order then how to do that?, simple in that case you need to
change the symbol of this condition that's it.
Ok here we are checking "a of left" the value present in the left index should be
less than or equal to pivot, it is for ascending order but for the descending order you should
do is,"a of left" ok it should be greater than or equal to pivot because we want the
bigger value in the left side and here right side "a of right" should be less than
or equal to pivot.
Ok simple change like this ok.
10 4 1 17 23 5 ok so i took some random numbers, so here what I'll do is, I'll take the first
element as pivot element ok.
This will be left this will be right, i need to check whether the "left element is greater
than or equal to pivot", here we will check "4 is greater than or equal to 10", false
right?, so I'll stop here.
I will go to the right side here i want the element lesser than the pivot value.
so I'll check in the right side i will check "value 5 is less than or equal to 10"
yes it is true so i will move to the next value I'll take this as the right value ok.
I will check whether "23 is less than or equal to 10" no it is not, so i need to
swap 23 and 4 OK so this is the change Ok previously we wanted the smallest value in
the left side, bigger value in the right side but here opposite.
Here we want bigger value in left side and smaller value in the right side for that you
need to check this two condition ok this condition is same left always should be less than or
equal to right?.
Alright.
So in this way you can arrange the number in the descending order ok so that is about
the example guys.
So in the next tutorial we will see the algorithm as well as we will write the program for all
these condition.
so that's it for now guys thank you for watching don't forget to subscribe to my channel i
will meet you in next class till then take care.
Không có nhận xét nào:
Đăng nhận xét