Answer:
1. Average of total Cost = Total Cost/n = O(1)
2. Average of total Cost = Total Cost/n = O(n)
Step-by-step explanation:
1) Array has Initial size of 8
For adding 16 elements
1. So Cost for first 8 element its 8.
2. On adding 9th element cost is (8+1) = 9 and array is resized to size of 16
3. Then for 10th,11th,12th,.......,16th element Cost is 7.
So total cost is 8 +9 + 7 = 24 ..So for adding 16 elements Cost is 24 units.
For adding 32 elements
1. So Cost for first 8 element its 8.
2. On adding 9th element cost is (8+1) = 9 and array is resized to size of 16
3. Then for 10th,11th,12th,.......,16th element total Cost is 7.
4. On adding 17th element cost is (16+1) = 17 and array is resized to size of 32.
5. Then for 18th,19th,20th,.......,32th element total Cost is 15.
So total cost is 8 +9 + 7 +17+15= 56 ..So for adding 32 elements Cost is 56 units.
We are Intrested in finding the Amortized Cost i.e Averga cost over large number of insertion:-
When the array size is doubled:-
N operation of N pushes will have cost of 1+2+4+8+16+.. + 2k where 2k < N, Summing upo the series we have
2k-1 - 2 . But 2k <=n <= 2k-1 So,
The total cost of the sequence of n insertions will be O(n) .
Since the Amortized Cost is the Average of total Cost = Total Cost/n = O(1)
2) resize by increasing the size by 2 elements
For adding 16 elements
1. So Cost for first 8 element its 8.
2. On adding 9th element cost is (8+1) = 9 and array is resized to size of 10
3. Then for 10th element Cost is 1
4. Then for 11th element Cost is 11
5.Then for 12th element Cost is 1
6.Then for 13th element Cost is 13
6.Then for 14th element Cost is 1
6.Then for 15th element Cost is 15
6.Then for 16th element Cost is 1
So total cost is 60 ..So for adding 16 elements Cost is 60 units.
For adding 32 elements
1. So Cost for first 8 element its 8.
2. On adding 9th element cost is (8+1) = 9 and array is resized to size of 10
3. Then for 10th element Cost is 1
4. Then for 11th element Cost is 11
5.Then for 12th element Cost is 1
7.Then for 13th element Cost is 13
8.Then for 14th element Cost is 1
9.Then for 15th element Cost is 15
10.Then for 16th element Cost is 1
11. Then for 17th element Cost is 17
12. Then for 18th element Cost is 1
13.Then for 19th element Cost is 19
14.Then for 20th element Cost is 1
15.Then for 21th element Cost is 21
16.Then for 22th element Cost is 1
17.Then for 23th element Cost is 23 .. . . .. .. Then for 31th element Cost is 31 .. . . . . . . . . .. Then for 32th element Cost is 1 .
So total cost is 260 ..So for adding 32 elements Cost is 260 units.
N operation of N pushes will have cost of 1+2+3+4+5+.. + N-1 = N(N-1)/2
The total cost of the sequence of n insertions will be O(n2) .
Since the Amortized Cost is the Average of total Cost = Total Cost/n = O(n)