Skip to content

Added all Sorting Algorithm #10

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

Open
wants to merge 10 commits into
base: master
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
79 changes: 79 additions & 0 deletions sorting/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//InPlace-Algorithm.......



//Bubble Sort
//This algorithm is used to swap two adjacent elements one by one untill and unless we get the sorted array.
//Stable

#include<bits/stdc++.h>
using namespace std;

//Time Complexity is O(n**2)........

void bubble_sort(int *a,int n)
{
for(int i=0;i<n-1;i++)
{
int temp=true;
for(int j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1]){
swap(a[j],a[j+1]);
temp=false;
}
}
if(temp)
break;
}

}
//Selection sort
//This Algorthim is used to find the min index at every iteration and swap the current element with the minimum element.
//Unstable
//Time Complexity is O(n**2)...........

void selection_sort(int *a,int n)
{
for(int i=0;i<n-1;i++)
{
int min_index=i;
for(int j=i+1;j<n;j++)
{
if(a[min_index]>a[j])
min_index=j;
}
swap(a[i],a[min_index]);
}
}
//Insertion sort
//This algortith is used to place the current element at correct position and then it will place all lesser element before the current element.
//stable........

//Time Complexity is O(n**2)......
void insertion_sort(int *a,int n)
{
for(int i=1;i<n;i++)
{
int temp=a[i],j;
for(j=i-1;j>=0 && a[j]>temp;j--)
a[j+1]=a[j];
a[j+1]=temp;
}
}
int main()
{
int a[]={5,4,3,2,1};
bubble_sort(a,5);
for(int i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl;
selection_sort(a,5);
for(int i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl;
insertion_sort(a,5);
for(int i=0;i<5;i++)
cout<<a[i]<<" ";
return 0;
}
83 changes: 83 additions & 0 deletions sorting/10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//Counting sort.........

//This algorthim is used to sort the elements which are in range of [1,k].....

//We will first count the frequenecy of all elements in the another array.
//Now,We will find the cummulative frequency of all elements then we will place the correct element at correct position...

//Not a Comparison based Algorithm........

//Time Complexity is Theta(n+k)......

//Auxiliary space is Theta(n+k).....

//Stable



#include<bits/stdc++.h>
using namespace std;

//Method 1
//Naive-Implementation.....

//Time Complexity is O(n+k)...................

void count_sort_1(int *a,int n,int k)
{
int temp[k]={0};
int index=0;
for(int i=0;i<n;i++)
{
temp[a[i]]++;
}
for(int i=0;i<k;i++)
{
for(int j=0;j<temp[i];j++)
{
a[index]=i;
index++;
}
}
for(int i=0;i<n;i++)
cout << a[i] <<" ";
}

//Method 2
void counting_sort(int *a,int n,int k)
{
int count[k];
for(int i=0;i<k;i++)
{
count[i]=0;
}
for(int i=0;i<n;i++)
{
count[a[i]]++;
}
for(int i=1;i<k;i++)
{
count[i]=count[i-1]+count[i];
}
int output[n];
for(int i=n-1;i>=0;i--)
{
output[count[a[i]-1]]=a[i];
count[a[i]]--;
}
for(int i=0;i<n;i++)
{
a[i]=output[i];
}
for(int i=0;i<n;i++)
cout << output[i] <<" ";
}

int main()
{
int a[]={1,4,4,1,0,1};
//count_sort_1(a,6,5);
//cout<<endl;
counting_sort(a,6,5);
return 0;
}
53 changes: 53 additions & 0 deletions sorting/11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//Bucket sort
//This algorithm is used when the data is uniformaly distributed.
//we will divide all the elements into K buckets.
//But we have to find the maximum element in original array to get the bucket index.Once we get the Bucket index,we will push the values into bucket array or vector.
//We sort the Bucket vector then we will place the sorted elements into original array...
//Time Complexity

//Data is uniformly distributed....
//best case is O(n).....

//When data is not uniformly distributed....
//worst case O(n**2)..........

#include<bits/stdc++.h>
using namespace std;

void bucket_sort(int *a,int total_bucket,int n)
{
int m=a[0];
for(int i=1;i<n;i++)
m=max(m,a[i]);
m=m+1;
vector<int>buckets[total_bucket];
for(int i=0;i<n;i++)
{
int bucket_index=a[i]*total_bucket/m;
buckets[bucket_index].push_back(a[i]);

}
for(int i=0;i<total_bucket;i++)
{
sort(buckets[i].begin(),buckets[i].end());

}
//Finally concatination
int index=0;
for(int i=0;i<total_bucket;i++)
{
for(int j=0;j<buckets[i].size();j++)
{
a[index++]=buckets[i][j];
}
}

}
int main()
{
int a[]={30,40,10,80,5,12,70};
bucket_sort(a,4,7);
for(int i=0;i<7;i++)
cout<<a[i]<<" ";
return 0;
}
59 changes: 59 additions & 0 deletions sorting/12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include<bits/stdc++.h>
using namespace std;
//Radix sort
//This algorthim is the extended version of counting sort because the data may be is in the range of 1 to k**2,that's why we use this algorithm insteed of using Counting sort.
//In this algorithm we will sort the array acc.to digits.
//We will first sort it acc to least significant digit ,then sort it acc to middle digit and finally sort it acc to most significant digit.
//The whole logic is similar to counting sort......

//Time Complexity is Theta(d*(n+b))........
//b is the base
//here b=10;
//Auxiliary space is Theta(n+b)..........

//counting-sort
void counting_sort(int *a,int n,int exp)
{
int count[10]={0};
for(int i=0;i<n;i++)
{
count[(a[i]/exp)%10]++;
}
for(int i=1;i<10;i++)
{
count[i]+=count[i-1];
}
int output[n];
for(int i=n-1;i>=0;i--)
{
output[count[(a[i]/exp)%10]-1]=a[i];
count[(a[i]/exp)%10]--;
}
for(int i=0;i<n;i++)
{
a[i]=output[i];
}
}
void radix_sort(int *a,int n)
{
int m=a[0];
for(int i=1;i<n;i++)
m=max(m,a[i]);

//Run loop through number of digits in a maximum number .......

for(int exp=1;m/exp>0;exp=exp*10)
{
counting_sort(a,n,exp);
}
for(int i=0;i<n;i++)
{
cout<<a[i]<<" ";
}
}
int main()
{
int a[]={319,212,6,8,100,50};
radix_sort(a,6);
return 0;
}
62 changes: 62 additions & 0 deletions sorting/13.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//Find the Kth smallest Element.....

#include<bits/stdc++.h>
using namespace std;

//Method1...........
//Time Complexity is O(n*log(n)).........

int find_k_element(int *a,int n,int k)
{
return a[k-1];
}

//Method2............
int find_pivot(int *a,int l,int h)
{
int k=l-1;
int pivot=a[h];
for(int i=l;i<h;i++)
{
if(a[i] < pivot)
{
k++;
swap(a[k],a[i]);
}
}
swap(a[k+1],a[h]);
return k+1;
}
int find_pos_k(int *a,int n,int k)
{
int low=0;
int high=n-1;
while(low<=high)
{

int p=find_pivot(a,low,high);
if(p==k-1)
return p;
if(k-1 > p)
{
low=p+1;
}
else if(k-1 < p)
{
high=p-1;
}
}
return -1;
}

int main()
{
int a[] = { 51, 86, 34, 79, 92, 68, 14, 47, 22, 6};
sort(a,a+10);
cout << find_k_element(a,5,2) << endl;
for(int i=0;i<10;i++)
{
cout << a[find_pos_k(a,10,i+1)]<<" ";
}
return 0;
}
23 changes: 23 additions & 0 deletions sorting/14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//Chocolate Distribution Problem......

#include<bits/stdc++.h>
using namespace std;

//Time complexity is O(n*log(n))......

int difference_chocolate(int *a,int n,int m)
{
int ans=INT_MAX;
for(int i=0;i<=n-m;i++)
{
ans=min(ans,a[m+i-1]-a[i]);
}
return ans;
}
int main()
{
int a[]={3,4,1,9,56,7,9,12};
sort(a,a+8);
cout << difference_chocolate(a,8,5);
return 0;
}
Loading