ADSAD
ADSAD
1. Compare and contrast Quick sort and merge sort. Mention their worst time complexities. And
comment on which one is better on the basis of time complexity. Also, write algorithm of quick sort.
Ans. A) Compare and contrast Quick sort and merge sort
Time Complexity O(n) for 3 way partition or O(n log n) simple partition O(n log n) O(n2)
In quick sort, using 3 way partitioning, it is possible to bring best case time complexity to O(n), which
will be better than merge sort best case performance of O(n log n).
In worst case, merge sort still maintains performance of O(n log n), but quick sort falls at O(n 2).
D) Also, write algorithm of quick sort.
This algorithm follows divide and conquer approach.
Algorithm
PARTITION (ARR, BEG, END, LOC)
Step 1: [INITIALIZE] SET LEFT = BEG, RIGHT = END, LOC = BEG, FLAG =0
Step 2: Repeat Steps 3 to 6 while FLAG !=1
Step 3: Repeat while ARR[LOC] <=ARR[RIGHT]
AND LOC != RIGHT
SET RIGHT = RIGHT - 1
[END OF LOOP]
Step 4: IF LOC = RIGHT
SET FLAG = 1
ELSE IF ARR[LOC] > ARR[RIGHT]
SWAP ARR[LOC] with ARR[RIGHT]
SET LOC = RIGHT
[END OF IF]
Step 5: IF FLAG = 0
Repeat while ARR[LOC] >= ARR[LEFT] AND LOC != LEFT
SET LEFT = LEFT + 1
[END OF LOOP]
Step 6:IF LOC = LEFT
SET FLAG = 1
ELSE IF ARR[LOC] < ARR[LEFT]
SWAP ARR[LOC] with ARR[LEFT]
SET LOC = LEFT
[END OF IF]
[END OF IF]
Step 7: [END OF LOOP]
Step 8: END
2. Is it possible to perform binary search on linked list? Write algorithm to traverse a linked list.
Ans. Binary search is possible on linked list data structure but there is no point using it, because at the
end it would take same time as normal linear search.
Binary search reduces the number of elements to be searched by filtering out almost half the number of
elements at each iteration. For the best implementation of Binary search we need two prerequisites:
Address of any random element = Base address of the array + index of the element*size of the element
The above calculation makes it possible to access any element in an array in constant time. So binary
search is preferred in array list, but not in linked list.
Conclusion:
As far as possibility of binary search in linked list is concerned, yes it is possible. But there is no proper
significance of it. Basically, it is unnecessary.
STEP 7: EXIT
3. Given an unsorted array of numbers, you have to search the 2nd largest number. Try to keep
time and space complexity minimum.
Ans.
Program:
public class Main {
public static void main(String[] args) {
int[] myNum = {54, 32, 67, 45};
int max1=0, max2=0;
int size = myNum.length;
System.out.println("array is of length:"+size);
for(int i=0;i<size;i++) {
if(myNum[i]>max1){
max2=max1;
max1=myNum[i];
}
}
System.out.println("2nd largest number is:"+max2);
}
}
Code test:
Code Test:
5. Define space complexity and time complexity.
Ans. Both are parameters to judge which solution is more optimal.
Independent of the machine and its configuration, on which the algorithm is running on.
Shows a direct correlation with the number of inputs.
Can distinguish two algorithms clearly without ambiguity.
Space Complexity: Space complexity of an algorithm represents the amount of memory space needed the
algorithm in its life cycle.
Space needed by an algorithm is equal to the sum of the following two components
A fixed part that is a space required to store certain data and variables (i.e. simple variables and constants,
program size etc.), that are not dependent of the size of the problem.
A variable part is a space required by variables, whose size is totally dependent on the size of the
problem. For example, recursion stack space, dynamic memory allocation etc.
Time Complexity
Time Complexity of an algorithm is the representation of the amount of time required by the algorithm to
execute to completion. Time requirements can be denoted or defined as a numerical function t(N), where
t(N) can be measured as the number of steps, provided each step takes constant time.