Sorting Algorithms 20222 Notes
Sorting Algorithms 20222 Notes
Sorting Algorithms 20222 Notes
A = { a, b, c, d, e, f }
F = { 9 8 6 3 15 2 }
abcdef
26 +17
= 43
ab
cfde 17
26
e a b
15 9 8
0 up 0 up 0 up 1 1 left 1
1 up 1 up 2 2 left 2 up 2 up
1 1 up 2 up 2 up 3 3 left
1 up 2 2 up 2 up 3 up 3 up
Now for filling the blank cells we will
proceed as follows.
• For any particular blank cell: Look for the
value in the upper cell and the left cell of the
chosen cell.
• Whichever is the bigger value, we will put that
value in this chosen cell and put an arrow in
the direction of the bigger value OR
(depending upon which is bigger)
• However if the value are same we will put
upper cell value and put an arrow pointing
upwards in that cell
• Whenever we encounter a tilt arrow
• We will check the value in the upper diagonal
cell 0
(0+1)=1
Q
P: Pivot Element
i=0
j=1
T=8
X=9
Y=5
Z=7
Solution
B= 5
Example 2
A=2
C= 6
D=7
Solution
Bellman Ford Algorithm
• This is also a single source shortest path algorithm
• The difference between this and dijikstra algorithm is that, it can
also works in case the graph has negative edges
• Bellman-Ford algorithm returns a boolean value indicating
whether or not there is a negative-weight cycle that is reachable
from the source. If there is such a cycle, the algorithm indicates
that no solution exists. If there is no such cycle, the algorithm
produces the shortest paths and their weights.
5 2
3 4
3 3
3 2
3 2
7 2
7 2
3 4
3 3
Proceeding in the same way we find
D3 and S3
Now using D3 and S3 we will find the shortest path
Continuing in the same way we get
the final table as
• There are 19 balls which are identical in looks,
shapes and sizes. However one ball has less
weight than the other balls. What is the
minimum number of comparison in which you
can identify the ball when you have a
weighing balance.
9 9
(L) (R)
4 4
• There is a farmer who wishes to cross a river but
he is not alone. He also has a goat, a wolf, and a
cabbage along with him. There is only one boat
available that can support the farmer and either of
F,W,G,C
the goat, wolf or cabbage. So at a time, the boat
can have only two objects (farmerFG and one other).
F
• But the problem is, if the goat and wolf are left
FC
alone (either in the boat or onshore), the wolf will
eat the goat.
FG Similarly, if the Goat
C and cabbage are
left alone, then goat will eat the cabbage. How
C,F W
many minimum trips for the farmer to get
everything on the other side F,G,C,W
(without any harm)?
Crossing the river counts as one trip.
• There are 1000 wine bottles. One of the bottles
contains poisoned wine. A rat dies after one
hour of drinking the poisoned wine. How many
minimum rats are needed to figure out which
bottle contains poison in hour.
• A dealer has 1000 coins and 10 bags. He has
to divide the coins over the ten bags so that
he can make any number of coins simply by
handing over a few bags. How must divide his
money into the ten bags?
• You are blindfolded and 10 coins are place
in front of you on table. You are allowed to
touch the coins, but can’t tell which way
up they are by feeling them. You are told
that there are 5 coins head up, and 5 coins
tails up but not which ones are which.
• Can you make two piles of coins each
with the same number of heads
up? You can flip the coins any number of
times.
• 17 COINS
• TWO PLAYER GAME
• EACH CHANCE , A PLAYER CAN TAKE (1,2,3,OR MAX 4 COINS)
• HE CANNOT PASS THE CHANCE
• THE PLAYER WHO HAS THE LAST CHANCE TO PICK THE COIN IS
THE WINNER.
P1 P2
4 2
1 2
3