IT T33-Data Structures: SMVEC - Department of Information Technology 1
IT T33-Data Structures: SMVEC - Department of Information Technology 1
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:
Outcome:
Able to differentiate the different sorting algorithms
Know the suitable sorting method for specific application
Algorithm
It is a step by step process to accomplish a particular task.
Efficiency of an algorithm
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:
Variable part:
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;
}
S(Sum) = C + Sp(n)
=C+n+3
=n+3
= O (n)
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.
Count method
Step table method
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)
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).
Ω - 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.
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.
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)
Algorithm QuickSort(low,high)
{
if(low<high)then
{
j=partition(a,low,high+1);
QuickSort(low,j-1);
QuickSort(j+1,high);
}
}
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;
}
Step 2,
groups:
Step 3,
decrement right.
Exchange when you find elements that belong to the
other group.
Step 4:
Step 5:
right marker
Step 6:
RIGHT group.
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.
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;
}
}
}
Analysis (Efficiency)
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).
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
7 6
20 10 25
Step 2: Convert the complete binary tree into max-heap using adjust.
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.
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.
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
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
Analysis (Efficiency)
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)
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
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];
}
}
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
{
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;
} } } }
Example: Sort the list a[1:8]={70, 80, 5, 268, 322, 32, 100, 45} using radix sort.
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)
Here max=322.
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.
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
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
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)
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.
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?
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
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.
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.
The average efficiency of heap sort is 0 (n(log2 n)) where, n is the number of elements sorted.
1. structure property
2. heap order property
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.
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