Technical Questions
Technical Questions
TCSSpecific TechnicalTraining
Complexity of Algorithms
• Evaluation of Algorithms can be done based on their performance
• Ω Best case
• ΘAveragecase
• O worst case
Time Complexity
•Algorithm evaluation in done on worst casecomplexity
Example O(1)
O(n)
O(n2)
O(nlogn)
Time complexity Calculations
• Ignore constants (coeificients)
O(1) < O(logn) < O(n) < O(nlogn) < O(n2) < O(2n) < O(n!)
X= 10 + 20*30
Y= 15 – 10
Print x+y
O(1) * n= O(n)
Linear Time
Y= 15 + (3 *5)
For x in range (0,n)
{
Print x
}
Y= 15 + (3 *5)
for x in range(0,n)
for y in range(0,n)
print x*y
𝒙= 𝒃𝒚 𝒕𝒉𝒆𝒏𝒚= log𝑏 𝒙
for(int j = 1;j<n; j *=c)
{
OR
for(int j = 1 ;j<n ; j / = c)
{
}
Sorting
• Arranging objects / elements in a list in specific order
• Ascending or Descending
• Types of Sorting
• Bubble sort
• Selection Sort
• Insertion Sort
• Merge Sort
• Quick Sort
Bubble sort
• Simplest sorting algorithm
11 25 12 22 64
/ / Find the minimum element i n a r r [ 1 . . . 4 ]
/ / and place i t a t beginning o f a r r [ 1 . . . 4 ]
11 12 25 22 64
/ / Find the minimum element i n a r r [ 2 . . . 4 ]
/ / and place i t a t beginning o f a r r [ 2 . . . 4 ]
11 12 22 25 64
/ / Find the minimum element i n a r r [ 3 . . . 4 ]
/ / and place i t a t beginning o f a r r [ 3 . . . 4 ]
11 12 22 25 64
Algorithm
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[j]<arr[i])
{
int temp =arr[i];
arr[i] =arr[j];
arr[j] =temp;
}
}
Insertion Sort
• Simple sorting algorithm building sorted array by choosing
one element at atime