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
53 changes: 24 additions & 29 deletions bubble_sort
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@

#include<stdio.h>
#define MAX 100
int main(void)
#include<iostream>

using namespace std;

int main()
{
int arr[MAX],i,j,temp,n,xchanges;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0; i<n; i++)
int a[50],n,i,j,temp;
cout<<"Enter the size of array: ";
cin>>n;
cout<<"Enter the array elements: ";

for(i=0;i<n;++i)
cin>>a[i];

for(i=1;i<n;++i)
{
printf("Enter element %d : ",i+1);
scanf("%d",&arr[i]);
}

for(i=0; i<n-1 ;i++)
{
xchanges = 0;
for(j=0; j<n-1-i; j++)
{
if(arr[j] > arr[j+1])
for(j=0;j<(n-i);++j)
if(a[j]>a[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
xchanges++;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
if(xchanges==0) /*If list is sorted*/
break;
}
printf("Sorted list is :\n");
for(i=0; i<n; i++)
printf("%d ",arr[i]);
printf("\n");

cout<<"Array after bubble sort:";
for(i=0;i<n;++i)
cout<<" "<<a[i];

return 0;
}
67 changes: 41 additions & 26 deletions selection_sort
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@

#include<stdio.h>
#define MAX 100
int main(void)
#include<iostream>

using namespace std;

int main()
{
int arr[MAX],i,j,k,n;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d", &arr[i]);
}

for(i=1; i<n; i++)
{
k=arr[i]; /*k is to be inserted at proper place*/
for(j=i-1; j>=0 && k<arr[j]; j--)
arr[j+1]=arr[j];
arr[j+1]=k;
}
printf("Sorted list is :\n");
for(i=0; i<n; i++)
printf("%d ",arr[i]);
printf("\n");
return 0;
}/*End of main()*/
int i,j,n,loc,temp,min,a[30];
cout<<"Enter the number of elements:";
cin>>n;
cout<<"\nEnter the elements\n";

for(i=0;i<n;i++)
{
cin>>a[i];
}

for(i=0;i<n-1;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
loc=j;
}
}

temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}

cout<<"\nSorted list is as follows\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<" ";
}

return 0;
}