Professional Documents
Culture Documents
Bubble
Bubble
#include<iostream>
int main ()
int i, j,temp;
cout <<a[i]<<"\t";
cout<<endl;
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
cout <<a[i]<<"\t";
return 0;
}
Selction:
#include<iostream>
int main ()
int i, j,temp;
cout <<arr[i]<<"\t";
cout<<endl;
int min = i;
min = j;
if (min != i) {
temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
return 0;
Insertion
#include<iostream>
int main ()
int i, j,key;
cout <<arr[i]<<"\t";
cout<<endl;
j = i - 1;
arr[j + 1] = arr[j];
j = j - 1;
arr[j + 1] = key;
}
cout<<"Sorted Element List ...\n";
cout <<arr[i]<<"\t";
return 0;
Quicck:
#include <iostream>
for(int i = s;i<e;i++)
if(arr[i]<pivot)
arr[i] = arr[pIndex];
arr[pIndex] = temp;
pIndex++;
arr[pIndex] = temp;
return pIndex;
if(s<e)
int main()
int size=0;
cin>>size;
int myarray[size];
for(int i=0;i<size;i++)
cin>>myarray[i];
}
cout<<"Before Sorting"<<endl;
for(int i=0;i<size;i++)
cout<<myarray[i]<<" ";
cout<<endl;
cout<<"After Sorting"<<endl;
for(int i=0;i<size;i++)
cout<<myarray[i]<<" ";
return 0;
Merge
#include <iostream>
int i = l;
int j = m + 1;
int k = l;
int temp[size];
while (i <= m && j <= r) {
temp[k] = arr[i];
i++;
k++;
else {
temp[k] = arr[j];
j++;
k++;
while (i <= m) {
temp[k] = arr[i];
i++;
k++;
while (j <= r) {
temp[k] = arr[j];
j++;
k++;
if (l < r) {
// find midpoint
int m = (l + r) / 2;
mergeSort(arr, l, m, size);
mergeSort(arr, m + 1, r, size);
// merge
merge(arr, l, m, r, size);
int main()
int size;
int myarray[size];
cout << "Enter " << size << " integers in any order: " << endl;
return 0;
Heap
#include<iostream>
int temp;
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
largest = r;
if (largest != i) {
temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
int temp;
heapify(arr, n, i);
temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
int main() {
int arr[] = { 20, 7, 1, 54, 10, 15, 90, 23, 77, 25};
int n = 10;
int i;
cout<<arr[i]<<" ";
cout<<endl;
heapSort(arr, n);
cout<<arr[i]<<" ";
Linear
#include <iostream>
if (a[i] == n) {
temp = 0;
break;
if (temp == -1) {
int main() {
int arr[5];
int num;
linearSearch(arr, num);
return 0;
Binary:
if (arr[mid] == x) {
return mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
int main() {
int myarr[5];
int num;
int output;
if (output == -1) {
} else {
cout << "Match found at position: " << output << endl;
return 0;
Doubly:
#include<iostream>
struct Node
public:
int info;
Node* next;
Node*prev;
};
Node *first,*last;
public:
List()
first=NULL;
last=NULL;
void create();
void insert();
void delet();
void display();
void search();
};
void List::create()
Node *temp;
temp=new Node;
int n;
cout<<"\nEnter an Element:";
cin>>n;
temp->info=n;
temp->prev=NULL;
temp->next=NULL;
if(first==NULL)
first=temp;
last=first;
else
last->next=temp;
temp->prev=last;
last=temp;
void List::insert()
Node *p,*cur;
p=NULL;
cur=first;
int count=1,pos,ch,n;
cout<<"\nEnter an Element:";
cin>>n;
temp->info=n;
temp->prev=NULL;
temp->next=NULL;
cin>>ch;
switch(ch)
case 1:
temp->next=first;
temp->prev=NULL;
first=prev=temp;
first=temp;
break;
case 2:
last->next=temp;
temp->prev=last;
last=temp;
break;
case 3:
cin>>pos;
while(count!=pos)
p=cur;
cur=cur->next;
count++;
if(count==pos)
{
p->next=temp;
temp->prev=p;
cur->prev=temp;
temp->next=cur;
else
break;
void List::delet()
Node *p=NULL,*cur=first;
int count=1,pos,ch;
cin>>ch;
switch(ch)
case 1:
if(first!=NULL)
first=first->next;
first->prev=NULL;
else
cout<<"\nNot Able to Delete";
break;
case 2:
while(cur!=last)
p=cur;
cur=cur->next;
if(cur==last)
p->next=NULL;
cur->prev=NULL;
last=prev;
else
break;
case 3:
cin>>pos;
while(count!=pos)
p=cur;
cur=cur->next;
count++;
if(count==pos)
{
cout<<"\nDeleted Element is: "<<cur->info;
p->next=cur->next;
cur=cur->next;
cur->prev=p;
else
break;
void List::search()
int value,pos=0;
if(first==NULL)
cout<<"List is Empty";
return;
cin>>value;
Node *temp;
temp=first;
while(temp!=NULL)
pos++;
if(temp->info==value)
temp=temp->next;
void List::display()
Node *temp=first;
if(temp==NULL)
cout<<"\nList is Empty";
while(temp!=NULL)
cout<<temp->info;
cout<<"<-->";
temp=temp->next;
cout<<"NULL";
int main()
List l;
int ch;
while(1)
cout<<"\n1:CREATE\n2:INSERT\n3:DELETE\n4:SEARCH\n5:DISPLAY\n6:EXIT\n";
cin>>ch;
switch(ch)
case 1:
l.create();
break;
case 2:
l.insert();
break;
case 3:
l.delet();
break;
case 4:
l.search();
break;
case 5:
l.display();
break;
case 6:
return 0;
return 0;
}