DS Unit V Q&a

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

1

Data Structure
Unit – V

Sorting Techniques

 Sorting refers to arranging data in a particular format.

 Sorting algorithm specifies the way to arrange data in a particularorder.

 Most common orders are in numerical or lexicographical order.

 A Sorting Algorithm is used to rearrange a given array or list elements according


to a comparison operator on the elements.

 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

Department of CS&IT SSDM COLLEGE


2
 Worst 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).

1. Explain about Insertion Sort with example?

This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is


always sorted.

For example, the lower part of an array is maintained to be sorted.

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.

Hence the name, insertion sort.

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.

How Insertion Sort Works?

Example.

Insertion sort compares the first two elements.

It finds that both 14 and 33 are already in ascending order. For now, 14 is in sorted sub-list.

Insertion sort moves ahead and compares 33 with 27.

Department of CS&IT SSDM COLLEGE


3

And finds that 33 is not in the correct position.

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.

By now 14 and 27 in the sorted sub-list. Next, it compares 33 with 10.

These values are not in a sorted order.

However, swapping makes 27 and 10 unsorted.

Again find 14 and 10 in an unsorted order.

swap them again. By the end of third iteration, display sorted sub-list of 4 items.

Department of CS&IT SSDM COLLEGE


4

This process goes on until all the unsorted values are covered in a sorted sub-list.
Algorithm

Step 1 − If it is the first element, it is already sorted. return 1;


Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be
sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted

Pseudocode

procedure insertionSort( A : array of items ) int


holePosition
int valueToInsert

for i = 1 to length(A) inclusive do:


/* select value to be inserted */
valueToInsert = A[i] holePosition = i

/*locate hole position for the element to be inserted */

while holePosition > 0 and A[holePosition-1] > valueToInsert do:


A[holePosition] = A[holePosition-1]
holePosition = holePosition -1 end
while

/* insert the number at hole position */


A[holePosition] = valueToInsert

end for

end procedure

2. Explain about Merge Sort with example?

Merge sort is a sorting technique based on divide and conquer technique.


Worst-case time complexity being Ο(n log n), it is one of the most respected algorithms.
Merge sort first divides the array into equal halves and then combines them in a sorted manner.

How Merge Sort Works?

Example

Department of CS&IT SSDM COLLEGE


5

Merge sort first divides the whole array iteratively into equal halves unless the atomic values are
achieved.

An array of 8 items is divided into two arrays of size 4.

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.

14 and 33 are in sorted positions.

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 −

Department of CS&IT SSDM COLLEGE


6
Algorithm
Merge sort keeps on dividing the list into equal halves until it can no more be divided. By
definition, if it is only one element in the list, it is sorted.

Then, merge sort combines the smaller sorted lists keeping the new list sorted too.

Step 1 − if it is only one element in the list it is already sorted, return.


Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
Pseudocode
Algorithms point out two main functions − divide & merge. Merge sort

works with recursion .

procedure mergesort( var a as array )

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

procedure merge( var a as array, var b as array ) var c as

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

while ( a has elements ) add


a[0] to the end of c remove
a[0] from a
end while

while ( b has elements ) add


b[0] to the end of c remove
b[0] from b
end while
return c

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.

How Bubble Sort Works?

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.

Finding 27 is smaller than 33 and these two values must be swapped.

The new array should look like this −

Next we compare 33 and 35. Find that both are in already sorted positions.

Department of CS&IT SSDM COLLEGE


8
Then we move to the next two values, 35 and 10.

10 is smaller 35. Hence they are not sorted.

swap these values. Find that we have reached the end of thearray. After one
iteration, the array should look like this −

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.

Algorithm

Assume list is an array of n elements.


Assume that swap function swaps the values of the given arrayelements. begin
BubbleSort(list)

for all elements of list if


list[i] > list[i+1]
swap(list[i], list[i+1]) end
if
end for

Department of CS&IT SSDM COLLEGE


9

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.

Pseudocode of BubbleSort algorithm can be written as follows − procedure


bubbleSort( list : array of items )

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

/*if no number was swapped that means array is


sorted now, break the loop.*/

if(not swapped) then


break
end if end
for

end procedure return list

Department of CS&IT SSDM COLLEGE


10
4. Explain about Selection Sort with example?

 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.

How Selection Sort Works?

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.

Replace 14 with 10.


After one iteration 10, which happens to be the minimum value in the list, appears in the first
position of the sorted list.

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

Step 1 − Set MIN to location 0


Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted
Pseudocode

procedure selection sort list


: array of items
n : size of list
Department of CS&IT SSDM COLLEGE
12
for i = 1 to n - 1
/* set current element as minimum*/ min = i
/* check the element to be minimum */ for j =
i+1 to n
if list[j] < list[min] then min =
j;
end if
end for

/* swap the minimum element with the current element*/ if


indexMin != i then
swap list[min] and list[i] end if
end for
end procedure

5. Explain about Shell Sort?


Shell sort is a highly efficient sorting algorithm and is based on insertion sort 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.

This spacing is termed as interval.

This interval is calculated based on Knuth's formula as −

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).

How Shell Sort Works?

For example and ease of understanding, we take the interval of 4.

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}

Department of CS&IT SSDM COLLEGE


13

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 −

Then, take interval of 1 and this gap generates two sub-lists –


{14, 27, 35, 42}, {19, 10, 33, 44}

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.

Following is the step-by-step depiction −

Department of CS&IT SSDM COLLEGE


14

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

while interval > 0 do:


for outer = interval; outer < A.length; outer ++ do:
/* select value to be inserted */
valueToInsert = A[outer] inner =
outer;
/*shift element towards right*/
while inner > interval -1 && A[inner - interval] >= valueToInsert do: A[inner] =
A[inner - interval]
inner = inner - interval
end while
/* insert the number at hole position */ A[inner]
= valueToInsert
Department of CS&IT SSDM COLLEGE
15
end for
/* calculate interval*/ interval
= (interval -1) /3; 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.

Heap sort basically recursively performs two main operations -

o Build a heap H, using the elements of array.


o Repeatedly delete the root element of the heap formed in 1st phase.

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.

What is heap sort?

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)

Department of CS&IT SSDM COLLEGE


16
MaxHeapify(arr,i)
L = left(i)
R = right(i)
if L ? heap_size[arr] and arr[L] > arr[i]
largest = L
else
largest = i
if R ? heap_size[arr] and arr[R] > arr[largest]
largest = R
if largest != i
swap arr[i] with arr[largest]
MaxHeapify(arr,largest)
End
Working of Heap sort Algorithm
Now, let's see the working of the Heapsort Algorithm.

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 -

Department of CS&IT SSDM COLLEGE


18

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.

After completion of sorting, the array elements are -

Now, the array is completely sorted.

Heap sort complexity

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

Best Case O(n logn)

Average Case O(n log n)

Worst Case O(n log n)

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

Department of CS&IT SSDM COLLEGE


20
is O(n log n).
o Worst Case Complexity - It occurs when the array elements are required to be sorted in reverse
order. That means suppose you have to sort the array elements in ascending order, but its elements
are in descending order. The worst-case time complexity of heap sort is O(n log n).

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

o The space complexity of Heap sort is O(1).

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.

The new array should look like this −

Next we compare 33 and 35. We find that both are in already sorted positions.

Department of CS&IT SSDM COLLEGE


21
Then we move to the next two values, 35 and 10.

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.

Now we should look into some practical aspects of bubble sort.


Algorithm
We assume list is an array of n elements. We further assume that swap function swaps the values of the
given array elements.
begin BubbleSort(list)

for all elements of list


if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for

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 −

procedure bubbleSort( list : array of items )

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

/*if no number was swapped that means


array is sorted now, break the loop.*/

if(not swapped) then


break
end if

end for

end procedure return list

Department of CS&IT SSDM COLLEGE

You might also like