Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deepcodr patch 2 #10

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions bubblesort_templates.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include<iostream>
using namespace std;

//templates in cpp.
//bubble sort template in cpp.

template <typename T>
void bubblesort(T *x,int count)
{
register int a,b;//using register for faster execution.
T t;

for(a=1;a<count;a++)
{
for(b=count-1;b>=a;b--)
{
if(x[b-1]>x[b])
{
//exchange elements
t=x[b-1];
x[b-1]=x[b];
x[b]=t;
}
}
}
};

int main()
{
int arrayi[7]={3,4,2,1,6,5,7};
double arrayd[7]={3.4,4.6,2.2,-1,6.8,5.1,7.9};

int i;
cout<<"unsorted integer array is :"<<endl;
for(i=0;i<7;i++)
{
cout<<arrayi[i]<<' ';
}
cout<<endl;

cout<<"unsorted double array is :"<<endl;
for(i=0;i<7;i++)
{
cout<<arrayd[i]<<' ';
}
cout<<endl;

bubblesort(arrayi,7);
bubblesort(arrayd,7);

cout<<"sorted integer array is :"<<endl;
for(i=0;i<7;i++)
{
cout<<arrayi[i]<<' ';
}
cout<<endl;

cout<<"sorted double array is :"<<endl;
for(i=0;i<7;i++)
{
cout<<arrayd[i]<<' ';
}
cout<<endl;
}
100 changes: 100 additions & 0 deletions queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include<stdio.h>
//queue
static int front=-1;
static int rear=-1;

void enque(int queue[],int size)
{
if(rear==size-1)
{
printf("Queue Overflow\n");
return;
}
else if(front==-1)
{
int item;
printf("Enter item\n");
scanf("%d",&item);

front++;
rear++;
queue[rear]=item;
printf("%d inserted successfully\n",item);
}
else
{
int item;
printf("Enter item\n");
scanf("%d",&item);

rear++;
queue[rear]=item;
printf("%d inserted successfully\n",item);
}
return;
}

void deque(int queue[])
{
if(front==-1 || front>rear)
{
printf("Queue Underflow\n");
return;
}
else
{
int item=queue[front];
front++;
printf("%d deque successful\n",item);
}
return;
}

void display(int queue[])
{
if(front==-1)
{
printf("Queue Empty\n");
return;
}
for(int i=front;i<=rear;i++)
{
printf("%d\t",queue[i]);
}
printf("\n");
}

int main()
{
int size;

printf("Enter size of queue\n");
scanf("%d",&size);

int queue[size];

int ch;
do{
printf("Enter Your choice\n");
printf("1.Enque\n2.Deque\n3.Display\n4.Exit\n");
scanf("%d",&ch);

switch(ch)
{
case 1:
enque(queue,size);
break;
case 2:
deque(queue);
break;
case 3:
display(queue);
break;
case 4:
break;
default:
printf("Enter a valid choice\n");
}
}while(ch!=4);
return 0;
}