Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (1)
- Linear Search
- #include<iostream.h>
- #include<conio.h>
- int LinearSearch(int a[],int n,int data);
- void main()
- {
- clrscr();
- int a[50],i,n,d,flag;
- cout<<"Enter the number of elements ";
- cin>>n;
- cout<<"Enter the elements";
- for(i=0;i<n;i++)
- {
- cin>>a[i];
- }
- cout<<"Enter the element to be searched";
- cin>>d;
- flag=LinearSearch(a,n,d);
- if(flag==-1)
- cout<<"Element Not Found";
- else
- cout<<"Element found at location"<<flag+1;
- getch();
- }
- int LinearSearch(int a[],int n,int d)
- {
- int i;
- for(i=0;i<n;i++)
- {
- if(a[i]==d)
- return i;
- }
- return -1;
- }
- (2)
- Binary Search
- #include<iostream.h>
- #include<conio.h>
- int BinarySearch(int a[],int n,int d)
- {
- int b=0,e=n-1,m;
- while(b<=e)
- {
- m=(b+e)/2;
- if(a[m]==d)
- return m;
- else
- if(a[m]<d)
- b=m+1;
- else
- e=m-1;
- }
- return 0;
- }
- void main()
- {
- clrscr();
- int a[50],i,n,d,flag;
- cout<<"Enter the number of elements ";
- cin>>n;
- cout<<"Enter the elements";
- for(i=0;i<n;i++)
- {
- cin>>a[i];
- }
- cout<<"Enter the element to be searched";
- cin>>d;
- flag=BinarySearch(a,n,d);
- if(flag==0)
- cout<<"Element Not Found";
- else
- cout<<"Element found at location"<<flag+1;
- getch();
- }
- (3)
- Insertion Based on Location
- #include<iostream.h>
- #include<conio.h>
- void insert(int a[],int n,int d,int pos)
- {
- int i;
- for(i=n-1;i>=pos-1;i--)
- {
- a[i+1]=a[i];
- }
- a[pos-1]=d;
- }
- void main()
- {
- clrscr();
- int a[50],i,n,d,pos;
- cout<<"Enter the number of elements ";
- cin>>n;
- cout<<"Enter the elements";
- for(i=0;i<n;i++)
- {
- cin>>a[i];
- }
- cout<<"Enter the element to be inserted and its location";
- cin>>d>>pos;
- insert(a,n,d,pos);
- cout<<"After Insertion";
- for(i=0;i<=n;i++)
- {
- cout<<a[i]<<",";
- }
- getch();
- }
- (4)
- Insertion Based on value
- #include<iostream.h>
- #include<conio.h>
- void insert(int a[],int n,int d)
- {
- int i,pos=0;
- if(d>=a[n-1])
- a[n]=d;
- else
- {
- while(a[pos]<=d)
- pos++;
- for(i=n-1;i>=pos-1;i--)
- {
- a[i+1]=a[i];
- }
- a[pos]=d;
- }
- }
- void main()
- {
- clrscr();
- int a[50],i,n,d;
- cout<<"Enter the number of elements ";
- cin>>n;
- cout<<"Enter the elements";
- for(i=0;i<n;i++)
- {
- cin>>a[i];
- }
- cout<<"Enter the element to be inserted";
- cin>>d;
- insert(a,n,d);
- cout<<"After Insertion";
- for(i=0;i<=n;i++)
- {
- cout<<a[i]<<",";
- }
- getch();
- }
- (5)
- Deletion Based on Value
- #include<iostream.h>
- #include<conio.h>
- int del(int a[],int n,int data)
- {
- int i,j;
- for(i=0;i<n;i++)
- {
- if(a[i]==data)
- {
- for(j=i+1;j<n;j++)
- a[j-1]=a[j];
- return 1;
- }
- }
- return -1;
- }
- void main()
- {
- clrscr();
- int a[50],i,n,element,f;
- cout<<"Enter the number of elements ";
- cin>>n;
- cout<<"Enter the elements";
- for(i=0;i<n;i++)
- {
- cin>>a[i];
- }
- cout<<"Enter the elementto be deleted";
- cin>>element;
- f=del(a,n,element);
- if(f==1)
- {
- cout<<"After Deletion";
- for(i=0;i<n-1;i++)
- {
- cout<<a[i]<<",";
- }
- }
- else
- cout<<"Element Not Found";
- getch();
- }
- (6)
- Deletion based on position
- #include<iostream.h>
- #include<conio.h>
- void del(int a[],int n,int pos)
- {
- int i;
- for(i=pos;i<n;i++)
- {
- a[i-1]=a[i];
- }
- }
- void main()
- {
- clrscr();
- int a[50],i,n,pos;
- cout<<"Enter the number of elements ";
- cin>>n;
- cout<<"Enter the elements";
- for(i=0;i<n;i++)
- {
- cin>>a[i];
- }
- cout<<"Enter the postion of deletion";
- cin>>pos;
- del(a,n,pos);
- cout<<"After Deletion";
- for(i=0;i<n-1;i++)
- {
- cout<<a[i]<<",";
- }
- getch();
- }
- (7)
- Selection Sorting
- #include<iostream.h>
- #include<conio.h>
- void SelectionSort(int a[],int n)
- {
- int min,pos,i,j;
- for(i=0;i<=n-1;i++)
- {
- min=a[i];
- pos=i;
- for(j=i+1;j<n;j++)
- {
- if(a[j]<min)
- {
- min=a[j];
- pos=j;
- }
- }
- int temp;
- temp=a[i];
- a[i]=a[pos];
- a[pos]=temp;
- }
- }
- void main()
- {
- clrscr();
- int a[50],i,n,d,pos;
- cout<<"Enter the number of elements ";
- cin>>n;
- cout<<"Enter the elements";
- for(i=0;i<n;i++)
- {
- cin>>a[i];
- }
- SelectionSort(a,n);
- cout<<"Ascending Order";
- for(i=0;i<n;i++)
- {
- cout<<a[i]<<",";
- }
- getch();
- }
- (8)
- Bubble Sort
- #include<iostream.h>
- #include<conio.h>
- void BubbleSort(int a[],int n)
- {
- int i,j,temp;
- for(i=0;i<=n;i++)
- {
- for(j=0;j<(n-1)-i;j++)
- {
- if(a[j]>a[j+1])
- {
- temp=a[j];
- a[j]=a[j+1];
- a[j+1]=temp;
- }
- }
- }
- }
- void main()
- {
- clrscr();
- int a[50],i,n,d,pos;
- cout<<"Enter the number of elements ";
- cin>>n;
- cout<<"Enter the elements";
- for(i=0;i<n;i++)
- {
- cin>>a[i];
- }
- BubbleSort(a,n);
- cout<<"Ascending Order";
- for(i=0;i<n;i++)
- {
- cout<<a[i]<<",";
- }
- getch();
- }
- (9)
- Insertion Sorting
- #include<iostream.h>
- #include<conio.h>
- void InsertionSort(int a[],int n)
- {
- int i,j,t;
- for(i=1;i<n;i++)
- {
- t=a[i];
- j=i-1;
- while(t<a[j]&&j>=0)
- {
- a[j+1]=a[j];
- j--;
- }
- a[j+1]=t;
- }
- }
- void main()
- {
- clrscr();
- int a[50],i,n,d,pos;
- cout<<"Enter the number of elements ";
- cin>>n;
- cout<<"Enter the elements";
- for(i=0;i<n;i++)
- {
- cin>>a[i];
- }
- InsertionSort(a,n);
- cout<<"Ascending Order";
- for(i=0;i<n;i++)
- {
- cout<<a[i]<<",";
- }
- getch();
- }
- (10)
- Merging
- #include<iostream.h>
- #include<conio.h>
- void Merge(int a[],int m,int b[],int n,int c[])
- {
- int p1,p2,p3;
- p1=p2=p3=0;
- while(p1<m&&p2<n)
- {
- if(a[p1]>b[p2])
- c[p3++]=b[p2++];
- else
- c[p3++]=a[p1++];
- }
- while(p1<m)
- c[p3++]=a[p1++];
- while(p2<n)
- c[p3++]=b[p2++];
- }
- void main()
- {
- clrscr();
- int a[50],b[50],c[100],i,n,m;
- cout<<"Enter the number of elements for two arrays";
- cin>>m>>n;
- cout<<"Enter the elements in 1st array";
- for(i=0;i<m;i++)
- {
- cin>>a[i];
- }
- cout<<"Enter the elements in 2nd array";
- for(i=0;i<n;i++)
- {
- cin>>b[i];
- }
- Merge(a,m,b,n,c);
- cout<<"Element in new array are";
- for(i=0;i<(m+n);i++)
- {
- cout<<c[i]<<",";
- }
- getch();
- }
- (11)
- Concatenation
- #include<iostream.h>
- #include<conio.h>
- void Concatenation(int a[],int m,int b[],int n,int c[])
- {
- int p=0,i=0,j=0;
- while(i<m)
- c[p++]=a[i++];
- while(j<n)
- c[p++]=b[j++];
- }
- void main()
- {
- clrscr();
- int a[50],b[50],c[100],i,n,m;
- cout<<"Enter the number of elements for two arrays";
- cin>>m>>n;
- cout<<"Enter the elements in 1st array";
- for(i=0;i<m;i++)
- {
- cin>>a[i];
- }
- cout<<"Enter the elements in 2nd array";
- for(i=0;i<n;i++)
- {
- cin>>b[i];
- }
- Concatenation(a,m,b,n,c);
- cout<<"Element in new array are";
- for(i=0;i<(m+n);i++)
- {
- cout<<c[i]<<",";
- }
- getch();
- }
- (12)
- Program to add n integer numbers
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int sum=0,n;
- cout<<"Enter how many no.";
- cin>>n;
- for(int x=1;x<=n;x++)
- {
- sum=sum+x;
- }
- cout<<"TOTAL is "<<sum;
- getch();
- }
- (13)
- Program to print pyramid
- /*1
- 12
- 123
- 1234 */
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int n;
- cout<<"enter how many rows";
- cin>>n;
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=i;j++)
- cout<<j;
- cout<<endl;
- }
- getch();
- }
- (14)
- Program to find maximum in an array
- #include<iostream.h>
- #include<conio.h>
- int maximum(int n,int w[]);
- void main()
- {
- clrscr();
- int b[10],n,max;
- cout<<"ENTER HOW MANY ELEMENTS ";
- cin>>n;
- cout<<"ENTER VALUES ";
- for(int i=0;i<n;i++)
- cin>>b[i];
- max=maximum(n,b);
- cout<<"MAXIMUM IS "<<max;
- getch();
- }
- int maximum(int n,int w[])
- {
- int max=w[0];
- for(int i=0;i<n-1;i++)
- if(w[i+1]>max)
- max=w[i+1];
- return max;
- }
- (15)
- Program to find SUM of an array using pointer
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- int *p,sum=0;
- int x[5]={4,10,7,5,12};
- p=x;
- for(int i=0;i<5;i++)
- {
- sum = sum + *p;
- p++;
- }
- cout<<sum;
- getch();
- }
- (16)
- Program for Bubble Sorting through pointers
- #include<iostream.h>
- #include<conio.h>
- void main()
- {
- clrscr();
- short int x[5]={10,20,5,3,123};
- short int *p,*q,temp;
- for(int i=0;i<5;i++)
- {
- p=x;
- q=x+1;
- for(int j=0;j<4-i;j++)
- {
- if((*p)>(*q))
- {
- temp=*p;
- *p=*q;
- *q=temp;
- }
- p++;
- q++;
- }
- }
- for(i=0;i<5;i++)
- cout<<x[i]<<endl;
- getch();
- }
- (17)
- Program to print array values through pointer
- #include<iostream>h>
- #include<conio>h>
- #include<dos>h> /*for sleep function*/
- void main()
- {
- clrscr();
- short int array[5]={10,3,96,32767,45},*p;
- p=array;
- for(int i=0;i<5;i++)
- {
- gotoxy(40,10+i); /*location of answer*/
- sleep(1); /* one second break*/
- cout<<*p<<endl;
- p++;
- }
- getch();
- }
- (18)
- Program to sort strings using bubble sort method
- #include<iostream.h>
- #include<conio.h>
- #include<string.h>
- void main()
- {
- char x[5][10]={"harish","Harish","HARISH","hARISH","HaRIsh"};
- cout<<"Before Sorting ->";
- for(int i=0;i<5;i++)
- { gotoxy(20,1+i);
- cout<<x[i]; }
- char b[10];
- for( i=1;i<5;i++)
- {
- for(int j=0;j<5-i;j++)
- {
- if((strcmp(x[j],x[j+1]))>0)
- {
- strcpy(b,x[j+1]);
- strcpy(x[j+1],x[j]);
- strcpy(x[j],b);
- }
- }
- }
- gotoxy(40,1);
- cout<<"After Sorting ->";
- for( i=0;i<5;i++) /*capital letters ascii code comes before
- small letters ascii code */
- { gotoxy(60,1+i);
- cout<<x[i]; }
- getch();
- }
- (19)
- Quick Sorting
- #include<iostream.h>
- #include<conio.h>
- #include<stdlib.h>
- #define size 10
- void quick(int a[size],int,int);
- int partition(int a[size],int,int);
- void swap(int a[size],int *,int *);
- int n;
- int main()
- {
- int i,a[size];
- clrscr();
- cout<<"Quick Sort Method";
- cout<<"Enter total numbers to sort";
- cin>>n;
- cout<<"Enter the elements";
- for(i=0;i<n;i++)
- {
- cin>>a[i] ;
- }
- quick(a,0,n-1);
- cout<<"Sorted Array";
- for(i=0;i<n;i++)
- {
- cout<<"\t"<<a[i];
- }
- getch();
- return 0;
- }
- //This function is to sort the elements in a sublist
- void quick(int a[size],int low,int high)
- {
- int m,i;
- if(low<high)
- {
- m=partition(a,low,high);
- quick(a,low,m-1);
- quick(a,m+1,high);
- }
- }
- //This function is to partition a list and decide the pivot element
- int partition(int a[size],int low,int high)
- {
- int pivot=a[low],i=low,j=high;
- while(i<=j)
- {
- while(a[i]<=pivot)
- i++;
- while(a[j]>pivot)
- j--;
- if(i<j)
- swap(a,&i,&j);
- }
- swap(a,&low,&j);
- return j;
- }
- void swap(int a[size],int *i,int *j)
- {
- int temp;
- temp=a[*i];
- a[*i]=a[*j];
- a[*j]=temp;
- }
- (20)
- Merge Sort
- #include<iostream.h>
- #include<conio.h>
- #include<stdlib.h>
- int n;
- void main()
- {
- int i,low,high;
- int a[5];
- void mergesort(int a[5],int low,int high);
- void display(int a[5]);
- clrscr();
- cout<<"Merge Sort\n";
- cout<<"Enter the length of list";
- cin>>n;
- cout<<"Enter list Elements";
- for(i=0;i<n;i++)
- {
- cin>>a[i];
- }
- low=0;
- high=n-1;
- mergesort(a,low,high);
- display(a);
- getch();
- }
- //This function is to split the list into sublists
- void mergesort(int a[5],int low,int high)
- {
- int mid;
- void combine(int a[5],int low,int mid,int high);
- if(low<high)
- {
- mid=(low+high)/2;//split the list at mid
- mergesort(a,low,mid);//first sublist
- mergesort(a,mid+1,high);
- combine(a,low,mid,high);
- }
- }
- //This function is to for merging the two sublists
- void combine(int a[5],int low,int mid,int high)
- {
- int i,j,k;
- int temp[5];
- k=low;
- i=low;
- j=mid+1;
- while(i<=mid && j<=high)
- {
- if(a[i]<=a[j])
- {
- temp[k]=a[i];
- i++;
- k++;
- }
- else
- {
- temp[k]=a[j];
- j++;
- k++;
- }
- }
- while(i<=mid)
- {
- temp[k]=a[i];
- i++;
- k++;
- }
- while(j<=high)
- {
- temp[k]=a[j];
- j++;
- k++;
- }
- //copy the elements from temp array to a
- for(k=low;k<=high;k++)
- {
- a[k]=temp[k];
- }
- }
- //Function to display sorted array
- void display(int a[5])
- {
- int i;
- cout<<"The Sorted array is";
- for(i=0;i<n;i++)
- cout<<a[i]<<"\t";
- }
- (21)
- Radix Sort
- #include<iostream.h>
- #include<conio.h>
- #include<math.h>
- void main()
- {
- int a[100][100],r=0,c=0,i,sz,b[50],temp;
- clrscr();
- cout<<"Enter Size of Array";
- cin>>sz;
- for(r=0;r<100;r++)
- {
- for(c=0;c<100;c++)
- {
- a[r][c]=1000;
- }
- }
- for(i=0;i<sz;i++)
- {
- cout<<"Enter Element";
- cin>>b[i];
- r=b[i]/100;
- c=b[i]%100;
- a[r][c]=b[i];
- }
- for(r=0;r<100;r++)
- {
- for(c=0;c<100;c++)
- {
- for(i=0;i<sz;i++)
- {
- if(a[r][c]==b[i])
- {
- cout<<a[r][c]<<"\t";
- }
- }
- }
- }
- getch();
- }
- (22)
- Program to find sum of 10 elements
- #include<iostream.h>
- #include<conio.h>
- void add(int a[]);
- void main()
- {
- clrscr();
- int a[10],i;
- cout<<"enter elements";
- cout<<"\n\n";
- for(i=0;i<10;i++)
- {
- cout<<"\n";
- cin>>a[i];
- }
- add(a);
- getch();
- }
- void add(int a[])
- {
- int sum=0,i;
- for(i=0;i<10;i++)
- {
- sum+=a[i];
- }
- cout<<"\n\n";
- cout<<"sum is"<<sum;
- }
- (23)
- Program to increase value of each element by 10
- #include<iostream.h>
- #include<conio.h>
- void inc(int a[]);
- void main()
- {
- clrscr();
- int a[10],i;
- cout<<"enter elements";
- cout<<"\n\n";
- for(i=0;i<10;i++)
- {
- cout<<"\n\n";
- cin>>a[i];
- }
- inc(a);
- getch();
- }
- void inc(int a[])
- {
- int i;
- for(i=0;i<10;i++)
- {
- a[i]=a[i]+10;
- }
- cout<<"\n\n";
- cout<<"new values are";
- cout<<"\n\n";
- for(i=0;i<10;i++)
- {
- cout<<a[i]<<"\t";
- }
- }
- (24)
- Program to count numbers greater than 5
- #include<iostream.h>
- #include<conio.h>
- int count(int a[],int n)
- {
- int count=0,i;
- for(i=0;i<n;i++)
- {
- if(a[i]>5)
- {
- count++;
- }
- }
- return(count);
- }
- void main()
- {
- clrscr();
- int a[10],n,i;
- cout<<"enter size";
- cout<<"\n\n";
- cin>>n;
- cout<<"\n\n";
- cout<<"enter elements";
- cout<<"\n\n";
- for(i=0;i<n;i++)
- {
- cout<<"\n";
- cin>>a[i];
- }
- cout<<"\n\n";
- cout<<"nos. greater than 5 are"<<"\t"<<count(a,n);
- getch();
- }
Advertisement
Add Comment
Please, Sign In to add comment