0% found this document useful (0 votes)
79 views30 pages

IT T33-Data Structures: SMVEC - Department of Information Technology 1

The document discusses various sorting algorithms and their time and space complexities. It describes bubble sort, selection sort, insertion sort, shell sort, merge sort, quick sort, heap sort, and radix sort. It provides pseudocode for bubble sort and analyzes its time complexity as O(n^2) in all cases. Examples are given to illustrate the bubble sorting process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views30 pages

IT T33-Data Structures: SMVEC - Department of Information Technology 1

The document discusses various sorting algorithms and their time and space complexities. It describes bubble sort, selection sort, insertion sort, shell sort, merge sort, quick sort, heap sort, and radix sort. It provides pseudocode for bubble sort and analyzes its time complexity as O(n^2) in all cases. Examples are given to illustrate the bubble sorting process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

IT T33- Data Structures

Unit IV

Sorting: O notation – efficiency of sorting – bubble sort – quick sort – selection sort – heap sort –
insertion sort – shell sort – merge sort – radix sort.

Objective:

 To study and analyze time complexity of various sorting algorithms


 To design, implement, and analyze the various sorting algorithms

Outcome:
 Able to differentiate the different sorting algorithms
 Know the suitable sorting method for specific application

4.1 Efficiency of an algorithm or performance analysis of an algorithm

Algorithm
It is a step by step process to accomplish a particular task.
Efficiency of an algorithm

Efficiency of an algorithm is one of the properties of an algorithm based on the resources


used by the algorithm.The most commonly used resources are time and space.

Space Complexity- It is the amount of memory space (main memory) required to complete the
execution of the algorithm.

Time Complexity - It is the amount of CPU time required to complete the execution of the
algorithm.

Computing Space Complexity The space required for an algorithm is calculated based on fixed
and variable part of the program.

Fixed part:

 Space required for code


 Simple variable
 Static compound variable
 Constants

Variable part:

 Dynamic compound variable


 Recursive procedure

The space required to execute any algorithm P is denoted by,

S[P] = C + Sp(instance characteristics)

Where C – is constant i.e. fixed part, can be ignored.

Sp – Variable part.
SMVEC- Department of Information Technology 1
IT T33- Data Structures
Eg 1:

Algorithm ComputeABC(a,b,c)
{
Sum = a+b+c;
}
S(ComputeABC) = C + Sp(instance characteristics)
= C + Sp(0)
=0

Eg 2:
Algorithm Sum(a,n)
{
Sum = 0;
for i=0 to n
{
Sum = Sum + a[i];
}
return Sum;
}

Instance Code a n Sum i Total


1(n=5) C 10 2 2 2 10+6
2(n=10) C 20 2 2 2 20+6
3(n=100) C 200 2 2 2 200+6
4(n=1000) C 2000 2 2 2 2000+6
5(n=10000) C 20000 2 2 2 20000+6

S(Sum) = C + Sp(n)
=C+n+3
=n+3
= O (n)

Computing Time Complexity

Time complexity of any program is the sum of compile time and run time.

The compile time remains constant for all instance of an algorithm (assume that a compiled
program can be run many times without recompilation).So compile time can be ignored.

T(p) = tp(instance characteristics)

There are 2 methods to calculate time complexity

 Count method
 Step table method

SMVEC- Department of Information Technology 2


IT T33- Data Structures
Program Step
It is defined as syntactically and semantically meaningful segment of a program that has
execution time which is independent of the instance characteristics of the problem. Number of
program steps assigned for any program statement depends on kind of statement.

 Comment line has zero program step.


 Assignment statement which does not involve any function call is assigned one program step.
 Control part of a program such as while, for, do has one program step.

Count method
Count is a global variable with initial value „0‟. Whenever the statement in the algorithm is
executed, the count is incremented by the program step assigned for that statement.

Eg:
Algorithm Sum(a,n)
{
Sum = 0; -1
for i=0 to n - n+1
{
Sum = Sum + a[i]; -n
}
return Sum; -1
}

t(Sum) = 1+ n + 1 + n + 1
= 2n + 3
= O (n)

Step table method


The Step table list out the following fields
(i) Statement in algorithm to be analysed.
(ii) No. of program steps per execution of statement
(iii) Total no. of times (frequency) each statement is executed.
(iv) Total no. of program steps contributed for each statement.

Statement No. of program steps Frequency Total no. of program


per execution steps
Algorithm Sum(a,n) - - -
{ - - -
Sum = 0; 1 1 1
for(i=0 to n) 1 n+1 n+1
{ - - -
Sum = Sum+a[i]; 1 n n
} - - -
return Sum 1 1 1
} - - -
2n+3

SMVEC- Department of Information Technology 3


IT T33- Data Structures
Asymptotic notation: (o, Ω, ө)

Asymptotic Notation is used to represent the efficiency of an algorithm.

O – Big ‘oh’ Notation

f(n) = O(g(n)), iff there exist a positive constant c,n0 such the f(n) <= c*g(n) for all n>= n0.
Eg: f(n) = 2n + 3
f(n) = O(n).

Eg: f(n) = 10n3+n2+9n+10


f(n) = O(n3).

Ω - Omega Notation

f(n) = Ω(g(n)), iff there exist a positive constant c,n0 such the f(n) >= c*g(n) for all n>= n0.

Ө - Omega Notation

f(n) = Ө(g(n)), iff there exist a positive constant c1,c2,n0 such the
c1*g(n)<= f(n) <= c2*g(n).
Relation between time complexity in Big „oh‟ notation :
O(1) < O(log n) < O(n) < O(n log n) < O(n2) < O(n3) < O(2n).

4.2 Sorting
Sorting a sequence of elements means rearranging them in either ascending or descending
order depending upon the relationship among the data items present in the group.

Factors to be considered while choosing the sorting techniques


 Programming time of a sorting technique.
 Execution time of a sorting technique.
 No. of comparison required for sorting the list.
 Main memory or auxiliary memory space, needed for the sorting techniques.

Sorting algorithm

A sorting algorithm is defined as an algorithm that puts elements of a list in a certain order that can
either be numerical order, lexicographical order or any user-defined order.

Types of Sorting:

 Internal sorting - which deals with sorting the data stored in computer‟s memory
 External sorting - This deals with sorting the data stored in files.

Application of Sorting
Speeding up the search process is the most important application of sorting.

SMVEC- Department of Information Technology 4


IT T33- Data Structures
Various Sorting techniques
 Bubble Sort
 Selection Sort
 Insertion Sort
 Shell Sort
 Merge Sort
 Quick Sort
 Radix (Bucket) Sort
 Heap Sort

4.3 Bubble sort


It is the easiest and most widely used sorting technique. This technique is also referred to
as sinking sort. The idea in this technique is to repeatedly move the smallest element into the
lowest index position or repeatedly move the largest element into highest index position in the
list.

Algorithm BubbleSort(a,n)
{
for(i=1 to n)
{
for(j=1 to n-i)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
}

Analysis (Efficiency)

Best case: O (n2)


Average case: O (n2)
Worst case: O (n2)

Advantages of Bubble Sort


 Popular & easy to implement.
 No additional temporary storage is required.
 It requires only few lines of code.

Disadvantage of Bubble Sort


 Amount of time increases with the no. of elements get increased.
 Sorting becomes difficult when element size increases

SMVEC- Department of Information Technology 5


IT T33- Data Structures

Example : Sort the elements a[1:5] = { 56 , 91 , 35, 72, 43}

i j a[1] a[2] a[3] a[4] a[5] a[j]>a[j+1] operation


1 1 56 91 35 72 43 56>91 (f) No operation
2 56 91 35 72 43 91>35 (t) Swap 91 & 35
3 56 35 91 72 43 91>72 (t) Swap 91 & 72
4 56 35 72 91 43 91>43 (t) Swap 91 & 43
56 35 72 43 91
2 1 56 35 72 43 91 56>35 (t) Swap 56 & 35
2 35 56 72 43 91 56>72 (f) No operation
3 35 56 72 43 91 72>43 (t) Swap 72 & 43
35 56 43 72 91
3 1 35 56 43 72 91 35>56 (f) No operation
2 35 56 43 72 91 56>43 (t) Swap 56 & 43
35 43 56 72 91
4 1 35 43 56 72 91 35>43 (f) No operation
35 43 56 72 91
35 43 56 72 91 Sorted order

4.4 Quick sort algorithm

Quick sort is otherwise called as partition exchange sorting.


Basic Concept: divide and conquer-
Given an array of n elements (e.g., integers):
• If array only contains one element, return
• Else
– Pick one element to use as pivot.
– Partition elements into two sub-arrays:
• Elements less than or equal to pivot
• Elements greater than pivot
 Quicksort two sub-arrays, Recursively apply Quicksort to the subgroups
 Return results
Different ways to select a pivot element:
 First element
 Last element
 Use that median as the pivot.
 Random element

Algorithm QuickSort(low,high)
{
if(low<high)then
{
j=partition(a,low,high+1);
QuickSort(low,j-1);
QuickSort(j+1,high);
}
}

SMVEC- Department of Information Technology 6


IT T33- Data Structures

Algorithm partition(a,m,p)
{
v=a[m];
i=m;
j=p;
repeat
{
repeat
{
i=i+1
}until a[i]>=v;
repeat
{
j=j-1
}until a[j]<=v;
if(i<j)then
Interchange(a,i,j);
}until(i>=j);
a[m]=a[j];
a[j]=v;
return j;
}

Algorithm Interchange(a,i,j)
{ temp=a[i];
a[i]=a[j];
a[j]=temp;
}

a[1] a[2] a[3] a[4] a[5] i j i<j i>=j


50 20 60 10 ∞ 3 4 3<4 (t)
i)Swap a[i] &a[j]
ii)repeat
50 20 10 60 ∞ 4 3 4<3 (f) 4>=3 (t)
i)Swap a[j] & a[m]
ii) return 3
10 20 50 60 ∞ 2 1 2<1 (f) 2>=1 (t)
i)Swap a[j] & a[m]
ii) return 1
SMVEC- Department of Information Technology 7
IT T33- Data Structures
Analysis (Efficiency)

Best case: O (n log n)


Average case: O (n log n)
Worst case: O (n2)

Step 1, select a pivot(it is arbitrary)We will select the


first element

Step 2,

groups:

group will have elements greater that the


pivot.

Step 3,

increment left index.

decrement right.
Exchange when you find elements that belong to the
other group.
Step 4:

Step 5:

right marker

Step 6:
RIGHT group.

SMVEC- Department of Information Technology 8


IT T33- Data Structures

Step 7: Step 8:
pass each other, we are
done with the partition task.

Step 9:
Apply Quicksort to the LEFT and RIGHT groups, recursively.

4.5 Selection Sort


 Keep finding the “next” smallest item
 Place the item found in the appropriate position

Algorithm SelectionSort(a,n)
{
for(i=1 to n)
{
min = i;
for j=i+1 to n
{
if(a[j]<a[min])
{
min = j;
}
}
if(i!=min)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
}

SMVEC- Department of Information Technology 9


IT T33- Data Structures

Analysis (Efficiency)

Best case: O (n2)


Average case: O (n2)
Worst case: O (n2)

Advantages of Selection Sort

 Easy of understand and implement


 Performs well for small data set

Disadvantage of Selection Sort


 Poor efficiency when dealing with higher no. of elements
 Needs n2 sequential no. of steps for sorting n elements.

Example: Sort the elements a[1:5] = { 56 , 91 , 35, 72, 48}

i J min a[1] a[2] a[3] a[4] a[5] a[j]<a[min] Operation


1 2 1 56 91 35 72 48 91<56 (f) No operation
3 1 56 91 35 72 48 35<56 (t) min = j (=3)
4 3 56 91 35 72 48 72<35 (f) No operation
5 3 56 91 35 72 48 48<35 (f) No operation
3 56 91 35 72 48 End of pass Swap min and i
35 91 56 72 48
2 3 2 35 91 56 72 48 56<91 (t) min = j (=3)
4 3 35 91 56 72 48 72<56 (f) No operation
5 3 35 91 56 72 48 48<56 (t) min = j (=5)
5 35 91 56 72 48 End of pass Swap min and i
35 48 56 72 91
3 4 3 35 48 56 72 91 72<56 (f) No operation
5 3 35 48 56 72 91 91<56 (f) No operation
3 35 48 56 72 91 End of pass Swap min and i
35 48 56 72 91
4 5 4 35 48 56 72 91 91<72 (f) No operation
4 35 48 56 72 91 End of pass Swap min and i
35 48 56 72 91
5 - - 35 48 56 72 91 Sorted
order

SMVEC- Department of Information Technology 10


IT T33- Data Structures
4.6 Heap sort

 Heap sort is an efficient sorting algorithm


 The (binary) heap data structure is an array object that can be viewed as a complete binary
tree
 Heap sort is an in-place algorithm i.e. does not use any extra space
 This method is based on a data structure called Heap.
 Heap data structure can also be used as a priority queue.

Complete Binary tree


A Binary tree with n nodes of depth k is complete if its nodes correspond to the nodes
which are numbered from 1 to n in a full binary tree of depth k (nodes are numbered starting from
nodes at level 1 then go to nodes at level 2 and so on… from left to right).

Eg:

8 12

3 1 12

Heap:
There are two types of Heap
(i) Max-Heap
(ii) Min-Heap

Heap Property:
 Max-Heap
 A Max-heap is a complete binary tree with a property that each node is at least as
large as the values at its children (if they exist)

 Min-Heap
 A Min-heap is a complete binary tree with a property that each node is at least as
small as the values at its children (if they exist).

4.6.1 Heap Sort Algorithm


Algorithm HeapSort(a,n)
{
Heapify(a,n);
for i = n to 0,step-1 do
{
t=a[i];
a[i]=a[1];
a[1]=t;
adjust(a,1,i-1);
}
}

SMVEC- Department of Information Technology 11


IT T33- Data Structures

Algorithm Heapify(a,n)
{
for i =n/2 to 1,step-1 do
{
adjust(a,i,n);
}
}

Algorithm adjust(a,i,n)
{
j=2i;
item=a[i];
while(j<=n)do
{
if((j<n)and(a[j]<a[j+1]))
{
break;
}
a[j/2]=a[j];
j=2j;
}
a[j/2]=item;
}

Example: Sort the list a[1:6] = {5, 7, 6, 20, 10, 25} using heap sort.

Step 1: Construct a complete binary tree for the given set of elements

a[1:6] = {5, 7, 6, 20, 10, 25}

7 6

20 10 25

Step 2: Convert the complete binary tree into max-heap using adjust.

SMVEC- Department of Information Technology 12


IT T33- Data Structures

Adjust Adjust

Cad Cad

Adjust

ad
Adjust
Adjust
Ca Ca

Adjust
ad

 Since all the non-leaf node(root) in the complete binary tree satisfy the max-heap property, it
becomes max-heap and the first largest element in the list is moved into the root.

 Interchange the element in the first root with the element in the nth position node

Interchange
Cadad

Since, the nth element is in sorted order, it is not considered for the remaining sorting
process. Therefore the node is deleted from the tree.

SMVEC- Department of Information Technology 13


IT T33- Data Structures

Step 3: Construct a complete binary tree for the element 1 to n-1 and convert it to max-heap

Adjust

Cad

Since all root in the complete binary tree satisfy the heap property, it becomes max-heap
and second largest element is moved into the first root.
Interchange the element in the first root with the element in the (n-1)th position node

Interchange
Cadad

Since, the (n-1)th element is in sorted order, it is not considered for the remaining sorting process.
Therefore the node is deleted from the tree.

SMVEC- Department of Information Technology 14


IT T33- Data Structures
Step 4: Construct a complete binary tree for the element 1 to n-2 and convert it to max-heap

Adjust

Cad

Since all root in the complete binary tree satisfy the heap property, it becomes max-heap
and third largest element is moved into the first root.
Interchange the element in the first root with the element in the (n-2)th position node

Interchange
Cadad

Since, the (n-2)th element is in sorted order, it is not considered for the remaining sorting process.
Therefore the node is deleted from the tree.

Step 5: Construct a complete binary tree for the element 1 to n-3 and convert it to max-heap

Adjust

Cad

Since all root in the complete binary tree satisfy the heap property, it becomes max-heap
and fourth largest element is moved into the first root.
Interchange the element in the first root with the element in the (n-3)th position node

SMVEC- Department of Information Technology 15


IT T33- Data Structures

Interchange
Cadad

Since, the (n-3)th element is in sorted order, it is not considered for the remaining sorting process.
Therefore the node is deleted from the tree.

Step 6: Construct a complete binary tree for the element 1 to n-4 and convert it to max-heap

Since all root in the complete binary tree satisfy the heap property, it becomes max-heap and
fourth largest element is moved into the first root.
Interchange the element in the first root with the element in the (n-4)th position node

Interchange
Cadad

Since, the (n-4)th element is in sorted order, it is not considered for the remaining sorting process.
Therefore the node is deleted from the tree.

Finally the heap consists of single element i.e. the nth largest element which is placed in 1st position
i.e. the (n-5)th position of array

The sorted list a[1:6] = {5, 6, 7, 10, 20, 25}.

Analysis (Efficiency)

Best case: O(n log n)


Average case: O(n log n)
Worst case: O(n log n)

SMVEC- Department of Information Technology 16


IT T33- Data Structures

4.7 Insertion sort : The insertion sort algorithm is performed using following steps...

Step 1: Assume that first element in the list is in sorted portion of the list and remaining all
elements are in unsorted portion.
Step 2: Consider first element from the unsorted list and insert that element into the sorted list in
order specified.
Step 3: Repeat the above process until all the elements from the unsorted list are moved into the
sorted list.
Algorithm InsertionSort(a,n)
{
for i=2 to n
{
key = a[i];
j=i-1;
while (j>0) and (key < a[j])
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=key;
}
}
Example: Sort the elements a[1:5] = {56, 91, 35, 72, 48}
i j a[1] a[2] a[3] a[4] a[5] key< a[i] Operation
{a[j+1]=a[j]…(t)
a[j+1]=key…(f) &
(no more previous
element)
2 1 56 91 35 72 48 91<56 (f) a[2] = key (=91)
56 91 35 72 48 End of pass -
3 2 56 91 35 72 48 35<91 (t) a[3] = a[2] (=91)
1 56 91 91 72 48 35<56 (t) a[2] = a[1] (=56)
0 56 56 91 72 48 No more a[1] = key (=35)
previous
element
35 56 91 72 48 End of pass -
4 3 35 56 91 72 48 72<91 (t) a[4] = a[3] (=91)
2 35 56 91 91 48 72<56 (f) a[3] = key (=72)
35 56 72 91 48 End of pass -
5 4 35 56 72 91 48 48<91 (t) a[5] = a[4] (=91)
3 35 56 72 91 91 48<72 (t) a[4] = a[3] (=72)
2 35 56 72 72 91 48<56 (t) a[3] = a[2] (=56)
1 35 56 56 72 91 48<35 (f) a[2] = key (=48)
35 48 56 72 91 End of pass -
6 - 35 48 56 72 91 - Sorted order
Analysis (Efficiency)

Best case: O(n)


Average case: O(n)
Worst case: O(n2)

SMVEC- Department of Information Technology 17


IT T33- Data Structures
4.8 Shell sort (or) Diminishing increment sort

 It is improvement of insertion sort.


 Shell sort works by comparing elements that are distant rather than adjacent elements in an
array or list where adjacent elements are compared.
 Shell sort is also known as diminishing increment sort.
 The distance between comparisons decreases as the sorting algorithm runs until the last
phase in which adjacent elements are compared.
 The shell sort compares elements that are a certain distance away (d positions away) from
each other and it compares these elements repeatedly.
 The comparison is made as by using the concept of dividing the array as n/2 , and compare
the elements, compare until 1.
 The comparison model makes the sorting process of the shell sort very efficient.
Algorithm ShellSort(a,n)
{
dist=n/2;
do
{
for(i=1 to n)
{
j = i;
key = a[i+dist];
while ((j>0)and(key<a[j]))
{
a[j+dist]=a[j];
j=j-dist;
}
a[j+dist]=key;
}
dist=dist/2;
}while(dist);
}

Analysis (Efficiency)
Best case: O (n log n)
Average case: O (n2)
Worst case: O (n2)

Example: Sort the list of elements a[1:6] = {70, 50, 40, 30, 35, 25}.
dist= n/2 = 6/2 = 3;
SMVEC- Department of Information Technology 18
IT T33- Data Structures
dist I j a[1] a[2] a[3] a[4] a[5] a[6] j>0 key< a[i] No operation
3 1 1 70 50 40 30 35 25 1>0(t) 30<70 (t) a[j+dist]=a[j]
a[4]=70
j=j-dist (j= -2)
-2 70 50 40 70 35 25 -2>0(f) - a[j+dist]=key
a[1]=30
- 30 50 40 70 35 25 - - i=2;j=2;key=35;
2 2 30 50 40 70 35 25 2>0(t) 35<50 (t) a[j+dist]=a[j]
a[5]=50
j=j-dist (j= -1)
-1 30 50 40 70 50 25 -1>0(f) - a[j+dist]=key
a[2]=35
- 30 35 40 70 50 25 - i=3;j=3;key=25;
3 3 30 35 40 70 50 25 3>0(t) 25<40 (t) a[j+dist]=a[j]
a[6]=40
j=j-dist (j= 0)
0 30 35 40 70 50 40 0>0 (f) - a[j+dist]=key
a[3]=25
- 30 35 25 70 50 40 - - i=4;
dist=dist/2 (=1)
1 1 1 30 35 25 70 50 40 1>0 (t) 35<30 (f) a[j+dist]=key
a[2]=35
i=2;j=2;key=25
2 2 30 35 25 70 50 40 2>0 (t) 25<35 (t) a[j+dist]=a[j]
a[3]=35
j=j-dist (j=1)
1 30 35 35 70 50 40 1>0 (t) 25<30 (t) a[j+dist]=a[j]
a[2]=30
j=j-dist (j=0)
0 30 30 35 70 50 40 0>0 (f) - a[j+dist]=key
a[1]=25
- 25 30 35 70 50 40 - - i=3;j=3;key=70
3 3 25 30 35 70 50 40 3>0 (t) 70<35 (f) a[j+dist]=key
a[4]=70
i=4;j=4;key=50
4 4 25 30 35 70 50 40 4>0 (t) 50<70 (t) a[j+dist]=a[j]
a[5]=50
j=j-dist (j=3)
3 25 30 35 70 70 40 3>0 (t) 50<35 (f) a[j+dist]=key
a[4]=50
- 25 30 35 50 70 40 - - i=5;j=5;key=40
5 5 25 30 35 50 70 40 5>0 (t) 40<70 (t) a[j+dist]=a[j]
a[6]=70
j=j-dist (j=4)
4 25 30 35 50 70 70 4>0 (t) 40<50 (t) a[j+dist]=a[j]
a[5]=50
j=j-dist (j=3)
3 25 30 35 50 50 70 3>0 (t) 40<35 (f) a[j+dist]=key
a[4]=40
- 25 30 35 40 50 70 - - i=6;
dist=dist/2 (=0)
0 - - 20 30 35 40 50 70 - - Sorted Order

SMVEC- Department of Information Technology 19


IT T33- Data Structures
4.9 Merge sort
Divide-and-Conquer
 It is Recursive in structure
 Divide the problem into several smaller sub problems that are similar to the original but
smaller in size.
 Conquer the sub-problems by solving them recursively. If they are small enough, just solve
them in a straightforward manner.
 Combine the solutions to create a solution to the original problem

Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each
Conquer: Sort the two subsequences recursively using merge sort.
Combine: Merge the two sorted subsequences to produce the sorted answer.
If n=1 terminate (every one-element list is already sorted)
Algorithm MergeSort(low,high) Algorithm Merge(low,mid,high)
{ {
if(low<high) h=low;
{ i=low;
mid=(low+high)/2; j=mid+1;
MergeSort(low,mid);
MergeSort(mid+1,high); while((h<=mid)&&(j<=high))
Merge(low,mid,high); {
}} if(a[h]<a[j])then
{
b[i]=a[h];
h=h+1;
}
else
{
b[i]=a[j];
j=j+1;
}
i=i+1;
}
if(h>mid)then
{
for(k=j to high)
{
b[i]=a[k];
i=i+1;
} }
else
{ for(k=h to mid)
{
b[i]=a[k];
i=i+1;
} }
for(k=low to high)
{
a[k]=b[k];
}
}

SMVEC- Department of Information Technology 20


IT T33- Data Structures

Example: Sort the elements a[0:6]={38,27,43,3,9,82,10}

Tree representation of Merge sort algorithm

Trace out of Merge Algorithm

3 27 38 43 9 10 82

i = 0; h = 0; j = 4;

Step 1:
 h<=mid && j <=high
0<=3(t) && 4<=6(t)
 a[h]<a[j]
3<9 (t)
 b[i]=a[h]
b[0]=3
 h=h+1(=1)
 i=i+1(=1)
3

Step 2:
 h<=mid && j <=high
1<=3(t) && 4<=6(t)
 a[h]<a[j]
27<9 (f)
 b[i]=a[j]
b[1]=9
 j=j+1(=5)
 i=i+1(=2)
3 9

SMVEC- Department of Information Technology 21


IT T33- Data Structures
Step 3:
 h<=mid && j <=high
1<=3(t) && 5<=6(t)
 a[h]<a[j]
27<10 (f)
 b[i]=a[j]
b[2]=10
 j=j+1(=6)
 i=i+1(=3)
3 9 10
Step 4:
 h<=mid && j <=high
1<=3(t) && 6<=6(t)
 a[h]<a[j]
27< 82(t)
 b[i]=a[h]
b[3]=27
 h=h+1(=2)
 i=i+1(=4)
3 9 10 27
Step 5:
 h<=mid && j <=high
2<=3(t) && 6<=6(t)
 a[h]<a[j]
38< 82(t)
 b[i]=a[h]
b[4]=38
 h=h+1(=3)
 i=i+1(=5)
3 9 10 27 38
Step 6:
 h<=mid && j <=high
3<=3(t) && 6<=6(t)
 a[h]<a[j]
43< 82(t)
 b[i]=a[h]
b[5]=43
 h=h+1(=4)
 i=i+1(=6)
3 9 10 27 38 43
Step 7: Analysis (Efficiency)
 h<=mid && j <=high
4<=3(f) && 6<=6(t) Best case: O (n log n)
 h>mid Average case: O (n log n)
4>3 (t) Worst case: O (n log n)
 k=6
 b[i]=a[k]
 b[6]=82
 i=i+1 (=7)
3 9 10 27 38 43 82

SMVEC- Department of Information Technology 22


IT T33- Data Structures

4.10 Radix or Bucket sort


 It is generalization of Bucket Sort for large integer keys, Radix sort is a multiple pass
distribution sort. Radix = “The base of a number system”It distributes each item to a bucket
according to part of the item's key.
Basic Idea of radix sort:
 Bucket Sort on each digit least significant digit to most significant digit (lsd to msd)
 Set number of buckets B.B= radix. i.e the base of the number system ,if decimal
number it is 10. Insert the numbers into the buckets.
Algorithm RadixSort(a,n)
{
// To find th maximum element
max=a[1];
for i=2 to n
{
if(a[i]>max)
{
max=a[i];
}
}
// To find the maximum no.of digits in the given element
digit = 0;
while(max>0)
{
digit=digit+1;
max=max/10;
}
// To sort the element using modulo division
divisor = 1;
for(p=1 to digit)
{
for(i=0 to 9)
{
bsize[i]=0;
}
for(i=1 to n)

{
r=((a[i]/divisor)%10);
bucket[r][bsize[r]]=a[i];
bsize[r]++;
}
i=1;
for(k=0 to i)
{
for(j=o to bsize[k])
{

a[i]=bucket[k][j];
i=i+1;
} } } }

SMVEC- Department of Information Technology 23


IT T33- Data Structures

Example: Sort the list a[1:8]={70, 80, 5, 268, 322, 32, 100, 45} using radix sort.

Step 1: Find the maximum element in the given list of elements.

i max a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[i]>max
1 70 70 80 5 268 322 32 100 45 80>70 (t)
i++;
max=80;
2 80 70 80 5 268 322 32 100 45 5>80 (f)
i++;
3 80 70 80 5 268 322 32 100 45 268>80 (t)
i++;
max=268
4 268 70 80 5 268 322 32 100 45 322>268 (t)
i++;
max=322
5 322 70 80 5 268 322 32 100 45 32>322 (f)
i++
6 322 70 80 5 268 322 32 100 45 100>322 (f)
i++
7 322 70 80 5 268 322 32 100 45 45>322 (f)

Step 2: Find the no. of digits in the maximum element.

Here max=322.

Digit max>0 max Operation


0 322>0 (t) 32 digit++;
max=max/10;
1 32>0 (t) 3 digit++;
max=max/10;
2 3>0 (t) 0 digit++;
max=max/10;
3 0>0 (f) 0 -

Here, the no. of digits in the max .element is 3.

Since, digit=3

Step 3:
Sort the list of elements based on each least significant digit.
Change all the elements in the list into 3-digit number by adding required no of 0‟s to its left.

a[1:8] = {070, 080, 005, 268, 322, 032, 100, 045}


Pass 1

1st LSD = ((element/1)%10)


Based on the first least significant digit in the list, the elements in the list are placed in the
corresponding buckets

SMVEC- Department of Information Technology 24


IT T33- Data Structures

100
080 032 045
070 322 005 268
0 1 2 3 4 5 6 7 8 9

After the Pass 1, move the elements from bucket into array using FIFO

a[1:8] = {070,080,100,322,032,005,045,268}

Pass 2

2nd LSD = ((element/10)%10)


Based on the second least significant digit in the list, the elements in the list are placed in
the corresponding buckets

005
100 322 032 045 268 070 080
0 1 2 3 4 5 6 7 8 9

After the Pass 2, move the elements from bucket into array using FIFO

a[1:8] = {100,005,322,032,045,268,070,080}

Pass 3

3rd LSD = ((element/100)%10)


Based on the third least significant digit in the list, the elements in the list are placed in the
corresponding buckets

080
070
045
032
005 100 268 332
0 1 2 3 4 5 6 7 8 9

After the Pass 3, move the elements from bucket into array using FIFO to get sorted order of the
array

a[1:8] = {005,032,045,070,080,100,268,332}.

Analysis (Efficiency)

Best case: O (kn)


Average case: O (kn)
Worst case: O (kn)

SMVEC- Department of Information Technology 25


IT T33- Data Structures

Two Marks
1. What is an Algorithm?
An algorithm is clearly specified set of simple instructions to be followed to
solve a problem. The algorithm forms a base for program.

2. What are the properties of an Algorithm?


Takes zero or more inputs
Results in one or more outputs
All operations are carried out in a finite time
Efficient and flexible
Should be concise and compact to facilitate verification of their correctness.
3. Define Program?
It is an instruction and it is written according to the instructions, which is given in the algorithm.

4. What is Complexity analysis?


It is the analysis of the amount of memory and time an algorithm requires to
completion.There are two types of Complexity
Space Complexity
Time Complexity
5. Explain the performance analysis of the algorithm?
The analysis of the performance of an algorithm based on specification is
called performance analysis. It is loosely divided into
i.Priori estimates
ii.Posterior Testing
6. Explain Space complexity?
Space complexity of an algorithm is the amount of memory it needs to run to completion.
7. Explain Time complexity?
Time complexity is the amount of computer time an algorithm requires to run to
completion.
8. List out the components that are used for space complexity?
a. Instruction Space
b. Environment Stack
c. Data Space.

SMVEC- Department of Information Technology 26


IT T33- Data Structures

9. What do asymptotic notation means?


Asymptotic notations are terminology that is introduced to enable us to make meaningful
statements about the time and space complexity of an algorithm.
The different notations are
Big – Oh notation
Omega notation
Theta notation.
10. Define Efficiency of an algorithm?
It denotes the rate at which an algorithm solves a problem of size n. It is measured by the
amount of resources it uses, the time and the space.

11. Define Worst case of an algorithm?


It is the longest time that an algorithm will use over all instances of size n for a given
problem to produce the result.

12. Define Best case of an algorithm?


It is the shortest time that an algorithm will use over all instances of size n for a given
problem to produce the result.
13. Define average case an algorithm?
It is the average time that an algorithm will use over all instances of size n for a given
problem to produce the result.
14. Define Divide and Conquer algorithm?
Divide and Conquer algorithm is based on dividing the problem to be solved into several,
smaller sub instances, solving them independently and then combining the sub instances solutions
so as to yield a solution for the original instance.

15. Mention some application of Divide and Conquer algorithm?


a. Quick Sort
b. Merge Sort
c. Binary search

16. Define dynamic programming algorithm?


Dynamic programming algorithm is a general class of algorithms which solve problems by
solving smaller versions of the problem, saving the solutions to the small problems and then
combining them to solve the larger problems.

SMVEC- Department of Information Technology 27


IT T33- Data Structures
17. Mention application of dynamic programming algorithm?
a Efficient Fibonocci number computation
b. Chained matrix multiplication.
c. Longest common subsequence problem
18. State the various steps in algorithm?
i. Devising the algorithm
ii. Validating the algorithm
iii. Expressing the algorithm
iv. Determination of complexity of the algorithm

19. Define algorithm paradigms space of an algorithm?


Algorithmic paradigms are defined as the general approach to design and
construct efficient solutions to problems.

20. Mention the various spaces utilized by a program?


i. A fixed amount of memory occupied by the space for the program code and
space occupied by the variables used in the program.
ii. A variable amount of memory occupied by the variable whose size is dependent on the
problem being solved. This space increases or decreases depending upon whether the program
uses iterative or recursive procedures

21. What is meant by sorting?

Ordering the data in an increasing or decreasing fashion according to some relationship


among the data item is called sorting.

22. What are the two main classifications of sorting based on the source of data?
 Internal sorting
 External sorting
23. What is meant by external sorting?
External sorting is a process of sorting in which large blocks of data stored in storage
devices are moved to the main memory and then sorted.
24. What is meant by internal sorting?

Internal sorting is a process of sorting the data in the main memory.

25. What are the various factors to be considered in deciding a sorting algorithm?

 Programming time
 Execution time of the program
 Memory needed for program environment

SMVEC- Department of Information Technology 28


IT T33- Data Structures

26. What is the main idea behind insertion sort? When can we use insertion sort?

The main idea of insertion sort is to insert in the i th pass the i th element in A (1) A (2)...A
(i) in its rightful place.Insertion sort is useful only for small files or very nearly sorted files.

27. What is the main idea behind insertion sort?

The main idea behind the selection sort is to find the smallest element among in A (I) A
(J+1)...A (n) and then interchange it with a (J). This process is then repeated for each value of J.

28. What is the basic of shell sort (or Diminishing increment sort).?(November 2015)

Instead of sorting the entire array at once, it is first divide the array into smaller segments,
which are then separately sorted using the insertion sort.

29. What is the purpose of quick sort and list its advantages? (April 2015)

The purpose of the quick sort is to move a data item in the correct direction, just enough for
to reach its final place in the array.

Advantage :Quick sort reduces unnecessary swaps and moves an item to a greater distance, in
one move.

30. What is the average efficiency of heap sort? .

The average efficiency of heap sort is 0 (n(log2 n)) where, n is the number of elements sorted.

31. What are the properties involved in heap?

1. structure property
2. heap order property

32. Define max and min heap?

A heap in which the parent has a larger key than the child's is called a max heap.

A heap in which the parent has a smaller key than the child's is called a min heap.

SMVEC- Department of Information Technology 29


IT T33- Data Structures
Assignment Questions

1. Sort the sequence 3, 1, 4, 1, 5, 9, 2, 6, 5 using insertion sort. What is the running time of
insertion sort if all elements are equal?
2. Show how heapsort processes the input 142, 543, 123, 65, 453, 879, 572, 434, 111, 242,
811, 102. What is the running time of heapsort for presorted input?
3. Sort 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5 using quicksort with median-of-three partitioning. Determine
the running time of quicksort for a. sorted input b. reverse-ordered input c. random input.
4. Show the result of running Shellsort on the input 9, 8, 7, 6, 5, 4, 3, 2, 1
5. Sort the following elements using radix sort 12,98,34,67,54,23,56,45

SMVEC- Department of Information Technology 30

You might also like