0% found this document useful (0 votes)
18 views44 pages

ALP Laborator 3: Algoritmi de Sortare

The Agent has 2 weapons and must kill an enemy with 6 health. Weapon 1 does 4 damage and weapon 2 does 2 damage. The minimum number of moves needed is 2 - the Agent uses weapon 1 first, reducing the enemy's health to 6-4=2, then uses weapon 2 to finish off the enemy.

Uploaded by

Deniz Maximilian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views44 pages

ALP Laborator 3: Algoritmi de Sortare

The Agent has 2 weapons and must kill an enemy with 6 health. Weapon 1 does 4 damage and weapon 2 does 2 damage. The minimum number of moves needed is 2 - the Agent uses weapon 1 first, reducing the enemy's health to 6-4=2, then uses weapon 2 to finish off the enemy.

Uploaded by

Deniz Maximilian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

ALP Laborator 3

Algoritmi de sortare
Bubble Sort

1 42 8 4 16 23 15

8 42 4 16 23 15

8 4 42 16 23 15

8 4 16 42 23 15

8 4 16 23 42 15

8 4 16 23 15 42
Bubble Sort

2 8 4 16 23 15 42

4 8 16 23 15 42

4 8 16 23 15 42

4 8 16 23 15 42

4 8 16 15 23 42

4 8 16 15 23 42
Bubble Sort

3 4 8 16 15 23 42

4 8 16 15 23 42

4 8 16 15 23 42

4 8 15 16 23 42

4 8 15 16 23 42

4 8 15 16 23 42
Bubble Sort - no swap loop

4 4 8 15 16 23 42

4 8 15 16 23 42

4 8 15 16 23 42

4 8 15 16 23 42

4 8 15 16 23 42

4 8 15 16 23 42
Bubble Sort
1 3
(42, 8, 4, 16, 23, 15) –> (8, 42, 4, 16, 23, 15), (4, 8, 16, 15, 23, 42) –> (4, 8, 16, 15, 23, 42),

(8, 42, 4, 16, 23, 15) –> (8, 4, 42, 16, 23, 15), (4, 8, 16, 15, 23, 42) -> (4, 8, 16, 15, 23, 42),

(8, 4, 42, 16, 23, 15) –> (8, 4, 16, 42, 23, 15), (4, 8, 16, 15, 23, 42) -> (4, 8, 15, 16, 23, 42),

(8, 4, 16, 42, 23, 15) –> (8, 4, 16, 23, 42, 15), (4, 8, 15, 16, 23, 42) -> (4, 8, 15, 16, 23, 42),

(8, 4, 16, 23, 42, 15) –> (8, 4, 16, 23, 15, 42). (4, 8, 15, 16, 23, 42) -> (4, 8, 15, 16, 23, 42).

_____________________________________ _____________________________________
2
(8, 4, 16, 23, 15, 42) -> (4, 8, 16, 23, 15, 42), (4, 8, 15, 16, 23, 42) -> (4, 8, 15, 16, 23, 42),
4
(4, 8, 16, 23, 15, 42) -> (4, 8, 16, 23, 15, 42), (4, 8, 15, 16, 23, 42) -> (4, 8, 15, 16, 23, 42),

(4, 8, 16, 23, 15, 42) -> (4, 8, 16, 23, 15, 42), (4, 8, 15, 16, 23, 42) -> (4, 8, 15, 16, 23, 42),

(4, 8, 16, 23, 15, 42) -> (4, 8, 16, 15, 23, 42), (4, 8, 15, 16, 23, 42) -> (4, 8, 15, 16, 23, 42),

(4, 8, 16, 15, 23, 42) -> (4, 8, 16, 15, 23, 42). (4, 8, 15, 16, 23, 42) -> (4, 8, 15, 16, 23, 42).

____________________________________ ____________________________________
Insertion Sort
42 8 4 16 23 15

42 8 4 16 23 15

8 42 4 16 23 15
Insertion Sort
8 42 4 16 23 15

8 42 4 16 23 15

8 4 42 16 23 15

4 8 42 16 23 15
Insertion Sort
4 8 42 16 23 15

4 8 42 16 23 15

4 8 16 42 23 15
Insertion Sort
4 8 16 42 23 15

4 8 16 42 23 15

4 8 16 23 42 15
Insertion Sort
4 8 16 23 42 15

4 8 16 23 42 15

4 8 16 23 15 42

4 8 16 15 23 42

4 8 15 16 23 42
Insertion Sort
(42, 8, 4, 16, 23, 15) -> (42, 8, 4, 16, 23, 15) -> (8, 42, 4, 16, 23, 15),

(8, 42, 4, 16, 23, 15) -> (8, 42, 4, 16, 23, 15) -> (8, 4, 42, 16, 23, 15) -> (4, 8, 42, 16, 23, 15)

(4, 8, 42, 16, 23, 15) -> (4, 8, 42, 16, 23, 15) -> (4, 8, 16, 42, 23, 15)

(4, 8, 16, 42, 23, 15) -> (4, 8, 16, 42, 23, 15) -> (4, 8, 16, 23, 42, 15)

(4, 8, 16, 23, 42, 15) -> (4, 8, 16, 23, 42, 15) -> (4, 8, 16, 23, 15, 42) -> (4, 8, 16, 15, 23, 42) -> (4, 8, 15, 16, 23, 42)
Selection Sort
42 8 4 16 23 15

min() = 4

4 8 42 16 23 15
Selection Sort
4 8 42 16 23 15

min() = 8

4 8 42 16 23 15
Selection Sort
4 8 42 16 23 15

min() = 15

4 8 15 16 23 42
Selection Sort
4 8 15 16 23 42

min() = 16

4 8 15 16 23 42
Selection Sort
4 8 15 16 23 42

min() = 23

4 8 15 16 23 42
Selection Sort
(42, 8, 4, 16, 23, 15) => min(42, 8, 4, 16, 23, 15) => (4, 8, 42, 16, 23, 15)

(4, 8, 42, 16, 23, 15) => min(8, 42, 16, 23, 15) => (4, 8, 42, 16, 23, 15)

(4, 8, 42, 16, 23, 15) => min(42, 16, 23, 15) => (4, 8, 15, 16, 23, 42)

(4, 8, 15, 16, 23, 42) => min(16, 23, 42) => (4, 8, 15, 16, 23, 42)

(4, 8, 15, 16, 23, 42) => min(23, 42) => (4, 8, 15, 16, 23, 42)

(4, 8, 15, 16, 23, 42)


Merge Sort
42 8 4 16 23 15
Merge Sort
42 8 4 16 23 15

42 8 4
Merge Sort
42 8 4 16 23 15

42 8 4

42 8
Merge Sort
42 8 4 16 23 15

42 8 4

42 8

42 8
Merge Sort
42 8 4 16 23 15

42 8 4

42 8

42 8

8 42
Merge Sort
42 8 4 16 23 15

42 8 4

42 8

42 8
8 42 4

8 42

4 8 42
Merge Sort
42 8 4 16 23 15

42 8 4 16 23 15

42 8

42 8
8 42 4

8 42

4 8 42
Merge Sort
42 8 4 16 23 15

42 8 4 16 23 15

42 8

16 23

42 8
8 42 4

8 42

4 8 42
Merge Sort
42 8 4 16 23 15

42 8 4 16 23 15

42 8

16 23

42 8
8 42 4
16 23

8 42

4 8 42
Merge Sort
42 8 4 16 23 15

42 8 4 16 23 15

42 8

16 23

42 8
8 42 4
16 23

8 42

4 8 42 16 23
Merge Sort
42 8 4 16 23 15

42 8 4 16 23 15

42 8

16 23

42 8
8 42 4
16 23 16 23 15

8 42

4 8 42 16 23 15 16 23
Merge Sort
4 8 42 15 16 23

4 8 15 16 23 42
Quick Sort
42 8 4 16 23 15

8 4 42 16 23 15

8 4 15 16 23 42
Quick Sort
8 4 15 16 23 42

8 4 15 16 23 42

4 8 15 16 23 42
Quick Sort
8 4 15 16 23 42

8 4 15 16 23 42
Quick Sort
8 4 15 16 23 42

8 4 15 16 23 42
The Game!
Two players play a game.

Initially there are n integers a1,a2,…,an written on the board. Each turn a player selects one number and erases it
from the board. This continues until there is only one number left on the board, i. e. n−1 turns are made. The first
player makes the first move, then players alternate turns.

The first player wants to minimize the last number that would be left on the board, while the second player wants to
maximize it.

You want to know what number will be left on the board after n−1 turns if both players make optimal moves.
The Game!
213

The first player erases 3

The second erases 1.

2 is left on the board.


Kill the enemy
There is an Agent and he has n weapons. The i-th weapon has a damage value ai, and the Agent will face an enemy
whose health value is H.

The Agent will perform one or more moves until the enemy dies.

In one move, he will choose a weapon and decrease the enemy's health by its damage value. The enemy will die
when his health will become less than or equal to 0. However, not everything is so easy: the Agent can't choose the
same weapon for 2 times in a row.

What is the minimum number of times that the Agent will need to use the weapons to kill the enemy?
Kill the enemy
Input:
2 4 (2- number of weapons, 4 - enemy health)
3 7 (3 - damage of first weapon, 7 - damage of second weapon)

Output: 1
The Agent can use the second weapon, making health value of the enemy equal
to 4−7=−3. −3≤0, so the enemy is dead, and using weapon 1 time was enough.
Kill the enemy
Input:

2 6 (2- number of weapons, 6 - enemy health)

4 2 (4 - damage of first weapon, 2 - damage of second weapon)

Output: 2

The Agent can use the first weapon first, and then the second one. After this, the health
of enemy will drop to 6−4−2=0, meaning he would be killed after using weapons 2 times.
Kill the enemy
Input:
3 11 (3- number of weapons, 11 - enemy health)
2 1 7 (2 - damage of first weapon, 1 - damage of second weapon, 7 - damage of third
weapon)

Output: 3
The Agent can use the weapons in order (third, first, third), decreasing the health value of
enemy to 11−7−2−7=−5 after using the weapons 3 times. Note that we can't kill the
enemy by using the third weapon twice, as even though 11−7−7<0, it's not allowed to use
the same weapon twice in a row.
Barrels
You have n barrels lined up in a row, numbered from left to right from one. Initially, the i-th barrel contains ai liters of water.

You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels x and y (the x-th barrel shouldn't be empty) and pour any possible
amount of water from barrel x to barrel y (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them.

Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most k times.

Some examples:

if you have four barrels, each containing 5 liters of water, and k=1, you may pour 5 liters from the second barrel into the fourth, so the amounts of water in the barrels are
[5,0,5,10], and the difference between the maximum and the minimum is 10;

if all barrels are empty, you can't make any operation, so the difference between the maximum and the minimum amount is still 0
Barrels
Input:

4 1 (4 barrels, 1 move)

5 5 5 5 (capacity of each of the 4 barrels)

Output: 10
Barrels
Input:

4 1 (4 barrels, 1 move)

4 5 6 7 (capacity of each of the 4 barrels)

Output: 13
Barrels
Input:

3 2 (3 barrels, 2 moves)

0 0 0 (capacity of each of the 3 barrels)

Output: 0

You might also like