Vous êtes sur la page 1sur 5

Quick Sort also uses divide and conquer technique like merge sort, but does not require

additional storage space. It is one of the most famous comparison based


sorting algorithmwhich is also called as partition exchange sort. Like merge sort, it also uses
recursive call for sorting elements.
In Quick Sort pivot element is chosen and partition the array such that all elements smaller
than pivot are arranged to left of pivot and element bigger than pivot are arranged to its
right.
There are various ways to choose pivot element:

1. Chose pivot as first element.


2. Chose pivot as end element.
3. Chose pivot as median element.
4. Chose pivot as random element.
5. Quick Sort is also a good example of a recursive algorithm.
6. We can express time complexity of quick sort by this recurrence relation:
T(n) = T(k) + T(n-k-1)+ Θ(n).
T(k) -> recursion relation for elements left of pivot. k is a number of element smaller
than the pivot.
T(k) -> recursion relation for elements right of pivot.
Θ(n) -> It is for partition process.
7. Time complexity of Quick Sort is O(n*logn) in best and average case and O(n*n) in
the worst case.
Worst case is one when all elements of given array are smaller than pivot or larger
than the pivot.
8. Worst case can be easily eliminated by choosing random element as a pivot or best
way is to choose median element as a pivot.
9. It is an in-place sorting algorithm as it requires small additional storage space.
10. Quick Sort is not a stable sort, which means the “equal” elements may not appear in
the same order in the sorted array as they were in the unsorted array.

a.
b.
c. Distinguish between AVL and Red-Black trees [5 marks]

Difference:
1. AVL trees provide faster lookups than Red Black Trees because they are more strictly
balanced.
2. Red Black Trees provide faster insertion and removal operations than AVL trees as
fewer rotations are done due to relatively relaxed balancing.
3. AVL trees store balance factors or heights for each node, thus requires O(N) extra
spacewhereas Red Black Tree requires only 1 bit of information per node, thus
require O(1) extra space.
4. Red Black Trees are used in most of the language libraries
like map, multimap, multiset in C++ whereas AVL trees are used in databases where
faster retrievals are required
You are given the following binary search tree.

d. Colour the nodes of the tree red and black so that it becomes a valid red-black tree. If
you don't have a coloured pen, you could e.g. draw a circle for red nodes and a square
for black nodes. [15
marks]
e. Insert 60 into the tree using the red-black insertion algorithm. Write down the final tree.
Please use this simple rules to color your tree

1) Every node has a color either red or black.


2) Root of tree is always black.
3) There are no two adjacent red nodes (A red node cannot have a red parent or red child).
4) Every path from root to a NULL node has same number of black nodes.

A chain of 3 nodes is nodes is not possible in Red-Black Trees.


Following are NOT Red-Black Trees
30 30 30
/\ / \ / \
20 NIL 20 NIL 20 NIL
/\ /\ / \
10 NIL 10 NIL 10 NIL
Violates Violates Violates
Property 4. Property 4 Property 3
Question 3

a. what is the difference between dynamic programming and divide and conquer
[6]
b. Explain what you understand by greedy algorithm [4]

Techopedia explains Greedy Algorithm

A greedy algorithm works by choosing the best possible answer in each step and then
moving on to the next step until it reaches the end, without regard for the overall solution.
It only hopes that the path it takes is the globally optimum one, but as proven time and
again, this method does not often come up with a globally optimum solution. In fact, it is
entirely possible that the most optimal short-term solutions lead to the worst possible
global outcome.
For example: Take the path with the largest sum overall. A greedy algorithm would take the
blue path, as a result of shortsightedness, rather than the orange path, which yields the
largest sum.
c. Describe the merge sort algorithm [5]

Merge Sort is a divide and conquer algorithm. It works by recursively breaking down a problem
into two or more sub-problems of the same or related type, until these become simple enough
to be solved directly. The solutions to the sub-problems are then combined to give a solution
to the original problem. So Merge Sort first divides the array into equal halves and then
combines them in a sorted manner

d. Write a C program that implements bubblesort to sort an array of 10 integers


[10]
#include<stdio.h>
int main() {
int a[10], n, i, j, temp = 0;

printf("Enter how many numbers you want:\n");


scanf("%d", &n);

printf("Enter the %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}

printf("\n\t\tThe given array is:\n");


for (i = 0; i < n; i++) {
printf("\n\t\t%d", a[i]);
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

printf("\n\n\n\t\tThe sorted array using Buble sort is:\n");


for (i = 0; i < n; i++) {
printf("\n\t\t%d", a[i]);
}
return 0;

Dynamic programming Divide and conquer


Sub programs are dependent of each other Sub programs are not dependent of each
other
Store solution of sub program Does not store solution of sub program
Bottom up algorithm Top down algorithm

Vous aimerez peut-être aussi