Lab File-1
Lab File-1
Lab File-1
- Algorithm
procedure bubbleSort(A: list)
n = length(A)
for i from 0 to n - 1 do
for j from 0 to n - i - 1 do
if A[j] > A[j + 1] then
swap(A[j], A[j + 1])
end if
end for
end for
end procedure
- Code
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void bubbleSort(int a[], int n)
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
swap(a[j],a[j+1]);
}
}
}
int main()
{
int n;
cout<<"Enter the no of elements: ";
cin>>n;
int a[n];
sr(time(0));
cout<<"R aay: ";
for(int i=0;i<n;i++)
{
a[i]=r()%100;
cout<<a[i]<<" ";
}
cout<<endl;
clock_t start, end;
start=clock();
bubbleSort(a,n);
end=clock();
double executionTime=double(end-start)/CLOCK_PER_SEC;
cout<<"Sorted aay: ";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"Execution time: "<<executionTime<<" seconds"<<endl;
return 0;
}
- Table
Input Size (n) Execution Time (in seconds)
100 52
500 1279
1000 5208
5000 83591
10000 373367
- Graph
- Application
1) Educational Purposes
2) Small Data Sets
3) Teaching Tool
4) Debugging and Testing
end if
end for
if minIndex ≠ i then
swap(a[i], a[minIndex]) end if
end for
end procedure
- Code
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
void selectionSort(int a[],int n)
{
for(int i=0;i<n-1;i++)
{
int minIndex=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[minIndex])
minIndex=j;
}
swap(a[minIndex],a[i]);
}
}
int main()
{
int n;
cout<<"Enter the no of elements: ";
cin>>n;
int a[n];
sr(time(0));
cout<<"R aay: ";
for(int i=0;i<n;i++)
{
a[i]=r()%100;
cout<<a[i]<<" ";
}
cout<<endl;
clock_t start,end;
start=clock();
selectionSort(a,n);
end=clock();
cout<<"Sorted aay: ";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
double executionTime=double(end-start) /CLOCK_PER_SEC;
cout<<"Execution time: "<<executionTime<<" seconds"<< endl;
return 0;
- Table
Input Size (n) Execution Time (in seconds)
100 26
500 385
1000 1605
5000 32765
10000 150987
- Graph
- Application
1) Educational Purposes
2) Small Data Sets
3) Embedded Systems or Limited Environments
4) Human Sorting
5) Preprocessing or Initial Phases
- Algorithm
procedure insertionSort(A: list)
n = length(A)
for i from 1 to n-1 do
k = A[i]
j=i-1
while j >= 0 and A[j] > k do A[j
+ 1] = A[j]
j=j-1
end while
A[j + 1] = k
end for
end procedure
- Code
#include<iostream>
#include<ctime>
using namespace std;
void insertionSort(int a[],int n)
{
for(int i=1;i<n;i++)
{
int k=a[i];
int j=i-1;
while(j>=0 && a[j]>k) {
a[j+1]=a[j];
j--;
}
a[j+1]=k;
}
}
int main()
{
int n;
cout<<"Enter the size of the aay: ";
cin>>n;
int a[n];
sr(time(0));
for(int i=0;i<n;i++)
a[i]=r()%100;
clock_t start=clock();
insertionSort(a,n);
clock_t end=clock();
cout<<"Sorted aay: ";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
double executionTime=double(end-start) /CLOCK_PER_SEC;
cout<<"Execution time: "<<executionTime<<" seconds"<<endl;
return 0;
- Table
Input Size (n) Execution Time (in seconds)
100 15
500 210
1000 875
5000 18078
10000 68754
- Graph
- Application
1) Small Data Sets
2) Online Algorithms
3) Embedded Systems
4) Human Sorting
5) Sorting nearly sorted aays
- Algorithm
MergeSort(a[], left, right)
if left < right
middle = (left+right)/2
MergeSort(a, left, middle)
MergeSort(a, middle+1, right)
Merge(a, left, middle, right)
Merge(a[], left, middle, right) n1 =
middle - left + 1
n2 = right - middle
leftaay[1...n1], rightaay[1...n2] for i
= 1 to n1
rightaay[j] = a[middle + j] i = 1, j
=1
k = left
while i <= n1 and j <= n2
if leftaay[i] <= rightaay[j] a[k] =
leftaay[i]
i=i+1
else
a[k] = rightaay[j] j = j + 1
k=k+1
while i <= n1
a[k] = leftaay[i]
i=i+1
k=k+1
while j <= n2
a[k] = rightaay[j]
j=j+1
k=k+1
- Code
#include<iostream>
#include<ctime>
using namespace std;
void merge(int a[],int l,int m,int r)
{
int n1=m-l+1;
int n2=r-m;
int L[n1],R[n2];
for(int i=0;i<n1;i++)
L[i]=a[l+i];
for(int j=0;j<n2;j++)
R[j]=a[m+1+j];
int i=0,j=0,k=1;
while(i<n1 && j<n2)
{
if(L[i]<=R[j])
{
a[k]=L[i];
i++;
}
else
{
a[k]=R[j];
j++;
}
k++;
}
while(i<n1)
{
a[k]=L[i];
i++;
k++;
}
while(j<n2)
{
a[k]=R[j];
j++;
k++;
}
}
void mergeSort(int a[],int l,int r)
{
if (l<r)
{
int m=l+(r-l)/2;
mergeSort(a,l,m);
mergeSort(a,m+1,r);
merge(a,l,m,r);
}
}
int main()
{
int n;
cout<<"Enter the no of elements: ";
cin>>n;
int a[n];
sr(time(0));
for(int i=0; i < n; i++)
a[i]=r()%100;
clock_t start,end;
start=clock();
mergeSort(a,0,n-1);
end=clock();
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
double executionTime=double(end-start) /CLOCK_PER_SEC;
cout<<"Execution time: "<<executionTime<<" seconds"<<endl;
return 0;
- Table
Input Size (n) Execution Time (in seconds)
100 10
500 80
1000 165
5000 795
10000 1457
- Graph
- Application
1) Network Routing
2) Data Mining
3) Inversion Counting
4) Parallel Computing
5) Online Streaming Services
- Algorithm
QuickSort(a, low, high):
if low < high:
pivot_position = Partition(a, low, high)
QuickSort(a, low, pivot_position - 1)
QuickSort(a, pivot_position + 1, high)
Partition(a, low, high):
pivot = a[high]
i = low - 1
for j = low to high - 1:
if a[j] <= pivot:
i=i+1
Swap(a[i], a[j])
Swap(a[i + 1], a[high])
return i + 1
Swap(a, b):
temp = a
a=b
b = temp
- Code
#include <iostream>
#include <ctime>
using namespace std;
void swap(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
int partition(int a[],int low,int high)
{
int pivot=a[high],i=(low-1);
for(int j=low;j<=high-1;j++)
{
if(a[j]<pivot)
{
i++;
swap(&a[i],&a[j]);
}
}
swap(&a[i+1],&a[high]);
return (i+1);
}
void quickSort(int a[],int low,int high)
{
if(low<high)
{
int pi=partition(a,low,high);
quickSort(a,low,pi-1);
quickSort(a,pi+1,high);
}
}
int main()
{
int n;
cout<<"Enter the no of elements: ";
cin>>n;
int a[n];
sr(time(0));
cout<<"R aay: ";
for(int i=0;i<n;i++)
{
a[i]=r()%100;
cout<<a[i]<<" ";
}
cout<<endl;
clock_t start,end;
start=clock();
quickSort(a,0,n-1);
end=clock();
cout<<"Sorted aay: ";
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
double executionTime=double(end-start) /CLOCK_PER_SEC;
cout<<"Execution time: "<<executionTime<<" seconds"<<endl;
return 0;
- Table
Input Size (n) Execution Time (in seconds)
100 11
500 70
1000 95
5000 892
10000 2848
- Graph
- Application
1) Search Engines
2) Data Processing
LAB 2
LINEAR SORTING
- Algorithm
Counting Sort(A,B,k):
1. for i←0 to k
2. do C[i] ←0
3. for j ←1 to length[A]
4. do C[A[j]] ←C[A[j]]+1
5. // C[i] contains no of elements equal to i.
6. for i ←1 to k
7. do C[i]=C[i]+C[i-1]
- Code
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
void countingSort(int a[],int n,int max)
{
int count[max+1]={0};
for(int i=0;i<n;i++)
count[a[i]]++;
int outputIndex=0;
for(int i=0;i<=max;i++)
{
while(count[i]>0)
{
a[outputIndex++]=i;
count[i]--;
}
}
}
int main()
{
int n;
cout<<"Enter the no of elements in the aay: ";
cin>>n;
int a[n];
sr(time(0));
for(int i=0;i<n;i++)
a[i]=r()%100;
clock_t start_time=clock();
countingSort(a,n,n-1);
clock_t end_time=clock();
double execution_time=double(end_time-start_time)/CLOCKS_PER_SEC;
cout<<"Sorted aay:"<<endl;
for(int i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"Execution time: "<<execution_time<<" seconds"<<endl;
return 0;
}
- Table
Input Size (n) Execution Time (in seconds)
100 0.008
500 0.0010
1000 0.020
5000 0.07
10000 0.092
- Graph
- Application
1) Integer Sorting
2) Histogram Generation
3) Data Compression
4) Color Sorting
5) Radix Sort
- Algorithm
Radix Sort(A,d):
- For i=1 to d do
use a stable sort to sort aay A on digit i //Counting Sort
- Code
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
void countSort(int a[],int n,int exp)
{
int output[n];
int count[10]={0};
for(int i=0;i<n;i++)
count[(a[i]/exp)%10]++;
for(int i=1;i<10;i++)
count[i]+=count[i-1];
for(int i=n-1;i>=0;i--)
{
output[count[(a[i]/exp)%10]-1]=a[i];
count[(a[i]/exp)%10]--;
}
for(int i=0;i<n;i++)
a[i]=output[i];
}
void radixSort(int a[],int n)
{
int max=a[0];
for(int i= 1;i<n;i++)
{
if(a[i]>max)
max=a[i];
}
for(int exp=1;max/exp>0;exp*= 10)
countSort(a,n,exp);
}
int main()
{
int SIZE;
cout<<"Enter the Size of the aay :";
cin>>SIZE;
int a[SIZE];
sr(time(NULL));
cout<<"Original aay: ";
for(int i=0;i<SIZE;i++)
{
a[i]=r()%1000;
cout<<a[i]<<" ";
}
cout<<endl;
clock_t start=clock();
radixSort(a,SIZE);
clock_t end=clock();
cout<<"Sorted aay: ";
for(int i=0;i<SIZE;i++)
cout<<a[i]<<" ";
cout<<endl;
double time_taken=double(end-start)/CLOCKS_PER_SEC;
cout<<"Time taken by radix sort: "<<fixed<<time_taken<<" seconds"<<endl;
return 0;
}
- Table
Input Size (n) Execution Time (in seconds)
100 0.017
500 0.055
1000 0.095
5000 0.440
10000 0.456
- Graph
- Application
1) Data Processing
2) Digital Forensics
3) Image Processing
4) Genetic Sequencing
5) Sorting Network Traffic
- Algorithm
Bucket Sort(A)
1. n ←length[A]
2. for i ←1 to n
3. do insert A[i] into bucket B[⎣nA[i]⎦] 4.
for i ←0 to n-1
- Code
#include<iostream>
#include<ctime>
using namespace std;
void bucketSort(int a[],int n)
{
const int max_value=n;
const int bucket_size=10;
int buckets[bucket_size][max_value/bucket_size];
for(int i=0;i<bucket_size;++i)
for(int j=0;j<max_value/bucket_size;++j)
buckets[i][j]=0;
for(int i=0;i<n;++i)
{
int bucket_index=a[i]/bucket_size;
buckets[bucket_index][a[i]%bucket_size]=a[i]; }
int index=0;
for(int i=0;i<bucket_size;++i)
{
for(int j=0;j<max_value/bucket_size;++j) {
if(buckets[i][j]!=0)
a[index++]=buckets[i][j];
}
}
}
int main()
{
int size;
cout<<"Enter the Size of the aay :";
cin>>size;
int a[size];
sr(time(NULL));
for(int i=0;i<size;++i)
a[i]=r()%100;
cout<<"Unsorted aay:"<<endl;
for(int i=0;i<size;++i)
cout<<a[i]<<" ";
cout<<endl;
clock_t start_time=clock();
bucketSort(a,size);
clock_t end_time=clock();
cout<<"Sorted aay:"<<endl;
for(int i=0;i<size;++i)
cout<<a[i]<<" ";
cout<<endl;
double execution_time=double(end_time-start_time)/CLOCKS_PER_SEC;
cout<<"Execution time: " <<execution_time<<" seconds"<<endl; return 0;
}
- Table
Input Size (n) Execution Time (in seconds)
100 0.008
500 0.0010
1000 0.06
5000 0.056
10000 0.250
- Graph
- Application
1) Data Cleaning and Filtering
2) Computational Finance
LAB 3
HEAP
- Algorithm
Max_Heapify(A,i,n)
1. l ← LEFT(i)
2. r ← RIGHT(i)
3. if l ≤ n and A[l] > A[i]
4. then largest ←l
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest ≠ i
9. then exchange A[i] ↔ A[largest]
10. MAX-HEAPIFY(A, largest, n)
Build_Max_Heap(A)
1. n = length[A]
2. for i ← ⎣n/2⎦ down to 1
3. do MAX-HEAPIFY(A, i, n)
- Code
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
void maxHeapify(int a[],int n,int i)
{
int largest=i;
int left=2*i+1;
int right=2*i+2;
if(left<n && a[left]>a[largest])
largest=left;
if(largest != i)
{
swap(a[i],a[largest]);
maxHeapify(a,n,largest); }
}
void buildMaxHeap(int a[],int n)
{
for(int i=(n/2)-1;i>=0;i--)
maxHeapify(a,n,i);
}
void printaay(int a[],int n) {
for(int i=0;i<n;++i)
cout<<a[i]<<" ";
cout<<endl;
}
int main()
{
sr(time(0));
int n;
cout<<"Enter the Size of the aay :";
cin>>n;
int a[n];
for(int i=0;i<n;i++)
a[i]=r()%100;
cout<<"Original aay:"<< endl;
printaay(a,n);
clock_t startTime=clock();
buildMaxHeap(a,n);
clock_t endTime=clock();
cout<<"Heapified aay:"<<endl;
printaay(a,n);
double executionTime=double(endTime-startTime)/CLOCKS_PER_SEC;
cout<<"Execution Time: "<<executionTime<<" seconds"<<endl; return 0;
- Table
Input Size (n) Execution Time (in seconds)
100 0.0006
500 0.0020
1000 0.0050
5000 0.0250
10000 0.0430
- Graph
2) Analysis of Min Heapify and Build Min Heap
- Algorithm
Min_Heapify(A,i,n)
1. l ← LEFT(i)
2. r ← RIGHT(i)
3. if l ≤ n and A[l] < A[i]
4. then smallest ←l
5. else smallest ←i
6. if r ≤ n and A[r] < A[smallest]
7. then smallest ←r
8. if smallest ≠ i
9. then exchange A[i] ↔ A[smallest]
10. Min_Heapify(A, smallest, n)
Build_Min_Heap(A)
1. n = length[A]
2. for i ← ⎣n/2⎦ down to 1
3. do Min_Heapify(A, i, n)
- Code
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
void minHeapify(int a[],int n,int i)
{
int smallest=i;
int left=2*i+1;
int right=2*i+2;
if(left<n && a[left]<a[smallest])
smallest=left;
if(smallest!=i)
{
swap(a[i],a[smallest]);
minHeapify(a,n,smallest); }
}
void buildMinHeap(int a[],int n)
{
for(int i=(n/2)-1;i>=0;i--)
minHeapify(a,n,i);
}
void printaay(int a[],int n)
{
for(int i=0;i<n;++i)
cout<<a[i]<<" ";
cout<<endl;
}
int main()
{
sr(time(0));
int n;
cout<<"Enter the Size of the aay :";
cin>>n;
int a[n];
for(int i=0;i<n;i++)
a[i]=r()%100;
cout<<"Original aay:"<< endl;
printaay(a,n);
clock_t startTime=clock();
buildMinHeap(a,n);
clock_t endTime=clock();
cout<<"Heapified aay:"<<endl;
printaay(a,n);
double executionTime=double(endTime-startTime)/CLOCKS_PER_SEC;
cout<<"Execution Time: "<<executionTime<<" seconds"<<endl; return 0;
}
- Table
Input Size (n) Execution Time (in seconds)
100 0.00010
500 0.0021
1000 0.0035
5000 0.0132
10000 0.0254
- Graph
if(largest!=i)
{
swap(a[i],a[largest]);
heapify(a,n,largest);
}
}
void heapSort(int a[],int n)
{
for(int i=(n/2)-1;i>=0;i--)
heapify(a,n,i);
for(int i=n-1;i>0;i--)
{
swap(a[0],a[i]);
heapify(a,i,0);
}
}
int main()
{
sr(time(0));
int size;
cout<<"Enter the Size of the aay :";
cin>>size;
int a[size];
cout<<"Original aay: ";
for(int i=0;i<size;i++)
{
a[i]=r()%100;
cout<<a[i]<<" ";
}
cout<<endl;
clock_t start_time=clock();
heapSort(a,size);
clock_t end_time=clock();
cout<<"Sorted aay: ";
for(int i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
double execution_time=double(end_time-start_time)/CLOCKS_PER_SEC;
cout<<"Execution Time: "<<execution_time<<" seconds"<<endl;
return 0;
}
- Table
Input Size (n) Execution Time (in seconds)
100 0.0035
500 0.00115
1000 0.00265
5000 0.01334
10000 0.04778
- Graph
- Algorithm
Heapsort(A)
1. BUILD-MIN-HEAP(A)
2. for i ← length[A] downto 2
3. do exchange A[1] ↔ A[i] 4.
MIN-HEAPIFY(A, 1, i - 1)
- Code
#include<iostream>
#include<ctime>
#include<cstdlib>
using namespace std;
void heapify(int a[],int n,int i)
{
int smallest=i;
int left=2*i+1;
int right=2*i+2;
if(left<n && a[left]<a[smallest])
smallest=left;
if(smallest!=i)
{
swap(a[i],a[smallest]);
heapify(a,n,smallest);
}
}
void heapSort(int a[],int n)
{
for(int i=(n/2)-1;i>=0;i--)
heapify(a,n,i);
for(int i=n-1;i>0;i--)
{
swap(a[0],a[i]);
heapify(a,i,0);
}
}
int main()
{
sr(time(0));
int size;
cout<<"Enter the Size of the aay :";
cin>>size;
int a[size];
cout<<"Original aay: ";
for(int i=0;i<size;i++)
{
a[i]=r()%100;
cout<<a[i]<<" ";
}
cout<<endl;
clock_t start_time=clock();
heapSort(a,size);
clock_t end_time=clock();
cout<<"Sorted aay: ";
for(int i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
double execution_time=double(end_time-start_time)/CLOCKS_PER_SEC;
cout<<"Execution Time: "<<execution_time<<" seconds"<<endl;
return 0;
}
- Table
Input Size (n) Execution Time (in seconds)
100 0.0021
500 0.00118
1000 0.00235
5000 0.01770
10000 0.052
- Graph