DS Unit V Q&a
DS Unit V Q&a
DS Unit V Q&a
Data Structure
Unit – V
Sorting Techniques
The comparison operator is used to decide the new order of element in the respective
data structure.
The techniques of sorting can be divided into two categories. These are:
Internal Sorting
External Sorting
Internal Sorting:
If all the data that is to be sorted can be adjusted at a time in the main memory, the internal sorting
method is being performed.
External Sorting:
When the data that is to be sorted cannot be accommodated in the memory at the same time
and some has to be kept in auxiliary memory such as hard disk, floppy disk, magnetic tapes
etc, then external sorting methods are performed
To get the amount of time required to sort an array of 'n' elements by a particular method, the
normal approach is to analyze the method to find the number of comparisons (or exchanges)
required by it.
Most of the sorting techniques are data sensitive, and so the metrics for them depends on the order
in which they appear in an input array.
Various sorting techniques are analyzed in various cases and named these cases as follows:
Best case
Average case
Hence, the result of these cases is often a formula giving the average timerequired for a particular
sort of size 'n.'
Most of the sort methods have time requirements that range from O(nlog n) to O(n2).
An element which is to be inserted in this sorted sub-list, has to find its appropriate place and it
has to be inserted there.
The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-
list (in the same array).
This algorithm is not suitable for large data sets as its average and worst case complexity
are of Ο(n2), where n is the number of items.
Example.
It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.
It swaps 33 with 27. It also checks with all the elements of sorted sub-list. Here the sorted sub-list
has only one element 14, and 27 is greater than 14. Hence, the sorted sub-list remains sorted
after swapping.
swap them again. By the end of third iteration, display sorted sub-list of 4 items.
This process goes on until all the unsorted values are covered in a sorted sub-list.
Algorithm
Pseudocode
end for
end procedure
Example
Merge sort first divides the whole array iteratively into equal halves unless the atomic values are
achieved.
This does not change the sequence of appearance of items in the original.
Now divide these two arrays into halves.
Further divide these arrays and achieve atomic value which can no more be divided.
Now, combine them in exactly the same manner as they were broken down.
first compare the element for each list and then combine them into another list in a sorted manner.
Compare 27 and 10 and in the target list of 2 values put 10 first, followed by 27. Change the
order of 19 and 35 whereas 42 and 44 are placed sequentially.
In the next iteration of the combining phase, compare lists of two data values, and merge them
into a list of found data values placing all in a sorted order.
After the final merging, the list should look like this −
Then, merge sort combines the smaller sorted lists keeping the new list sorted too.
if ( n == 1 ) return a
var l1 as array = a[0] ... a[n/2] var l2
as array = a[n/2+1] ... a[n] l1 =
mergesort( l1 )
l2 = mergesort( l2 )
return merge( l1, l2 )
end procedure
array
while ( a and b have elements )
if ( a[0] > b[0] )
add b[0] to the end of c remove
b[0] from b
else
add a[0] to the end of c remove
a[0] from a
end if end
while
end procedure
Department of CS&IT SSDM COLLEGE
7
3. Explain about Bubble Sort with example?
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison- based algorithm
in which each pair of adjacent elements is compared and the elements are swapped if they are not
in order.
This algorithm is not suitable for large data sets as its average and worst case complexity are of
Ο(n2) where n is the number of items.
Example
Bubble sort starts with very first two elements, comparing them to check which one is greater.
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, compare 33
with 27.
Next we compare 33 and 35. Find that both are in already sorted positions.
swap these values. Find that we have reached the end of thearray. After one
iteration, the array should look like this −
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that an array is completely
sorted.
Algorithm
return list
end BubbleSort
Pseudocode
Observe in algorithm that Bubble Sort compares each pair of array element unless the whole
array is completely sorted in an ascending order.
This may cause a few complexity issues like what if the array needs no more swapping as all the
elements are already ascending.
To ease-out the issue, we use one flag variable swapped which will help, if any swap has
happened or not.
If no swap has occurred, i.e. the array requires no more processing to be sorted; it will come out
of the loop.
loop = list.count;
for i = 0 to loop-1 do:
swapped = false
for j = 0 to loop-1 do:
/* compare the adjacent elements */ if
list[j] > list[j+1] then
/* swap them */
swap( list[j], list[j+1] ) swapped
= true
end if
end for
Selection sort is a simple sorting algorithm. This sorting algorithm is an in- place
comparison-based algorithm in which the list is divided into two parts, the sorted part at the
left end and the unsorted part at the right end.
Initially, the sorted part is empty and the unsorted part is the entirelist.
The smallest element is selected from the unsorted array and swapped with the leftmost
element, and that element becomes a part of the sorted array.
This process continues moving unsorted array boundary by one element to the right.
This algorithm is not suitable for large data sets as its average and worst case complexities
are of Ο(n2), where n is the number of items.
Example.
For the first position in the sorted list, the whole list is scanned sequentially.
The first position where 14 is stored presently, search the whole list and find that 10 is the
lowest value.
For the second position, where 33 is residing, start scanning the rest of the list in a linear manner.
find that 14 is the second lowest value in the list and it should appear at the second place. Swap these
values.
Department of CS&IT SSDM COLLEGE
11
After two iterations, two least values are positioned at the beginning in a sorted manner.
The same process is applied to the rest of the items in the array.
Algorithm
This algorithm avoids large shifts as in case of insertion sort, if the smaller value is to the far
right and has to be moved to the far left.
This algorithm uses insertion sort on a widely spread elements, first to sort them and then sorts
the less widely spaced elements.
Knuth's Formula
h=h*3+1
where − h is interval with initial value 1
This algorithm is quite efficient for medium-sized data sets as its average and worst- case
complexity of this algorithm depends on the gap sequence the best known is Ο(n), where n is the
number of items. And the worst case space complexity is O(n).
Make a virtual sub-list of all values located at the interval of 4 positions. Here these
values are {35, 14}, {33, 19}, {42, 27} and {10, 44}
compare values in each sub-list and swap them (if necessary) in the original array. After this
step, the new array should look like this −
compare and swap the values, if required, in the original array. After this step,
the array should look like this −
Finally, sort the rest of the array using interval of value 1. Shell sort uses insertion sort to sort the
array.
Algorithm
Following is the algorithm for shell sort.
Step 1 − Initialize the value of h
Step 2 − Divide the list into smaller sub-list of equal interval h
Step 3 − Sort these sub-lists using insertion sort Step 3 −
Repeat until complete list is sorted
Pseudocode
procedure shellSort() A :
array of items
/* calculate interval*/
while interval < A.length /3 do:
interval = interval * 3 + 1
end while
end procedure
6. How Heap sort technique Works?
Heap sort processes the elements by creating the min-heap or max-heap using the elements of the given
array. Min-heap or max-heap represents the ordering of array in which the root element represents the
minimum or maximum element of the array.
Before knowing more about the heap sort, let's first see a brief description of Heap.
What is a heap?
A heap is a complete binary tree, and the binary tree is a tree in which the node can have the utmost two
children. A complete binary tree is a binary tree in which all the levels except the last level, i.e., leaf node,
should be completely filled, and all the nodes should be left-justified.
Heapsort is a popular and efficient sorting algorithm. The concept of heap sort is to eliminate the elements
one by one from the heap part of the list, and then insert them into the sorted part of the list.
Algorithm
1. HeapSort(arr)
2. BuildMaxHeap(arr)
3. for i = length(arr) to 2
4. swap arr[1] with arr[i]
5. heap_size[arr] = heap_size[arr] ? 1
6. MaxHeapify(arr,1)
7. End
BuildMaxHeap(arr)
1. BuildMaxHeap(arr)
2. heap_size(arr) = length(arr)
3. for i = length(arr)/2 to 1
4. MaxHeapify(arr,i)
5. End
MaxHeapify(arr,i)
In heap sort, basically, there are two phases involved in the sorting of elements. By using the heap sort
algorithm, they are as follows -
o The first step includes the creation of a heap by adjusting the elements of the array.
o After the creation of heap, now remove the root element of the heap repeatedly by shifting it to the
end of the array, and then store the heap structure with the remaining elements.
Now let's see the working of heap sort in detail by using an example. To understand it more clearly, let's
take an unsorted array and try to sort it using heap sort. It will make the explanation clearer and easier.
First, we have to construct a heap from the given array and convert it into max heap.
After converting the given heap into max heap, the array elements are -
Next, we have to delete the root element (89) from the max heap. To delete this node, we have to swap it
with the last node, i.e. (11). After deleting the root element, we again have to heapify it to convert it into
max heap.
Department of CS&IT SSDM COLLEGE
17
After swapping the array element 89 with 11, and converting the heap into max-heap, the elements of array
are -
In the next step, again, we have to delete the root element (81) from the max heap. To delete this node, we
have to swap it with the last node, i.e. (54). After deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 81 with 54 and converting the heap into max-heap, the elements of array
are -
In the next step, we have to delete the root element (76) from the max heap again. To delete this node, we
have to swap it with the last node, i.e. (9). After deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 76 with 9 and converting the heap into max-heap, the elements of array
are -
In the next step, again we have to delete the root element (54) from the max heap. To delete this node, we
have to swap it with the last node, i.e. (14). After deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 54 with 14 and converting the heap into max-heap, the elements of array
are -
In the next step, again we have to delete the root element (22) from the max heap. To delete this node, we
have to swap it with the last node, i.e. (11). After deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 22 with 11 and converting the heap into max-heap, the elements of array
are -
In the next step, again we have to delete the root element (14) from the max heap. To delete this node, we
have to swap it with the last node, i.e. (9). After deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 14 with 9 and converting the heap into max-heap, the elements of array
are -
Department of CS&IT SSDM COLLEGE
19
In the next step, again we have to delete the root element (11) from the max heap. To delete this node, we
have to swap it with the last node, i.e. (9). After deleting the root element, we again have to heapify it to
convert it into max heap.
After swapping the array element 11 with 9, the elements of array are -
Now, heap has only one element left. After deleting it, heap will be empty.
Now, let's see the time complexity of Heap sort in the best case, average case, and worst case. We will also
see the space complexity of Heapsort.
1. Time Complexity
Case Time Complexity
o Best Case Complexity - It occurs when there is no sorting required, i.e. the array is already sorted.
The best-case time complexity of heap sort is O(n logn).
o Average Case Complexity - It occurs when the array elements are in jumbled order that is not
properly ascending and not properly descending. The average case time complexity of heap sort
The time complexity of heap sort is O(n logn) in all three cases (best case, average case, and worst case).
The height of a complete binary tree having n elements is logn.
2. Space Complexity
Space Complexity O(1)
Stable N0
7.Bubble sort
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which
each pair of adjacent elements is compared and the elements are swapped if they are not in order. This
algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2)
where n is the number of items.
How Bubble Sort Works?
We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're keeping it short and
precise.
Bubble sort starts with very first two elements, comparing them to check which one is greater.
In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we compare 33 with 27.
We find that 27 is smaller than 33 and these two values must be swapped.
Next we compare 33 and 35. We find that both are in already sorted positions.
We know then that 10 is smaller 35. Hence they are not sorted.
We swap these values. We find that we have reached the end of the array. After one iteration, the array
should look like this −
To be precise, we are now showing how an array should look like after each iteration. After the second
iteration, it should look like this −
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that an array is completely sorted.
return list
end BubbleSort
Pseudocode
We observe in algorithm that Bubble Sort compares each pair of array element unless the whole array is
completely sorted in an ascending order. This may cause a few complexity issues like what if the array
needs no more swapping as all the elements are already ascending.
To ease-out the issue, we use one flag variable swapped which will help us see if any swap has happened
Department of CS&IT SSDM COLLEGE
22
or not. If no swap has occurred, i.e. the array requires no more processing to be sorted, it will come out of
the loop.
Pseudocode of BubbleSort algorithm can be written as follows −
loop = list.count;
end for
end for