Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

Bubble:

#include<iostream>

using namespace std;

int main ()

int i, j,temp;

int a[10] = {10,2,0,14,43,25,18,1,5,45};

cout <<"Input list ...\n";

for(i = 0; i<10; i++) {

cout <<a[i]<<"\t";

cout<<endl;

for(i = 0; i<10; i++) {

for(j =0; j<(10-i-1); j++)

if(a[j] > a[j+1]) {

temp = a[j];

a[j] = a[j+1];

a[j+1] = temp;

cout <<"Sorted Element List ...\n";

for(i = 0; i<10; i++) {

cout <<a[i]<<"\t";

return 0;

}
Selction:

#include<iostream>

using namespace std;

int main ()

int i, j,temp;

int arr[10] = {10,2,0,14,43,25,18,1,5,45};

cout <<"Input list ...\n";

for(i = 0; i<10; i++) {

cout <<arr[i]<<"\t";

cout<<endl;

for (int i = 1; i < 9; i++) {

int min = i;

for (int j = i + 1; j < 10; j++) {

if (arr[j] < arr[min]) {

min = j;

if (min != i) {

temp = arr[min];

arr[min] = arr[i];

arr[i] = temp;

cout<<"Sorted Element List ...\n";

for(i = 0; i<10; i++) {


cout <<arr[i]<<"\t";

return 0;

Insertion

#include<iostream>

using namespace std;

int main ()

int i, j,key;

int arr[10] = {10,2,0,14,43,25,18,1,5,45};

cout <<"Input list ...\n";

for(i = 0; i<10; i++) {

cout <<arr[i]<<"\t";

cout<<endl;

for (int i = 1; i<10; i++) {

key = arr[i]; //Picking the element

j = i - 1;

while (j>= 0 && arr[j]>key) {

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

}
cout<<"Sorted Element List ...\n";

for(i = 0; i<10; i++) {

cout <<arr[i]<<"\t";

return 0;

Quicck:

#include <iostream>

using namespace std;

// quick sort sorting algorithm

int Partition(int arr[], int s, int e)

int pivot = arr[e];

int pIndex =s;

for(int i = s;i<e;i++)

if(arr[i]<pivot)

int temp = arr[i];

arr[i] = arr[pIndex];

arr[pIndex] = temp;

pIndex++;

int temp = arr[e];


arr[e] = arr[pIndex];

arr[pIndex] = temp;

return pIndex;

void QuickSort(int arr[], int s, int e)

if(s<e)

int p = Partition(arr,s, e);

QuickSort(arr, s, (p-1)); // recursive QS call for left partition

QuickSort(arr, (p+1), e); // recursive QS call for right partition

int main()

int size=0;

cout<<"Enter Size of array: "<<endl;

cin>>size;

int myarray[size];

cout<<"Enter "<<size<<" integers in any order: "<<endl;

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;

QuickSort(myarray,0,(size-1)); // quick sort called

cout<<"After Sorting"<<endl;

for(int i=0;i<size;i++)

cout<<myarray[i]<<" ";

return 0;

Merge

#include <iostream>

using namespace std;

void merge(int arr[], int l, int m, int r, int size)

int i = l;

int j = m + 1;

int k = l;

/* create temp array */

int temp[size];
while (i <= m && j <= r) {

if (arr[i] <= arr[j]) {

temp[k] = arr[i];

i++;

k++;

else {

temp[k] = arr[j];

j++;

k++;

/* Copy the remaining elements of first half, if there are any */

while (i <= m) {

temp[k] = arr[i];

i++;

k++;

/* Copy the remaining elements of second half, if there are any */

while (j <= r) {

temp[k] = arr[j];

j++;

k++;

/* Copy the temp array to original array */

for (int p = l; p <= r; p++) {


arr[p] = temp[p];

/* l is for left index and r is

right index of the

sub-array of arr to be sorted */

void mergeSort(int arr[], int l, int r, int size)

if (l < r) {

// find midpoint

int m = (l + r) / 2;

/* recurcive mergesort first

and second halves */

mergeSort(arr, l, m, size);

mergeSort(arr, m + 1, r, size);

// merge

merge(arr, l, m, r, size);

int main()

cout << "Enter size of array: " << endl;

int size;

cin >> size;

int myarray[size];
cout << "Enter " << size << " integers in any order: " << endl;

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;

mergeSort(myarray, 0, (size - 1), size); // mergesort(arr,left,right) called

cout << "After Sorting" << endl;

for (int i = 0; i < size; i++) {

cout << myarray[i] << " ";

return 0;

Heap

#include<iostream>

using namespace std;

void heapify(int arr[], int n, int i) {

int temp;

int largest = i;

int l = 2 * i + 1;

int r = 2 * i + 2;

if (l < n && arr[l] > arr[largest])


largest = l;

if (r < n && arr[r] > arr[largest])

largest = r;

if (largest != i) {

temp = arr[i];

arr[i] = arr[largest];

arr[largest] = temp;

heapify(arr, n, largest);

void heapSort(int arr[], int n) {

int temp;

for (int i = n / 2 - 1; i >= 0; i--)

heapify(arr, n, i);

for (int i = n - 1; i >= 0; 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<<"Given array is: "<<endl;

for (i = 0; i<n-1; i++)

cout<<arr[i]<<" ";

cout<<endl;
heapSort(arr, n);

printf("\nSorted array is: \n");

for (i = 0; i < n; ++i)

cout<<arr[i]<<" ";

Linear

#include <iostream>

using namespace std;

void linearSearch(int a[], int n) {

int temp = -1;

for (int i = 0; i < 5; i++) {

if (a[i] == n) {

cout << "Element found at position: " << i + 1 << endl;

temp = 0;

break;

if (temp == -1) {

cout << "No Element Found" << endl;

int main() {
int arr[5];

cout << "Please enter 5 elements of the Array" << endl;

for (int i = 0; i < 5; i++) {

cin >> arr[i];

cout << "Please enter an element to search" << endl;

int num;

cin >> num;

linearSearch(arr, num);

return 0;

Binary:

#include <iostream >

using namespace std;

int binarySearch(int arr[], int left, int right, int x) {

while (left <= right) {

int mid = left + (right - left) / 2;

if (arr[mid] == x) {

return mid;

} else if (arr[mid] < x) {

left = mid + 1;

} else {

right = mid - 1;

}
}

return -1;

int main() {

int myarr[5];

int num;

int output;

cout << "Please enter 10 elements ASCENDING order" << endl;

for (int i = 0; i < 5; i++) {

cin >> myarr[i];

cout << "Please enter an element to search" << endl;

cin >> num;

output = binarySearch(myarr, 0, 5, num);

if (output == -1) {

cout << "No Match Found" << endl;

} else {

cout << "Match found at position: " << output << endl;

return 0;

Doubly:
#include<iostream>

using namespace std;

struct Node

public:

int info;

Node* next;

Node*prev;

};

class List:public Node

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;

Node *temp=new Node;

cout<<"\nEnter an Element:";

cin>>n;
temp->info=n;

temp->prev=NULL;

temp->next=NULL;

cout<<"\nINSERT AS\n1:FIRSTNODE\n2:LASTNODE\n3:IN BETWEEN FIRST&LAST NODES";

cout<<"\nEnter Your Choice:";

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:

cout<<"\nEnter the Position to Insert:";

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

cout<<"\nNot Able to Insert";

break;

void List::delet()

Node *p=NULL,*cur=first;

int count=1,pos,ch;

cout<<"\nDELETE\n1:FIRSTNODE\n2:LASTNODE\n3:IN BETWEEN FIRST&LAST NODES";

cout<<"\nEnter Your Choice:";

cin>>ch;

switch(ch)

case 1:

if(first!=NULL)

cout<<"\nDeleted Element is "<<first->info;

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)

cout<<"\nDeleted Element is: "<<cur->info;

p->next=NULL;

cur->prev=NULL;

last=prev;

else

cout<<"\nNot Able to Delete";

break;

case 3:

cout<<"\nEnter the Position of Deletion:";

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

cout<<"\nNot Able to Delete";

break;

void List::search()

int value,pos=0;

if(first==NULL)

cout<<"List is Empty";

return;

cout<<"Enter the Value to be Searched:";

cin>>value;

Node *temp;

temp=first;

while(temp!=NULL)

pos++;

if(temp->info==value)

cout<<"Element"<<value<<"is Found at "<<pos<<" Position";


return;

temp=temp->next;

cout<<"Element "<<value<<" not Found in the List";

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<<"\n**** MENU ****";

cout<<"\n1:CREATE\n2:INSERT\n3:DELETE\n4:SEARCH\n5:DISPLAY\n6:EXIT\n";

cout<<"\nEnter Your Choice:";

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;
}

You might also like