Vous êtes sur la page 1sur 35

ASSIGNMENT -1

Insertion Sort
#include <iostream.h> #include <conio.h>

void main( ) { int size=6; int array[size]= {84,36, 69, 22,94, 91}; int i, j, key; for (j=1; j<size; j++) { key= array[j]; for (i=j-1; (i>=0) && (array[i]<key); i++) { array[i+1]= array[i]; } array[i+1]=key; } for (i = 0; i < size - 1; i++) { cout <<array[i]; } getch(); }

Selection Sort
#include <iostream.h> #include <conio.h> void main( ) { int n=6; int arr[n]= {84,36, 69, 22,94, 91}; int i, j, minIndex, tmp; for (i = 0; i < n - 1; i++) { minIndex = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[minIndex]) minIndex = j; if (minIndex != i) { tmp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = tmp; } } for (i = 0; i < n - 1; i++) { cout <<arr[i]; } getch(); }

Fibonacci Sequence

#include <iostream.h> #include <conio.h>

void main( ) { int n, c, first = 0, second = 1, next; cout << "Enter the number of terms of Fibonacci series you want" << endl; cin >> n; cout << "First " << n << " terms of Fibonacci series are :- " << endl; for ( c = 0 ; c < n ; c++ ) { if ( c <= 1 ) next = c; else { next = first + second; first = second; second = next; } cout << next << endl; } getch(); }

2) Using recursive method-

#include <iostream.h> #include <conio.h>

int Fibonacci(int nNumber) { cout<< nNumber; if (nNumber == 0) return 0; if (nNumber == 1) return 1; return Fibonacci(nNumber-1) + Fibonacci(nNumber-2); }

void main( ) { int n, c, first = 0, second = 1, next; cout << "Enter the number of terms of Fibonacci series you want" << endl; cin >> n; cout << "First " << n << " terms of Fibonacci series are :- " << endl; Fibonacci(n); getch(); }

Quick Sort
void quickSort(int arr[], int left, int right) { int i = left, j = right; int tmp; int pivot = arr[(left + right) / 2];

/* partition */ while (i <= j) { while (arr[i] < pivot) i++; while (arr[j] > pivot) j--; if (i <= j) { tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; i++; j--; } } /* recursion */

if (left < j) quickSort(arr, left, j); if (i < right) quickSort(arr, i, right); }

Tower of Hanoi

#include <stdio.h> #include <conio.h>

void move ( int, char, char, char ) ;

void main( ) { int n = 3 ; clrscr( ) ; move ( n, 'A', 'B', 'C' ) ; getch( ) ; }

void move ( int n, char sp, char ap, char ep ) { if ( n == 1 ) printf ("\nMove from %c to %c ", sp, ep ) ; else {move ( n - 1, sp, ep, ap ) ; move ( 1, sp, ' ', ep ) ; move ( n - 1, ap, sp, ep ) ; }}

merge sort

#include<stdio.h> void getdata(int arr[],int n) { int i; Cout<<enter the data; for(i=0;i<n;i++) { Cin>>arr[i]; } } void display(int arr[],int n) { int i; Cout<<" "; for(i=0;i<n;i++) { cout<<arr[i] } getchar(); } void sort(int arr[],int low,int mid,int high) { int i,j,k,l,b[20]; l=low; i=low; j=mid+1;

while((l<=mid)&&(j<=high){ if(arr[l]<=arr[j]) { b[i]=arr[l]; l++; } else { b[i]=arr[j]; j++; } i++; } if(l>mid) { for(k=j;k<=high;k++) { b[i]=arr[k]; i++; } } else { for(k=l;k<=mid;k++) { b[i]=arr[k]; i++; } } for(k=low;k<=high;k++) { arr[k]=b[k];

} } void partition(int arr[],int low,int high) { int mid; if(low<high) { mid=(low+high)/2; partition(arr,low,mid); partition(arr,mid+1,high); sort(arr,low,mid,high); } } void main() { int arr[20]; int n; cout<<"Enter number of data:"; cin>>n; getdata(arr,n); partition(arr,0,n-1); display(arr,n); getchar(); }

Heap Sort
#include <stdio.h> #include <conio.h> void makeheap ( int [ ], int ) ; void heapsort ( int [ ], int ) ; void main( ) { int arr[10] = { 11, 2, 9, 13, 57, 25, 17, 1, 90, 3 } ; int i ; clrscr( ) makeheap ( arr, 10 ) ; cout<< "\nBefore Sorting:\n"; for ( i = 0 ; i <= 9 ; i++ ) cout<< arr[i] ; heapsort ( arr, 10 ) ; cout<< "\nAfter Sorting:\n" ; for ( i = 0 ; i <= 9 ; i++ ) cout<< arr[i] ; getch( ); } void makeheap ( int x[ ], int n ) { int i, val, s, f ; for ( i = 1 ; i < n ; i++ ) { val = x[i] ;

s=i; f=(s-1)/2; while ( s > 0 && x[f] < val ) { x[s] = x[f] ; s=f; f=(s-1)/2; } x[s] = val ; } void heapsort ( int x[ ], int n ) { int i, s, f, ivalue ; for ( i = n - 1 ; i > 0 ; i-- ) { ivalue = x[i] ; x[i] = x[0] ; f=0; if ( i == 1 ) s = -1 ; else s=1; if ( i > 2 && x[2] > x[1] ) s=2; while ( s >= 0 && ivalue < x[s] ) { x[f] = x[s] ; f=s; s=2*f+1;

if ( s + 1 <= i - 1 && x[s] < x[s + 1] ) s++ ; if ( s > i - 1 ) s = -1 ; } x[f] = ivalue ; } }

Factorial of a Number
#include<iostream.h> #include<conio.h> void main() { int n,fact; int rec(int); clrscr(); cout<<"Enter the number:->"; cin>>n; fact=rec(n); cout<<endl<<"Factorial Result are:: "<<fact<<endl; getch(); } rec(int x) { int f; if(x==1) return(x); else { f=x*rec(x-1); return(f); } }

Link List
#include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }*head;

void append(int num) { struct node *temp,*right; temp= (struct node *)malloc(sizeof(struct node)); temp->data=num; right=(struct node *)head; while(right->next != NULL) right=right->next; right->next =temp; right=temp; right->next=NULL; }

void add( int num ) { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->data=num; if (head== NULL) { head=temp; head->next=NULL; } else { temp->next=head; head=temp; } } void addafter(int num, int loc) { int i; struct node *temp,*left,*right; right=head; for(i=1;i<loc;i++) {

left=right; right=right->next; } temp=(struct node *)malloc(sizeof(struct node)); temp->data=num; left->next=temp; left=temp; left->next=right; return; }

void insert(int num) { int c=0; struct node *temp; temp=head; if(temp==NULL) { add(num); } else { while(temp!=NULL) { if(temp->data<num) c++; temp=temp->next; } if(c==0) add(num); else if(c<count()) addafter(num,++c); else append(num); } }

int delete(int num) { struct node *temp, *prev; temp=head; while(temp!=NULL) { if(temp->data==num) { if(temp==head) {

head=temp->next; free(temp); return 1; } else { prev->next=temp->next; free(temp); return 1; } } else { prev=temp; temp= temp->next; } } return 0; }

void display(struct node *r) { r=head; if(r==NULL) { return; } while(r!=NULL) { printf("%d ",r->data); r=r->next; } printf("\n"); } int count() { struct node *n; int c=0; n=head; while(n!=NULL) { n=n->next; c++; } return c; }

int main() { int i,num; struct node *n; head=NULL; while(1) { Cout<<"\nList Operations\n"; Cout<<"===============\n"; Cout<<"1.Insert\n"; Cout<<"2.Display\n"; Cout<<"3.Size\n; Cout<<"4.Delete\n"; Cout<<"5.Exit\n"; Cout<<"Enter your choice : "; if((cin>>i)<=0){ Cout<<"Enter only an Integer\n"; exit(0); } else { switch(i) { case 1:

case 2:

case 3: case 4:

case 5:

Cout<<"Enter the number to insert : "; cin>>num; insert(num); break; if(head==NULL) { Cout<<"List is Empty\n"; } else { Cout<<"Element(s) in the list are : "; } display(n); break; Cout<<"Size of the list is %d\n",count(); break; if(head==NULL) Cout<<"List is Empty\n"; else{ Cout<<"Enter the number to delete : "; Cin>>num; if(delete(num)) Cout<< deleted successfully\n",num; else Cout<<" not found in the list\n",num; } break; return 0;

default: } } } return 0; }

Cout<<"Invalid option\n";

ASSIGNMENT 2
Radix Sort
#include<stdio.h> #include<conio.h> void main() { int a[10],i,j,k,l,m,n,lar=0; int n1[10],a1[10][10],mod,div=1,temp; clrscr(); for(i=0;i<10;i++) { n1[i]=0; }

Cout<<"enter the size of array:"; Cin>>n; for(i=0;i<n;i++) { Cin>>a[i]); if(a[i]>lar) lar=a[i]; } mod=1; for(i=0;lar!=0;i++) { mod=mod*10; for(j=0;j<n;j++) {

temp=(a[j]%mod)/div; a1[temp][n1[temp]++]=a[j];

} div=div*10; l=0; for(k=0;k<10;k++) { for(m=1;m<=n1[k];m++) { a[l]=a1[k][m-1] ; l++; } n1[k]=0; } lar=lar/10; } for(i=0;i<n;i++) { printf("%d ",a[i]); } }

Max and Min In a Set


#include<stdio.h> int max, min; int a[100]; void maxmin(int i, int j) { int max1, min1, mid; if(i==j) { max = min = a[i]; } else { if(i == j-1) { if(a[i] <a[j]) { max = a[j]; min = a[i]; } else { max = a[i]; min = a[j]; } } else { mid = (i+j)/2; maxmin(i, mid); max1 = max; min1 = min; maxmin(mid+1, j); if(max <max1) max = max1; if(min > min1) min = min1; } } } void main (){ int i, num; clrscr(); cout<<"\n\t\t\tMAXIMUM & MINIMUM\n\n"; cout<<"\nEnter the total number of numbers : "; cin>>num; cout<< "Enter the numbers : \n"; for (i=1;i<=num;i++) { Cin>>a[i]; }

max = a[0]; min = a[0]; maxmin(1, num); cout<<"Maximum element in an array , max; cout<<"Minimum element in an array ", min; getch(); }

LINEAR SEARCH

#include<iostream.h> #include<conio.h> void main() { int a[20],n,x,i,flag=0; cout<<"How many Elements?"; cin>>n; cout<<"\nEnter Elements of the Array\n";

for(i=0;i<n;++i) cin>>a[i]; cout<<"\nEnter Element to search:"; cin>>x; for(i=0;i<n;++i) { if(a[i]==x) { flag=1; break; } } if(flag) cout<<"\nElement is Found at position "<<i+1; else cout<<"\nElement not found"; getch(); }

BINARY SEARCH
#include<iostream.h> #include<conio.h> int bsearch(int r,int a[],int s,int e); void main() { int a[10],n,r; cout<<"enter no. of elements "; cin>>n; cout<<"\n enter elements"; for(int i=0;i<n;i++) cin>>a[i]; cout<<"\n enter element to search "; cin>>r; int p=bsearch(r,a,0,n-1); cout<<"\n position of "<<r<<" in array is "<<p; getch(); } int bsearch(int r,int a[],int s,int e) { while(s<e) { int mid=(s+e)/2; if(r==a[mid]) return mid; else if(r<a[mid]) e=mid-1; else s=mid+1; } return -1; }

BUCKET SORT
#include<iostream.h> #include<conio.h> class element { public: int value; element *next; element() { value=NULL; next=NULL; } }; class bucket { public: element *firstElement; bucket() { firstElement = NULL; } }; void main() { int lowend=0; int highend=100; int interval=10; // minimum element //max element //number of intervals //bucket containing a perticular range of values

const int noBuckets=(highend-lowend)/interval; //number of buckets required bucket *buckets=new bucket[noBuckets]; bucket *temp; for(int a=0;a<noBuckets;a++) //creation of required buckets { temp=new bucket; buckets[a]=*temp; } cout<<"The Elements to be Sorted using Bucket sort are \n"; int array[5];

for(int i=0;i<5;i++) cin>>array[i]; for(int j=0;j<5;j++) //sending elments into appropriate buckets { element *temp,*pre; temp=buckets[array[j]/interval].firstElement; if(temp==NULL)//if it is the first element of the bucket { temp=new element; buckets[array[j]/interval].firstElement=temp; temp->value=array[j]; } else { pre=NULL; while(temp!=NULL) { //move till the appropriate position in the bucket

if(temp->value>array[j]) break; pre=temp; temp=temp->next; }

if(temp->value>array[j]) //if the new value is in betwen or at the begining { if(pre==NULL) //insertion at first if the bucket has elements already { element *firstNode; firstNode=new element(); firstNode->value=array[j]; firstNode->next=temp; buckets[array[j]/interval].firstElement=firstNode; else //insertion at middle { element *firstNode; firstNode=new element(); firstNode->value=array[j]; firstNode->next=temp; pre->next=firstNode; }

} } else// if the new value is to be created at last of bucket { temp=new element; pre->next=temp; temp->value=array[j]; } } } cout<<"------------------------The Sorted Elements Are---------------\n"; for(int jk=0;jk<10;jk++) { element *temp; temp= buckets[jk].firstElement; while(temp!=NULL) { cout<<"*"<<temp->value<<endl; temp=temp->next; } } getch(); }

Kruskals Algorithm for finding minimum spanning tree.


#include<iostream.h> #include<conio.h> #include<stdlib.h> int cost[10][10],i,j,k,n,m,c,visit,visited[10],l,v,count,count1,vst,p; void main() { int dup1,dup2; cout<<"enter no of vertices"; cin >> n; cout <<"enter no of edges"; cin >>m; cout <<"EDGE Cost"; for(k=1;k<=m;k++) { cin >>i >>j >>c; cost[i][j]=c; cost[j][i]=c; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(cost[i][j]==0) cost[i][j]=31999; visit=1; while(visit<n) { v=31999; for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(cost[i][j]!=31999 && cost[i][j]<v && cost[i][j]!=-1 ) { int count =0; for(p=1;p<=n;p++) { if(visited[p]==i || visited[p]==j)

count++; } if(count >= 2) { for(p=1;p<=n;p++) if(cost[i][p]!=31999 && p!=j) dup1=p; for(p=1;p<=n;p++) if(cost[j][p]!=31999 && p!=i) dup2=p; if(cost[dup1][dup2]==-1) continue; } l=i; k=j; v=cost[i][j]; } cout <<"edge from " <<l <<"-->"<<k; cost[l][k]=-1; cost[k][l]=-1; visit++; int count=0; count1=0; for(i=1;i<=n;i++) { if(visited[i]==l) count++; if(visited[i]==k) count1++; } if(count==0) visited[++vst]=l; if(count1==0) visited[++vst]=k; } getch(); }

PRIMs ALOGORITHM
#include<iostream.h> #include<conio.h> #include<stdlib.h> int cost[10][10],i,j,k,n,stk[10],top,v,visit[10],visited[10],u;

void main() { clrscr(); int m,c; cout <<"enter no of vertices"; cin >> n; cout <<"enter no of edges"; cin >> m; cout <<"\nEDGES Cost\n"; for(k=1;k<=m;k++) { cin >>i>>j>>c; cost[i][j]=c; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) if(cost[i][j]==0) cost[i][j]=31999;

cout <<"ORDER OF VISITED VERTICES"; k=1; while(k<n) { m=31999; if(k==1)

{ for(i=1;i<=n;i++) for(j=1;j<=m;j++) if(cost[i][j]<m) { m=cost[i][j]; u=i; } } else { for(j=n;j>=1;j--) if(cost[v][j]<m && visited[j]!=1 && visit[j]!=1) { visit[j]=1; stk[top]=j; top++; m=cost[v][j]; u=j; } } cost[v][u]=31999; v=u; cout<<v << " "; k++; visit[v]=0; visited[v]=1; } getch(); }

Strassen's Matrix Multiplication

#include<iostream.h> #include<conio.h> int row,col; void show(int c[4][4]); void main() { int a[4][4],i,j,m1,m2,m3,m4,m5,m6,m7,b[4][4],c[4][4]; clrscr(); cout<<"\n\t\tSTRASSEN'S MATRIX MULTIPLICATION"; cout<<"\n\t\t---------------------------------\n"; cout<<"\n\tEnter the order of matrix:"; cin>>row; cin>>col; cout<<"\n\n\tEnter the matrix A elements:"; for(i=0;i<row;i++) for(j=0;j<col;j++) cin>>a[i][j]; //partition(a); cout<<"\n\n\tEnter the matrix B elements:"; for(i=0;i<row;i++) for(j=0;j<col;j++) cin>>b[i][j]; m1=(a[0][0]+a[1][1])*(b[0][0]+b[1][1]); m2=(a[1][0]+a[1][1])*b[0][0]; m3=a[0][0]*(b[0][1]-b[1][1]); m4=a[1][1]*(b[1][0]-b[0][0]); m5=b[1][1]*(a[0][0]+a[0][1]); m6=(a[1][0]-a[0][0])*(b[0][0]+b[0][1]);

m7=(a[0][1]-a[1][1])*(b[1][0]+b[1][1]); c[0][0]=m1+m4-m5+m7; c[0][1]=m3+m5; c[1][0]=m2+m4; c[1][1]=m1+m3-m2+m6; cout<<"\n\t\tMATRIX A:\n"; show(a); cout<<"\n\n\t\tMATRIX B:\n"; show(b); cout<<"\n\n\t\tMATRIX A*B:\n"; show(c); /* for(i=0;i<row;i++,printf("\n")) for(j=0;j<col;j++) printf("%d\t",c[i][j]);*/ getch(); } void show(int c[4][4]) { int i,j; for(i=0;i<row;i++) { for(j=0;j<col;j++) { cout<<"\t\t"<<c[i][j]; } cout<<"\n\n"; } }

Vous aimerez peut-être aussi