M01S05 Daa-Unit-1-Time Space Tradeoff
M01S05 Daa-Unit-1-Time Space Tradeoff
Module 1
Video Link
• Take two arrays, one for the input and the other for the output.
• Read the elements of the first array in reverse linear order and place them in
the second array linearly from the beginning.
Second Method
• Swap the first and the last elements, then swap the next two immediate
elements one from each end, and so on. This process is repeated until all the
elements of the array get swapped.
• No extra array is used.
First Method
Second Method
int reverse (int ary1[ ], int n)
• Only one array of size ‘n’ and a {
temporary variable ‘temp’, inside int k = floor(n/2);
for i=0 to k-1
the swap function is used.
swap (ary1[i], ary1[(n-1) – i )];
• Total space - n+1 }
• For each swapping - 3 swap (int a, int b)
{
assignments are required
int temp = a;
• Number of swapping - Atmost a = b;
half-time the size of the array b = temp;
• Time complexity - 3n/2 }
0 1 2 3 4
5 6 7 8 9
0 1 2 3 4
9 8 7 6 5
int ary2[];
ary2[i]=ary1[(n-1) - i];
0 1 2 3 4
5 6 7 8 9
Try yourself
1. Write algorithms for the following and analyze the space taken by each of the
algorithms. Show that the concept of Time-Space tradeoff holds. Is it possible to
reduce the amount of memory and increase the speed of an algorithm
simultaneously?
Find the leaders in the given set of elements (the element which is larger
than the elements ahead of it
Remove duplicates in a list of elements
Sort the elements given in the list
Reversing a list of elements
Find the number of occurrences of an element
Produce a sorted list from 2 unsorted lists
2. List the functions in the increasing order of asymptotic complexity. n^2, 2^n ,
nlogn, n^3