Department of CSE
PROGRAM NO. :- 1
AIM :- Write a Program To Implement Linear Search On The List Of Elements.
Code :-
#include<iostream>
using namespace std;
int main()
int arr[10], i,n, num, index;
cout<<"Atul Attri (2236728)"<<endl;
cout<<"enter the number of elements:";
cin>>n;
cout<<"Enter Numbers: "<<endl;
for(i=0; i<n; i++)
cin>>arr[i];
cout<<"\nEnter a Number to Search: ";
cin>>num;
for(i=0; i<n; i++)
if(arr[i]==num)
index = i;
break;
cout<<"\nFound at Index No."<<index; cout<<endl;
Department of CSE
PROGRAM NO. :- 1
AIM :- Write a Program To Implement Linear Search On The List Of Elements.
Code :-
#include<iostream>
using namespace std;
int main()
int arr[10], i,n, num, index;
cout<<"Aryan Kamboj (2236717)"<<endl;
cout<<"enter the number of elements:";
cin>>n;
cout<<"Enter Numbers: "<<endl;
for(i=0; i<n; i++)
cin>>arr[i];
cout<<"\nEnter a Number to Search: ";
cin>>num;
for(i=0; i<n; i++)
if(arr[i]==num)
index = i;
break;
cout<<"\nFound at Index No."<<index;
cout<<endl;
Department of CSE
PROGRAM NO. :- 1
AIM :- Write a Program To Implement Linear Search On The List Of Elements.
Code :-
#include<iostream>
using namespace std;
int main()
int arr[10], i,n, num, index;
cout<<"Ashima (2236721)"<<endl;
cout<<"enter the number of elements:";
cin>>n;
cout<<"Enter Numbers: "<<endl;
for(i=0; i<n; i++)
cin>>arr[i];
cout<<"\nEnter a Number to Search: ";
cin>>num;
for(i=0; i<n; i++)
if(arr[i]==num)
index = i;
break;
cout<<"\nFound at Index No."<<index;
Department of CSE
PROGRAM NO. :- 2
AIM :- Write a Program To Implement Binary Search On The List Of Elements.
Code :-
#include <iostream>
using namespace std;
int main ()
{
int arr[100], first, mid, last, i, num, search;
cout<<"Ashima (2236721)" <<endl;
cout << " Define the size of the array: " << endl;
cin >> num;
cout << " Enter the values in sorted array either ascending or descending order: " << endl;
for (i = 0; i < num; i++)
{
cin >> arr[i];
}
first = 0;
last = num - 1;
cout << " Define a value to be searched from sorted array: " << endl;
cin >> search;
while ( first <= last)
{
mid = ( first + last) / 2;
if (arr[mid] == search)
{
cout << " Element is found at index " << (mid + 1);
exit (0);
}
else if ( search > arr[mid])
{ first = mid + 1;
Department of CSE
PROGRAM NO. :- 2
AIM :- Write a Program To Implement Binary Search On The List Of Elements.
Code :-
#include <iostream>
using namespace std;
int main ()
{
int arr[100], first, mid, last, i, num, search;
cout<<"Aryan Kamboj (2236717)" <<endl;
cout << " Define the size of the array: " << endl;
cin >> num;
cout << " Enter the values in sorted array either ascending or descending order: " << endl;
for (i = 0; i < num; i++)
{
cin >> arr[i];
}
first = 0;
last = num - 1;
cout << " Define a value to be searched from sorted array: " << endl;
cin >> search;
while ( first <= last)
{
mid = ( first + last) / 2;
if (arr[mid] == search)
{
cout << " Element is found at index " << (mid + 1);
exit (0);
}
else if ( search > arr[mid])
{ first = mid + 1;
Department of CSE
PROGRAM NO. :- 2
AIM :- Write a Program To Implement Binary Search On The List Of Elements.
Code :-
#include <iostream>
using namespace std;
int main ()
{
int arr[100], first, mid, last, i, num, search;
cout<<"Atul Attri (2236728)" <<endl;
cout << " Define the size of the array: " << endl;
cin >> num;
cout << " Enter the values in sorted array either ascending or descending order: " << endl;
for (i = 0; i < num; i++)
{
cin >> arr[i];
}
first = 0;
last = num - 1;
cout << " Define a value to be searched from sorted array: " << endl;
cin >> search;
while ( first <= last)
{
mid = ( first + last) / 2;
if (arr[mid] == search)
{
cout << " Element is found at index " << (mid + 1);
exit (0);
}
else if ( search > arr[mid])
{ first = mid + 1;
Department of CSE
PROGRAM NO. :- 05
AIM :- Write a Program To Implement Quick Sort On The List Of Elements In The
Array.
Code :-
#include <iostream>
using namespace std;
void swap( int &a,int &b){
int temp=a;
a=b;
b=temp;
}
int partition(int arr[],int low,int high){
int pivot =arr[high];
int i= low-1;
for(int j=low;j<=high-1;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 pivotIndex=partition(arr,low,high);
quickSort(arr,low,pivotIndex-1);
quickSort(arr,pivotIndex+1,high);
}
}
Department of CSE
int main(){
cout<<" atul attri";
cout<<" 2236728";
int n;
cout<<" enter the number of elements";
cin>>n;
int arr[n];
cout<<" enter "<<n <<" elements:\n";
for( int i=0;i<n;i++){
std::cin>>arr[i];
}
quickSort(arr,0,n-1);
cout<<" sorted array :";
for( int i=0; i<n;i++){
cout<<arr[i]<<" "<<endl;
}
return 0;
}
Output :-
Department of CSE
PROGRAM NO. :- 06
AIM :- Write a Program To Implement Insertion Sort On The List Of Elements In The
Array.
Code :-
#include <iostream>
using namespace std;
void insert(int a[], int n)
{
int i, j, temp;
for (i = 1; i < n; i++) {
temp = a[i];
j = i - 1;
while(j>=0 && temp <= a[j])
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
void printArr(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
cout << a[i] <<" ";
}
int main()
Department of CSE
{
cout<<" atual attri\n";
cout<<" 2236728\n";
int a[] = { 89, 45, 35, 8, 12, 2 };
int n = sizeof(a) / sizeof(a[0]);
cout<<"Before sorting array elements are - "<<endl;
printArr(a, n);
insert(a, n);
cout<<"\nAfter sorting array elements are - "<<endl;
printArr(a, n);
return 0;
}
Output:-
Department of CSE
PROGRAM NO. :- 07
AIM :- Write a Program To Implement merge Sort On The List Of Elements In The
Array.
Code:-
#include <iostream>
using namespace std;
void merge(int *,int, int , int );
void merge_sort(int *arr, int low, int high)
int mid;
if (low < high){
mid=(low+high)/2;
merge_sort(arr,low,mid);
merge_sort(arr,mid+1,high);
merge(arr,low,high,mid);
void merge(int *arr, int low, int high, int mid)
int i, j, k, c[50];
i = low;
k = low;
j = mid + 1;
Department of CSE
while (i <= mid && j <= high) {
if (arr[i] < arr[j]) {
c[k] = arr[i];
k++;
i++;
else {
c[k] = arr[j];
k++;
j++;
while (i <= mid) {
c[k] = arr[i];
k++;
i++;
while (j <= high) {
c[k] = arr[j];
k++;
j++;
for (i = low; i < k; i++) {
arr[i] = c[i];
}
Department of CSE
int main()
cout<<"Atul (2236728)"<<endl;
int myarray[30], num;
cout<<"Enter number of elements to be sorted:";
cin>>num;
cout<<"Enter "<<num<<" elements to be sorted:";
for (int i = 0; i < num; i++) { cin>>myarray[i];
merge_sort(myarray, 0, num-1);
cout<<"Sorted array\n";
for (int i = 0; i < num; i++)
cout<<myarray[i]<<"\t";
}
Department of CSE
PROGRAM NO. :- 08
AIM :- Write a Program To Implement Heap Sort On The List Of Elements In The
Array.
Code :-
#include <iostream>
using namespace std;
// function to heapify the tree
void heapify(int arr[], int n, int root)
{
int largest = root; // root is the largest element
int l = 2*root + 1; // left = 2*root + 1
int r = 2*root + 2; // right = 2*root + 2
// If left child is larger than root
if (l < n && arr[l] > arr[largest])
largest = l;
// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
largest = r;
// If largest is not root
if (largest != root)
{
//swap root and largest
swap(arr[root], arr[largest]);
// Recursively heapify the sub-tree
heapify(arr, n, largest);
}
}
// implementing heap sort
void heapSort(int arr[], int n)
{
// build heap
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// extracting elements from heap one by one
for (int i=n-1; i>=0; i--)
{
// Move current root to end
swap(arr[0], arr[i]);
// again call max heapify on the reduced heap
heapify(arr, i, 0);
}
Department of CSE
/* print contents of array - utility function */
void displayArray(int arr[], int n)
{
for (int i=0; i<n; ++i)
cout << arr[i] << " ";
cout << "\n";
}
// main program
int main()
{
int heap_arr[] = {4,17,3,12,9,6};
int n = sizeof(heap_arr)/sizeof(heap_arr[0]);
cout<<"Input array"<<endl;
displayArray(heap_arr,n);
heapSort(heap_arr, n);
cout << "Sorted array"<<endl;
displayArray(heap_arr, n);
return 0;
}
Output:-