432 - Pract1-3 - Div A
432 - Pract1-3 - Div A
432 - Pract1-3 - Div A
Practical 1C
Aim:
Program:
#include<iostream>
using namespace std;
int main()
{
int frstarr[50],scndarr[50], fstsize, scndsize, size, i, j, k,
mergarr[100];
cout<<"Enter Array 1 Size : ";
cin>>fstsize;
cout<<"Enter Array 1 Elements : ";
for(i=0; i<fstsize; i++)
{
cin>>frstarr[i];
mergarr[i]=frstarr[i];
}
cout<<"Enter Array 2 Size : ";
cin>>scndsize;
cout<<"Enter Array 2 Elements : ";
for(i=0; i<scndsize; i++)
{
cin>>scndarr[i];
}
size=fstsize+scndsize;
for(i=0, k=fstsize; k<size && i<scndsize; i++,k++)
{
mergarr[k]=scndarr[i];
}
cout<<"Now the new array after merging is :\n";
for(i=0; i<size; i++)
{
cout<<mergarr[i]<<" ";
}
}
Output:
Practical 2A
Implement sorting technique:
Aim: write a program to implement selection sort
Program:
#include<iostream.h>
#include<conio.h>
void swapping(int &a, int &b)
{ //swap the content of a and b
int temp;
temp = a;
a = b;
b = temp;
}
void display(int *array, int
size)
{
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
void selectionSort(int *array, int
size)
{
int i, j, imin;
for(i = 0; i<size-1; i++)
{
imin = i;
Output:
Practical 2B
Aim: - Write a program for insertion
Input.
#include<iostream.h>
#include<conio.h> void
display(int *array, int size)
{
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
void insertionSort(int *array, int
size)
{
int key, j;
for(int i = 1; i<size; i++)
{
key = array[i];//take value
j = i;
while(j > 0 && array[j-1]>key)
{
array[j] = array[j-1];
j--;
}
array[j] = key; //insert in right
place
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[50]; //create an array with given number of
elements
cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++)
{
cin >> arr[i];
}
cout << "Array before
Sorting: ";
display(arr, n);
insertionSort(arr, n);
cout << "Array after Sorting: ";
display(arr, n);
getch();
}
Output:
Practical 2C
Aim: write a program for merge sort
Program:
#include<iostream>
using namespace std;
void swapping(int &a, int &b)
{
//swap the content of a and b
int temp;
temp = a;
a = b;
b = temp;
}
void display(int *array, int size)
{
for(int i = 0; i<size; i++)
cout << array[i] << " ";
cout << endl;
}
void merge(int *array, int l, int m, int r)
{
int i, j, k, nl, nr;
//size of left and right sub-arrays
nl = m-l+1;
nr = r-m;
int larr[nl], rarr[nr];
//fill left and right sub-arrays
for(i = 0; i<nl; i++)
larr[i] = array[l+i];
for(j = 0; j<nr; j++)
rarr[j] = array[m+1+j];
i = 0;
j = 0;
k = l;
//marge temp arrays to real array
while(i < nl && j<nr)
{
if(larr[i] <= rarr[j])
{
array[k] = larr[i];
i++;
}
Else
{
array[k] = rarr[j];
j++;
}
k++;
}
while(i<nl)
{
//extra element in left array
array[k] = larr[i];
i++; k++;
}
while(j<nr)
{
//extra element in right array
array[k] = rarr[j];
j++; k++;
}
}
void mergeSort(int *array, int l, int r)
{
int m;
if(l < r)
{
int m = l+(r-l)/2;
// Sort first and second arrays
mergeSort(array, l, m);
mergeSort(array, m+1, r);
merge(array, l, m, r);
}
}
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n]; //create an array with given number of elements
cout << "Enter elements:" << endl;
for(int i = 0; i<n; i++)
{
cin >> arr[i];
}
cout << "Array before Sorting: ";
display(arr, n);
mergeSort(arr, 0, n-1); //(n-1) for last index
cout << "Array after Sorting: ";
display(arr, n);
}
Output:
Practical 3
Implement searching technique
Aim: write a program to implement binary search
Input.
#include<iostream>
using namespace std;
int main()
{
int frstarr[50],scndarr[50], fstsize, scndsize, size, i, j,
k, mergarr[100]; cout<<"Enter Array 1 Size : ";
cin>>fstsize;
cout<<"Enter Array 1 Elements : ";
for(i=0; i<fstsize; i++)
{
cin>>frstarr[i];
mergarr[i]=frstarr[i];
}
cout<<"Enter Array 2 Size : ";
cin>>scndsize;
cout<<"Enter Array 2 Elements : ";
for(i=0; i<scndsize; i++)
{
cin>>scndarr[i];
}
size=fstsize+scndsize;
for(i=0, k=fstsize; k<size && i<scndsize; i++,k++)
{
mergarr[k]=scndarr[i];
}
cout<<"Now the new array after merging is :\n";
for(i=0; i<size; i++)
{
cout<<mergarr[i]<<" ";
}
}
Output: