Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

432 - Pract1-3 - Div A

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

Practical 1A

Aim: write a practical on bubble sort


Program:
#include<bits/stdc++>
using name spacestd;
int main ()
{
int n;
cout<<"Enter number of element you want to store:";
cin>>n;
int arr[n],i,j;
cout<<"Enter array values:\n";
//taking the array value
//from user
for(i=0;i<n;i++)
{
cin>>arr[i];
}
//Now we will sort the array
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
//checking if previous value is
//grater than next one or not
if(arr[j]>arr[j+1])
{
//temp will temporarly store
//the value of arr[j] //then
we will swap the values int
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<”After Bubble sort the array is:\
n";
for(i=0;i<n;i++) cout<<arr[i]<<" ";
return 0;
}
Output:
Practical 1B
Aim: Write a program to store elements in 1-Dimentinal array and
perform the operation like searching, sorting and reversing the
element
Program
1) Sorting:
Input:
#include<iostream>
using namespace std;
int main ()
{
int i, j,temp; int a[5]
= {10,2,0,43,12};
cout <<"Input list ...\n";
for(i = 0; i<5; i++)
{
cout <<a[i]<<"\t";
}
cout<<endl;
for(i = 0; i<5; i++)
{
for(j = i+1; j<5; j++)
{
if(a[j]< a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
cout <<"Sorted Element
List ...\n";
for(i = 0; i<5; i++)
{
cout <<a[i]<<"\t";
}
return 0;
}
Output:
2) reversing
Input:
#include<iostrea
m>
Using
namespace std;
int main ()
{
int i, j,temp;
int a[5] =
{10,2,0,43,12};
cout <<"Input list ...\n";
for(i = 0; i<5; i++)
{
cout <<a[i]<<"\t";
}
cout<<endl;
for(i = 0; i<5; i++)
{
for(j = i+1; j<5; j++)
{
if(a[j]<a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
cout <<"Sorted Element List ...\n";
for(i = 0; i<5; i++)
{
cout <<a[i]<<"\t";
}
cout <<"Sorted Element in reversed order List ...\n";
for(i=4;i>=0;i--)
{
cout <<a[i]<<"\t";
}
return 0;
}
Output:
3) searching:
Input:
#include<iostream>
using namespace std;
int main ()
{
int i, j,temp;
int a[5] =
{10,2,0,43,12};
cout <<"Input list ...\n";
for(i = 0; i<5; i++)
{
cout <<a[i]<<"\t";
}
cout<<endl;
for(i = 0; i<5; i++)
{
for(j = i+1; j<5; j++)
{
if(a[j] < a[i])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
cout <<"Sorted Element List ...\
n";
for(i = 0; i<5; i++)
{
cout <<a[i]<<"\t";
}

cout <<"please enter searching


element ";
cin>>p;
for(i=0;i<5;i++)
{
if(p=a[i
])
{
cout<<"element found at"<<I;
}
else
{
cout<<"element not present ";
}
}
cout <<"Sorted Element in reversed order List ...\n";
for(i=4;i>=0;i--)
{
cout <<a[i]<<"\t";
}
return 0;
}
Output:

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;

//get index of minimum data

for(j = i+1; j<size; j++)

if(array[j] < array[imin])


imin = j;
//placing in correct position
swapping(array[i],
array[imin]);
}
}
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);
selectionSort(arr, n);
cout << "Array after Sorting: ";
display(arr, n);
getch();
}

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:

You might also like