Skip to content
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
34 changes: 34 additions & 0 deletions Algorithms/sorting/BubbleSorting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<iostream.h>
#include<conio.h>
void bubble_sorting(int [],int);
void main()
{
int a[50];
int n;
cout<<"enter how many numbers you will be sort";
cin>>n;
cout<<"enter the values\n";
for(int i=0;i<n;i++)
cin>>a[i];
bubble_sorting(a,n);
double b[5]={5.8,6.8,9.3,2.6,4.5};
bubble_sorting(b,n);
cout<<"the sorted array is:-\n ";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}

template <class x> void bubble_sorting(x *a,int size)
{
int round,i;
x temp;
for(round=0;round<size;round++)
for(i=0;i<(size-1)-round;i++)
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
69 changes: 69 additions & 0 deletions Data Structures/Tree/krushkal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include<stdio.h>
#include<conio.h>
int root[10],flag=0,count=0,temp,min;
int a[20],cost[20][20],n,i,j,k,totalcost=0,x,y;
void find_min(),check_cycle(),update();
void main()
{
clrscr();
printf("Enter the number of vertices please\n");
scanf("%d",&n);
printf("Enter the cost of the matrix please\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf ("%d",&cost[i][j]);
find_min();
while (min!=999 && count!=n-1)
{
check_cycle();
if (flag)
{
printf ("%d ---> %d = %d\n",x,y,cost[x][y]);
totalcost+=cost[x][y];
update();
count++;
}
cost[x][y]=cost[y][x]=999;
find_min();
}
if (count<n - 2)
printf("The graph is not connected\n");
else
printf("The graph is connected & the min cost is %d\n",totalcost);
getch();
}
void check_cycle()
{
if ((root[x]==root[y])&&(root[x]!=0))
flag=0;
else
flag=1;
}
void find_min ()
{
min = 999;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(min>cost[i][j])
{
min = cost[i][j];
x = i;
y = j;
}
}
void update()
{
if(root[x]==0 && root[y]==0)
root[x]=root[y]=x;
else if(root[x]==0)
root[x]=root[y];
else if(root[y]==0)
root[y]=root[x];
else
{
temp = root[y];
for(i=1;i<=n;i++)
if(root[i]==temp)
root[i]=root[x];
}
}