Data Structure Unit 3 Important Questions
Data Structure Unit 3 Important Questions
Data Structure Unit 3 Important Questions
Answer:
Quick sort is a highly efficient sorting algorithm and is based on
partitioning of array of data into smaller arrays. A large array is
partitioned into two arrays one of which holds values smaller than
the specified value, say pivot, based on which the partition is made
and another array holds values greater than the pivot value.
Quicksort partitions an array and then calls itself recursively
twice to sort the two resulting subarrays. This algorithm is
quite efficient for large-sized data sets as its average and
worst-case complexity are O(nLogn)
Algorithm:
Step 1 − Choose the highest index value has pivot
Step 2 − Take two variables to point left and right of the list
excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left ≥ right, the point where they met is new pivot
Time Complexity:
The time complexity of quick sort algorithm is of O(n log n).
Ques 17 What is quick sort? Sort the given values using quick sort: present
all steps/iterations:
38, 81, 22, 48, 13, 69, 93, 14, 45, 58, 79, 72
AKTU 2016-17, Marks 10
Answer:
Ques 18 Write algorithm for quick sort.trace your algorithm on the
following data to sort the list: 2, 13, 4, 21, 7, 56, 51, 85, 59, 1, 9, 10. How
the choice of pivot elements affects the efficiency of algorithm?
AKTU 2018-19, Marks 07
Answer:
Choice of pivot element affects the efficiency of algorithm:
If pivot element is chosen from first or last element of the given
array then it results in worst case scenario with O(n2) time
complexity.
If it is the median of the array then the array is divided into two
halves every time and results in best or average case scenario with
O(n logn )time complexity.
Ques 19 Use quick sort algorithm to sort 15, 22, 30, 10, 15, 64, 1, 3, 9, 2.
Is it a stable algorithm? Justify.
AKTU 2017-18, Marks 07
Answer:
Write a short note on insertion sort.
AKTU 2014-15, Marks 05
Answer:
Insertion sort is a sorting algorithm in which the elements are
transferred one at a time to the right position. In other words, an
insertion sort helps in building the final sorted list, one item at a
time, with the movement of higher-ranked elements. An insertion
sort has the benefits of simplicity and low overhead.
Insertion sort is a simple sorting algorithm that works the way we
sort playing cards in our hands.
Algorithm
// Sort an arr[] of size n
insertionSort(arr, n)
Loop from i = 1 to n-1.
……a) Pick element arr[i] and insert it into sorted sequence arr[0…i-1]
Analysis:
Complexity of best case: O(n)
Complexity of average case:O(n2)
Complexity of worst case:O(n2)
return list
end BubbleSort
Analysis:
Complexity of best case: O(n)
Complexity of average case:O(n2)
Complexity of worst case:O(n2)
OR
Separate Chaining:
This technique creates a linked list to the slot for which collision
occurs.The new key is then inserted in the linked list.These linked
lists to the slots appear like chains.
Linear Probing:
Linear probing is a scheme in computer programming for resolving
collisions in hash tables, data structures for maintaining a collection
of key–value pairs and looking up the value associated with a given
key
Hash function is defined by:
h(k,i) = (h’(k) + i) mod m
Where m is the size of the hash table and h’(k) = k mod m is a basic
hash function
Quadratic Probing:
Quadratic probing is an open addressing scheme in computer
programming for resolving hash collisions in hash tables. Quadratic
probing operates by taking the original hash index and adding
successive values of an arbitrary quadratic polynomial until an open
slot is found.
h(k,i) = (h’(k) + c1i+c2i2) mod m
Where h’ is the auxiliary hash function, c1, c2 not equal to 0 are
auxiliary constants and i=0,1,...,m-1
Double hashing:
In this function, permutations produced have many of the
characteristics of randomly chosen permutations.
Hash function is defined by:
h(k,i) = (h1(k) + i h2(k) ) mod m
Where h1 and h2 are the auxiliary hash functions,and m is the size of
the hash table.
Ques 9 What do you mean by hashing and collision? Discuss the
advantages and disadvantages of hashing over other searching techniques.
AKTu 2014-15, Marks 10
Answer:
Advantages:
Disadvantages:
Algorithm:
for i=1 to n
vmin= voxel(min(bbox(object[i])))
vmax= voxel(max(bbox(object[i])))
for x= vmin to x= vmax
for y= vmin to y= vmax
for z= vmin to z= vmax
for j=1 to n_objects(voxel(x,y,z))
if(not tested (object[i],object[j])
intersect (object[i],object[j])
OR
Describe two ways merge sort method. Explain the complexities Of merge
sort method.
Answer:
Time Complexity:
Best Case Complexity: O(n*log n)
Worst Case Complexity: O(n*log n)
Average Case Complexity: O(n*log n)
Space Complexity:
The space complexity of merge sort is O(n).
Ques 21 Write an algorithm for merge sorting. Using the algorithm sort in
ascending order : 10, 25, 16, 5, 35, 48, 8
AKTU 2014-15, Marks 10
Answer:
Merge Sort Program:
void mergeSort(int a[], int p, int r)
{
int q;
if(p < r)
{
q = (p + r) / 2;
mergeSort(a, p, q);
mergeSort(a, q+1, r);
merge(a, p, q, r);
}
}
void merge(int a[], int p, int q, int r)
{
int b[5];
int i, j, k;
k = 0;
i = p;
j = q + 1;
while(i <= q && j <= r)
{
if(a[i] < a[j])
{
b[k++] = a[i++];
}
else
{
b[k++] = a[j++];
}
}
while(i <= q)
{
b[k++] = a[i++];
}
while(j <= r)
{
b[k++] = a[j++];
}
int main()
{
int arr[] = {32, 45, 67, 2, 7};
int len = sizeof(arr)/sizeof(arr[0]);
printf("Given array: \n");
printArray(arr, len);