Skip to content
Closed
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
35 changes: 35 additions & 0 deletions Sorting Algorithms/Bubble Sort/bubble_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//Repeatedly swap two adjacent elements if they are in wrong order.
// For n elements of an array, it takes (n-1) iterations to complete. So, for i^th iteration, it will be (n-i).
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the number of elements in an array: ";
cin>>n;
int array[n];
cout<<"Enter the elements of the array:"<<endl;
for(int i=0;i<n;i++)
{
cin>>array[i];
}
int counter=1;
while(counter<n-1)
{
for(int i=0;i<n-1;i++)
{
if(array[i]>array[i+1])
{
int temp= array[i];
array[i]=array[i+1];
array[i+1]=temp;
}
}
counter++;
}
for(int i=0;i<n;i++)
{
cout<<array[i]<<" ";
}
return 0;
}
Binary file not shown.
35 changes: 35 additions & 0 deletions Sorting Algorithms/Insertion Sort/insertion_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//Insert an element from unsorted array to its correct position in the sorted array.
#include<iostream>
using namespace std;
int main()
{
int n;
// cout<<"Enter the number of elements of array: ";
cin>>n;
int array[n];
// cout<<"Enter the elements of the array:"<<endl;
for(int i=0;i<n;i++)
{
cin>>array[i];
}
int count=0;
for(int i=1;i<n;i++)
{
// count=0;
int j=i-1;
int current=array[i];
while(array[j]>current && j>=0)
{
array[j+1]=array[j];
j--;
count++;
}
array[j+1]=current;
}
// for(int i=0;i<n;i++)
// {
// cout<<array[i]<<" ";
// }
cout<<count;
return 0;
}
86 changes: 86 additions & 0 deletions Sorting Algorithms/Merge Sort/merge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include<iostream>
using namespace std;
void merge(int array[],int l,int mid,int r)
{
int n1=mid+1-l;
int n2=r-mid;

int a[n1];
int b[n2];
// temporary arrays.

for(int i=0;i<n1;i++)
{
a[i]=array[l+i];
}
for(int i=0;i<n2;i++)
{
b[i]=array[mid+1+i];
}

int i=0;int j=0; int k=l;
while(i<n1 and j<n2)
{
if(a[i]<b[j])
{
array[k]=a[i];
i++;
k++;
}
else
{
array[k]=b[j];
j++;
k++;
}
}
while(i<n1)
{
array[k]=a[i];
i++;
k++;
}
while(j<n2)
{
array[k]=b[j];
k++;
j++;
}
}
void mergesort(int array[],int l,int r)
{
if(l<r)
{
int mid=(l+r)/2;
mergesort(array,l,mid);
mergesort(array,mid+1,r);

merge(array,l,mid,r);
}
}
int main()
{
int n;
cout<<"Enter the size of array: ";
cin>>n;
int array[n];
cout<<"Enter the elements of array: ";
for(int i=0;i<n;i++)
{
cin>>array[i];
}
for(int i=0;i<n;i++)
{
cout<<array[i]<<" ";
}
cout<<endl<<endl;
mergesort(array,0,n-1);
cout<<"The sorted array is: ";
for(int i=0;i<n;i++)
{
cout<<array[i]<<" ";
}
cout<<endl;

return 0;
}
Binary file not shown.
30 changes: 30 additions & 0 deletions Sorting Algorithms/Selection Sort/selection_sorting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//Repeatedly swap two elements if they are in wrong order.
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int array[n];
for(int i=0;i<n;i++)
{
cin>>array[i];
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(array[i]>array[j])
{
int temp=array[j];
array[j]=array[i];
array[i]=temp;
}
}
}
for(int i=0;i<n;i++)
{
cout<<array[i]<<" ";
}
return 0;
}
Binary file added Sorting Algorithms/Selection Sort/sorting.exe
Binary file not shown.