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

Program - OBJECT:-Write A Program To Sort A Given Array Using Insertion Sort

The document contains code for sorting and searching algorithms: 1) It includes code to sort an array using insertion sort, bubble sort, and selection sort. 2) It also includes code to perform linear search and binary search to search for a number in an array. 3) For each algorithm, the code takes input from the user, implements the algorithm, and outputs the sorted array or search result.

Uploaded by

Sumit Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Program - OBJECT:-Write A Program To Sort A Given Array Using Insertion Sort

The document contains code for sorting and searching algorithms: 1) It includes code to sort an array using insertion sort, bubble sort, and selection sort. 2) It also includes code to perform linear search and binary search to search for a number in an array. 3) For each algorithm, the code takes input from the user, implements the algorithm, and outputs the sorted array or search result.

Uploaded by

Sumit Sharma
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

PROGRAM OBJECT:- Write a program to sort a given array using insertion sort.

#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[20],i,j,n,y,c=0; cout<<"Enter size of array :- "; cin>>n; for(i=0;i<n;i++) { cout<<"enter element:-"; cin>>a[i]; } for(i=1;i<n;i++) { y=a[i]; for(j=i-1;j>=0&&y<a[j];j--) { a[j+1]=a[j]; c++; } a[j+1]=y; } cout<<"After sorting values are:-"; for(i=0;i<n;i++) cout<<endl<<a[i]; cout<<endl<<"no. of comparision is:-"<<c; getch(); }

OUTPUT:-

PROGRAM OBJECT:- Write a program to sort an array using Bubble Sort CODE:#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[20],i,j,n,s,t,c=0; cout<<"Enter size of array :- "; cin>>n; for(i=0;i<n;i++) { cout<<"enter element:-"; cin>>a[i]; } for(i=1;i<n;i++) { s=0; for(j=0;j<n-i;j++) { c++; if(a[j]>a[j+1]) { s=1; t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } if(s==0) break; } cout<<"After sorting values are:-"; for(i=0;i<n;i++) cout<<endl<<a[i]; cout<<endl<<"no. of comparision is:-"<<c; getch(); }

OUTPUT:-

PROGRAM OBJECT:- Write a program to sort an array using Selection Sort CODE:#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[20],i,j,n,min,loc,t; cout<<"Enter size of array :- "; cin>>n; for(i=0;i<n;i++) { cout<<"enter element:-"; cin>>a[i]; } for(i=0;i<n-1;i++) { min=a[i]; loc=i; for(j=i+1;j<n;j++) { if(min>a[j]) { min=a[j]; loc=j; } } if(i!=loc) { t=a[i]; a[i]=a[loc]; a[loc]=t; } } cout<<"After sorting values are:-"; for(i=0;i<n;i++) cout<<endl<<a[i]; getch(); }

OUTPUT:-

PROGRAM OBJECT:- Write a program to search a number using Linear Search. CODE:#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[20],b[20],i,j=0,n,m,c=0; cout<<"Enter size of array :- "; cin>>n; for(i=0;i<n;i++) { cout<<"\nEnter Element:-"; cin>>a[i]; } cout<<"Enter number to be searched :- "; cin>>m; for(i=0;i<n;i++) { if(a[i]==m) { b[j++]=i+1; c=1; } } if(c>0) { cout<<"\nNumber found at location :- "; for(i=0;i<j;i++) cout<<b[i]<<" "; } else cout<<"\nNumber not found "; getch(); }

OUTPUT:-

PROGRAM

OBJECT:- Write a program to search a number using Linear Search. CODE:#include<iostream.h> #include<conio.h> void main() { clrscr(); int a[20],i,n,m,mid,min,max; cout<<"Enter size of array :- "; cin>>n; for(i=0;i<n;i++) { cout<<"Enter element:- "; cin>>a[i]; } cout<<"Enter number to be searched :- "; cin>>m; min=0,max=n-1; mid=(min+max)/2; while(min<=max && a[mid]!=m) { if(a[mid]<m) min=mid+1; else max=mid-1; mid=(min+max)/2; } if(min<=max) cout<<"\nNumber found at location :- "<<mid+1; else cout<<"\nNumber not found"; getch(); }

OUTPUT:-

INDEX S NO. PRACTICAL NAME DATE OF SUBMISSION SIGNATURE

You might also like