0% found this document useful (0 votes)
3 views

DAA LAB FILE 1

The document contains multiple programming tasks related to sorting algorithms, including implementations of iterative and recursive binary search, quick sort, merge sort, radix sort, bubble sort, and insertion sort. Each section provides source code examples and outlines the expected output and time complexity for each sorting method. The document serves as a comprehensive guide for understanding and implementing various sorting techniques in C++.

Uploaded by

chetanvasantala
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)
3 views

DAA LAB FILE 1

The document contains multiple programming tasks related to sorting algorithms, including implementations of iterative and recursive binary search, quick sort, merge sort, radix sort, bubble sort, and insertion sort. Each section provides source code examples and outlines the expected output and time complexity for each sorting method. The document serves as a comprehensive guide for understanding and implementing various sorting techniques in C++.

Uploaded by

chetanvasantala
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/ 13

LAB-1

1.a: Implement iterative binary search.


Source Code:
#include <iostream>
using namespace std;
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r)
{
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;

if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}

return -1;
}
int main(void)
{
int arr[] = { 18, 27, 53, 67, 89};
int x = 67;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
if(result == -1)
{
cout << "Element is not present in array";
}
else
{
cout << "Element is present at index " << result;
}
return 0;
}
1.b: Implement recursive binary search.
Source Code:
#include <iostream>
using namespace std;
int binarySearch(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
return binarySearch(arr, mid + 1, r, x);
}
return -1;
}
int main()
{
int arr[] = { 16, 21, 47, 68, 89};
int x = 21;
int n = sizeof(arr) / sizeof(arr[0]);
int result = binarySearch(arr, 0, n - 1, x);
if(result == -1)
cout << "Element is not present in array";
else
cout << "Element is present at index " << result;
return 0;
}

1.c: Sort a given set of elements using the Quick sort method. Repeat the
experiment for different values of n, the number of elements to be sorted.
The elements can be read from any file or generate them using a random
number generator.
Source Code:
#include <iostream>
using namespace std;

int partition(int arr[],int low,int high)


{
int pivot=arr[high];
int i=(low-1);

for(int j=low;j<=high;j++)
{
if(arr[j]<pivot)
{
i++;
swap(arr[i],arr[j]);
}
}
swap(arr[i+1],arr[high]);
return (i+1);
}
void quickSort(int arr[],int low,int high)
{
if(low<high)
{
int pi=partition(arr,low,high);
quickSort(arr,low,pi-1);
quickSort(arr,pi+1,high);
}
}
int main()
{
int n;
cout<<"Enter the size of array\n";
cin>>n;
int arr[n];
cout<<"Enter the elements of array\n";
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
quickSort(arr,0,n-1);
cout<<"Sorted Array\n";
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
LAB-2
2.a Write a program to sort the array using merge sort.
Source Code :
#include<bits/stdc++.
h> using namespace
std;
void merge(vector<int>&a, int low, int mid, int high)
{
int i = low, j = mid + 1, index = low, temp[100],
k; while ((i <= mid) && (j <= high)) {
if (a[i] < a[j]) {
temp[index] = a[i];
i++;
} else {
temp[index] = a[j];
j++;
}
index++; }
if (i > mid)
{

while (j <= high) {


temp[index] = a[j];
j++;
index++;
} } else
{
while (i <= mid) {
temp[index] = a[i];
i++;
index++;} }
for (k = low; k < index;
k++) a[k] = temp[k];}
void mergesort(vector<int>&v,int first,int last)
{
if(first>=last)return;
int middle = (first + last)
/ 2; mergesort(v, first,
middle);
mergesort(v, middle +
1,last ); merge(v, first,
middle, last);
}
int main()
{
int t;
cout<<"ENTER THE NUMBER OF TEST CASES : " ;
cin>>t;
cout<<end
l; while(t--)
{
int n;
cout<<" ENTER NUMBER OF ELEMENTS IN ARRAY : ";
cin>>n;

vector<int>v(n);
cout<<"ENTER ELEMENTS OF ARRAY : ";
for(int i=0;i<n;i++)
cin>>v[i];
cout<<" ELEMENTS OF ARRAY AFTER IS : { ";
for (int i = 0; i<n ; i++)
{
cout<<v[i]<<",";

}
cout<<"\b"<<" }";
mergesort(v,0,v.size()-1);
cout<<endl<<"SORTED ARRAY AFTER MERGE SORT IS : { ";
for (int i = 0; i < n;
i++)
cout<<v[i]<<",";
cout<<"\b"<<" }";
cout<<endl;
cout<<endl;
}cout<<" time complexity is nlogn
"; return 0;
}

OUTPUT :

2.b Write a program to sort the array using radix sort.


Source Code:
#include <bits/stdc++.h>
using namespace std;
int maximum(vector<int>&arr)
{
int maxi=INT_MIN;

for(int
i=0;i<arr.size();i++)
maxi=max(maxi,arr[i]);
return maxi;
}
void count_sort(vector<int>&arr, int n, int pos)
{int count[10] = { 0 };

for (int i = 0; i < n;


i++) count[(arr[i] / pos) %
10]++;
for (int i = 1; i < 10; i++)
count[i] = count[i] + count[i -
1]; int ans[n];
for (int i = n - 1; i >= 0; i--)
ans[--count[(arr[i] / pos) % 10]] =
arr[i]; for (int i = 0; i < n; i++)

arr[i] = ans[i]; }

void radix_sort(vector<int>&arr, int n)


{ int k = maximum(arr);
for (int pos = 1; (k / pos) > 0; pos
*= 10) count_sort(arr, n, pos); }
int main()
{
int n;
cout<<" ENTER NUMBER OF ELEMENTS IN ARRAY : ";
cin>>n;

vector<int>v(n);
cout<<"ENTER ELEMENTS OF ARRAY : ";
for(int i=0;i<n;i++)
cin>>v[i];
cout<<" ELEMENTS OF ARRAY AFTER IS : { ";
for (int i = 0; i<n ;
i++)
cout<<v[i]<<",";
cout<<"\b"<<" }";
radix_sort(v,v.size());
cout<<endl<<"SORTED ARRAY AFTER RADIX SORT IS : { ";
for (int i = 0; i < n; i++)
{cout<<v[i]<<",";}
cout<<"\b"<<" }";
cout<<endl;
cout<<"TIME COMPLEXITY OF RADIX SORT IS LINEAR ";return 0;

OUTPUT:
LAB-3

3.a Write a program to sort the array using bubble sort.

Source Code :
#include<bits/stdc++.h>

using namespace std;


void bubblesort(vector<int>&v)
{
int n=v.size();
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(v[j]>v[j+1])
swap(v[j],v[j+1]);
}
}
}
int main()
{
int n;
cout<<" ENTER NUMBER OF ELEMENTS IN ARRAY : ";
cin>>n;
vector<int>v(n);
cout<<"ENTER ELEMENTS IN ARRAY : "<<endl;
for(int
i=0;i<n;i++)
cin>>v[i];

cout<<" ELEMENTS OF ARRAY AFTER IS : { ";


for (int i = 0; i<n ; i++)
cout<<v[i]<<",";
cout<<"\b"<<"
}";
bubblesort(v);
cout<<endl<<"SORTED ARRAY AFTER BUBBLE SORT IS : { ";
for (int i = 0; i < n; i++)
{
cout<<v[i]<<",";
}
cout<<"\b"<<"
}"; cout<<endl;
cout<<" time complexity is
n*2"; return 0;
}

OUTPUT :

2.b Write a program to sort array using insertion sort.

Source Code :
#include<bits/stdc++.h>
using namespace std;

void insertionsort(vector<int>&arr)
{
int
n=arr.size();
int i,ele,j;
for(i=1;i<n;i++)
{
ele=arr[i
]; j=i-1;
while(j>=0 && arr[j]>ele)
{
arr[j+1]=arr[
j]; j--;
}
arr[j+1]=ele;
}
}
int main()
{
int n;
cout<<" ENTER NUMBER OF ELEMENTS IN ARRAY : ";
cin>>n;
vector<int>v(n);
cout<<"ENTER ELEMENTS IN ARRAY : "<<endl;
for(int
i=0;i<n;i++)
cin>>v[i];
cout<<" ELEMENTS OF ARRAY AFTER IS : { ";
for (int i = 0; i<n ; i++)
{
cout<<v[i]<<",";
}
cout<<"\b"<<" }";
insertionsort(v);
cout<<endl<<"SORTED ARRAY AFTER INSERTION SORT IS : { ";
for (int i = 0; i < n; i++)
{
cout<<v[i]<<",";
}
cout<<"\b"<<" }";
cout<<endl;
cout<<" time complexity is n*2";
return 0;
}

OUTPUT :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy