Posts

Showing posts with the label Searching And Sorting

Heap Sort

Image
#include<iostream> #include<conio.h> #define max 50 using namespace std; int array[max]; int add(int x[],int size) { for(int i=2;i<=size;i++) { int child=i; int parent=i/2; while(array[child]>array[parent]&&child>1)            //while child node is greater then parent node                                                                                                // and child is not root node. { int temp=array[child]; //Swapping of parent and child if child is greater then its                                 array[child]=array[parent];       //parent node. array...

Binary Search Tree Insertion and Traversing by Pre_Order and In_Order

Image
#include<iostream> #include<conio.h> using namespace std; struct node { int info; node *left,*right; }*root=NULL,*stack[50]; int size=0; int insert() { size++; int x; cout<<"Enter info part: "; cin>>x; node *temp=new node; temp->info=x; int n=2;     if(root==NULL) {    root=temp; temp->left=NULL; temp->right=NULL; return 1; } node *ptr=new node; node *ctr=new node; ctr=NULL; ptr=root; while(ptr!=NULL) { if(ptr->info>x) { ptr=ptr->left; } else if(ptr->info==x) {   cout<<"Item already in list!\n"; int x=0; return x; } else { ptr=ptr->right; } } ptr=root; while(ptr!=NULL) { if(ptr->info>x) { ctr=ptr; ptr=ptr->left; } else { ctr=ptr; ptr=ptr->right; } } if(ctr->info>x) { ctr->left=temp; temp->left=NULL; temp->right=NULL; } el...

Warshall Transitive Clousre

Image
#include<iostream> #include<conio.h> using namespace std; int main() { int a[4][4],i,j,k,r[4][4]; cout<<"Enter element of A matrix: \n"; for(i=0;i<4;i++) { for(j=0;j<4;j++) cin>>a[i][j]; cout<<endl; } cout<<"Matrix You enterd : \n"; for(i=0;i<4;i++) { for(j=0;j<4;j++) cout<<"\t"<<a[i][j]; cout<<endl; } cout<<"\nTransitive clousre :\n"; cout<<"R(0): \n"; for(i=0;i<4;i++) { for(j=0;j<4;j++) cout<<"\t"<<a[i][j]; cout<<endl; } for(i=0;i<4;i++) for(j=0;j<4;j++) r[i][j]=a[i][j]; for(k=0;k<4;k++) { for(i=0;i<4;i++) { for(j=0;j<4;j++) { r[i][j]=r[i][j]||(r[i][k]&&r[k][j]); } } cout<<"R("<<k+1<<")\n"; for(i=0;i<4;i++) { for(j=0;j<4;j++) cout<<"\t"...

Comparison Counting Sort

Image
#include<conio.h> #include<iostream> #define max 50 using namespace std; int main() { int a[6],count[max],s[6]; cout<<"Enter 5 oderable values: \n"; for(int i=0;i<6;i++) cin>>a[i]; for(int i=0;i<6;i++) count[i]=0; for(int i=0;i<5;i++) for(int j=i+1;j<6;j++) { if(a[i]<a[j]) count[j]=count[j]+1; else count[i]=count[i]+1; } for(int i=0;i<6;i++) s[count[i]]=a[i]; cout<<"Shorted array is : "; for(int i=0;i<6;i++) cout<<"\t"<<s[i]; getch(); return 0; } Output How it works:-

Insertion Sort

Image
#include<iostream> #include<conio.h> #define max 50 using namespace std; int main() { int a[max]; cout<<"How many elements you want to enter: "; int num,v,j; cin>>num; cout<<"Enter Elements one by one: \n"; for(int i=0;i<num;i++) cin>>a[i]; for(int i=0;i<num;i++) { v=a[i]; j=i-1; while (j>=0 && a[j]>v) { a[j+1]=a[j]; j=j-1; } a[j+1]=v; } cout<<"List after Sorting: "; for(int i=0;i<num;i++) cout<<"  "<<a[i]; getch(); return 0; } Output: How it work:- 89 | 45 68  90  29  34  17 45  89 | 68  90  29  34  17 45  68  89 | 90  29  34  17 45  68  89  90 | 29  34  17 29  45  68  89  90 | 34  17 29  34  45  68  89  90 | 17 17  29  34  45  68  89  90

Burte Force String Match

Image
#include<iostream> #include<conio.h> #include<string.h> using namespace std; int main() { char text[]={"SmartProgrammerOn"},strn[11]; int t,s; cout<<"Given Data is : \t"<<text; cout<<"\nWhat you want search(max size 10): "; cin>>strn; t=strlen(text); s=strlen(strn); int i,j=0; for(i=0;i<t;i++) { j=0; while (j<s&&strn[j]==text[i+j]) { j=j+1; if (j==s) { cout<<"String Match at location "<<i+1; return 0; } } } cout<<"No result found."; getch(); } OutPut:

Sequential Search

Image
#include<iostream> #include<conio.h> using namespace std; int main() { int a[10]={67,50,34,68,34,78,5,78,56,12},item,i; cout<<"Given Data is : "; for(i=0;i<10;i++) cout<<"  "<<a[i]; cout<<"\nEnter item you want to search : "; cin>>item; i=0; while (a[i]!=item) i++; if (i<10) cout<<"Item found at location "<<i+1; else cout<<"Item not found."; getch(); return 0; } Output:

Bubble Sort

Image
#include<iostream> #include<conio.h> using namespace std; int main() { int a[10]; cout<<"Enter 10 Oderable intergers: \n"; for(int i=0;i<=9;i++) cin>>a[i]; cout<<"List after sorted: "; for(int i=0;i<=9;i++) for(int j=0;j<=9;j++) if(a[j]>a[j+1]) { int temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } for(int i=0;i<=9;i++) cout<<"  "<<a[i]; getch(); return 0; } Output:

Selection Sort

Image
#include<iostream> #include<conio.h> using namespace std; int main() { int a[10],min; cout<<"\t\t\tSelection Sort"; cout<<"\nEnter 10 Oderable integers: \n"; for(int i=0;i<10;i++) cin>>a[i]; for(int i=0;i<10-1;i++) { min=i; for(int j=i+1;j<10;j++) { if(a[j]<a[min]) { min=j; } } if(min!=i) { int temp=a[i]; a[i]=a[min]; a[min]=temp; } } cout<<"Shorted List is: "; for(int i=0;i<10;i++) cout<<"  "<<a[i]; getch(); return 0; } Output:

Linear Search

Image
#include<iostream> #include<conio.h> using namespace std; int main() { int a[10],n; cout<<"Enter elements \n"; for(int i=0;i<10;i++) { cout<<"\t\t"; cin>>a[i]; } cout<<"Enter element you to search: "; cin>>n; for(int i=0;i<10;i++) if(a[i]==n) { cout<<"Element found"; return 0; } cout<<"\t\t\t\tNot found"; getch(); return 0; }

Wharshall's Path

Image
#include<iostream> #include<conio.h> #define INFINITY ((1 << (8*sizeof (int) - 6)) - 4) using namespace std; int main() { int a[4][4],b[4][4]; int i,j,k; cout<<"Enter value of Adjacent Matrix (row wise) :\n"; for(i=0;i<4;i++) for(j=0;j<4;j++) {cout<<"\t"; cin>>a[i][j]; } for(i=0;i<4;i++) for(j=0;j<4;j++) {if(a[i][j]==0) b[i][j]=INFINITY; else b[i][j]=a[i][j]; } for(k=0;k<4;k++) { for(i=0;i<4;i++) { for(j=0;j<4;j++) { if(b[i][j]>(b[i][k]+b[k][j])) b[i][j]=(b[i][k]+b[k][j]); } } } cout<<"\n\nWharshall's Path\n"; for(i=0;i<4;i++) { for(j=0;j<4;j++) cout<<b[i][j]<<"\t"; cout<<"\n"; } getch(); return 0; }

Quick Sort

Image
#include<iostream> #include<conio.h> using namespace std; void quickSort(int arr[], int left, int right) {   int l = left, r = right;   int a;   int pivot = arr[((left + right) / 2)];   while (l <= r) {         while (arr[l] < pivot)               l++;         while (arr[r] > pivot)               r--;         if (l<=r) {               a = arr[l];               arr[l] = arr[r];               arr[r] = a;               l++;               r--;     } } if (left<r)     quickSort(arr, left, r); if (l< right)         quickSort(arr, l, right); } int main() {   ...

Binary Search

Image
#include<iostream> #include<conio.h> using namespace std; int main() { int search[50],num,left,right,middle,item; cout<<"Enter number of elements : "; cin>>num; cout<<"\nEnter Elements in SORTED ORDER: "; for(int i=1;i<=num;i++) { cin>>search[i]; cout<<"\t\t\t\t"; } cout<<"\nEnter Item you want to search : "; cin>>item; left=1; right=num; if(item<search[left] || item>search[right]) { cout<<"Item not found."; return 0; } middle=(left+right)/2;                     while(left<=right && search[middle]!=item)   { if(search[middle]<item) left=middle+1; else right=middle-1; middle=(left+right)/2; } if(search[middle]==item) { cout<<"\n"<<item<<" is Found at location : "<<middle; } else { cout<<"Item is not found...