Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions Sorting algorithms/BUBBLESO.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,a[50],i,j,temp;
printf("enter number of elements that you want to store:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j]) //if you change the relational operator u will get desceding order
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("acending order of the elements is:");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
53 changes: 53 additions & 0 deletions Sorting algorithms/QUICKSOR.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include<stdio.h>
#include<conio.h>
void quicksort(int[],int,int);
void main()
{
int a[50],n,i;
clrscr();
printf("enter number of elemnts that u want to store:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
quicksort(a,0,n-1);
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
void quicksort(int a[],int low,int high)
{
int pivot,i,j,temp;
if(low<high)
{
pivot=low;
i=low;
j=high;
while(i<j)
{
while(a[i]<=a[pivot] && i<=high)
{
i++;
}
while(a[j]>a[pivot] && j>=low)
{
j--;
}
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[j];
a[j]=a[pivot];
a[pivot]=temp;
quicksort(a,low,j-1);
quicksort(a,j+1,high);
}
}

34 changes: 34 additions & 0 deletions Sorting algorithms/SELECTIO.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,j,min,temp;
printf("enter number of elements that u want to store:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[min]>a[j])
{
min=j;
}
}
if(min!=i)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
getch();
}
65 changes: 65 additions & 0 deletions Sorting algorithms/SHELLSOR.CPP
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include<IOSTREAM.H>
#include<conio.h>
class shellsort
{
private:
int a[20],n,i,j,gap,temp;
public:
void getdata()
{
cout<<"enter number of elemnts:\n";
cin>>n;
cout<<"enter the elements:\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
}
void shellsorting()
{

for(gap=n/2;gap>0;gap=gap/2)
{
for(j=gap;j<n;j++)
{
for(i=j-gap;i>=0;i-=gap)
{
if(a[i+gap]>a[i])
{
break;
}
else
{
swap();
}
}
}
}
}
void swap()
{
temp=a[i+gap];
a[i+gap]=a[i];
a[i]=temp;

}
void display()
{
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
}
};
void main()
{
clrscr();
shellsort obj;
obj.getdata();
cout<<"before sorting:";
obj.display();
obj.shellsorting();
cout<<endl<<"after sorting:";
obj.display();
getch();
}