Skip to content

Order n sorting algorithms #5

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

Merged
merged 1 commit into from
Oct 3, 2020
Merged
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
60 changes: 60 additions & 0 deletions Bucket Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>

using namespace std;

void Insertionsort(int a[],int n)
{
for(int i=1;i<n;i++)
{
int key = a[i];
int j=i-1;
while(j>=0 && a[j]>key)
{
a[j+1]=a[j];
j--;
}
a[j+1] = key;
}
}

void BucketSort(int a[], int n, int k)
{
int max = 0;
for (int i = 0; i < n; i++)
if (max < a[i])
max = a[i];
max++;

int bucket[k][n];
int size[k] = {0};

for (int i = 0; i < n; i++)
{
int index = k * a[i] / max;
bucket[index][size[index]] = a[i];
size[index]++;
}
for (int i = 0; i < k; i++)
{
Insertionsort(bucket[i], size[i]);
}
for (int i = 0, j = 0; j < k; j++)
{
for (int l = 0; l < size[j]; l++)
a[i++] = bucket[j][l];
}
}

int main()
{
int n;
cout << "\nEnter No of elements in the array : ";
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) //input
cin >> arr[i];
BucketSort(arr, n, 5);
cout << "\nSorted Array : ";
for (int i = 0; i < n; i++) //output
cout << arr[i] << " ";
}
36 changes: 36 additions & 0 deletions Counting Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
using namespace std;

void Countsort(int a[], int n)
{
int k = 0; //max element
for (int i = 0; i < n; i++)
if (a[i] > k)
k = a[i];
int count[k+1] = {0}, temp[n];
for (int i = 0; i < n; i++)
count[a[i]]++;
for (int i = 1; i <= k; i++)
count[i] = count[i - 1] + count[i];
for (int i = n - 1, j = k - 1; i >= 0; i--)
{
temp[count[a[i]]-1] = a[i];
count[a[i]]--;
}
for (int i = 0; i < n; i++)
a[i] = temp[i];
}

int main()
{
int n;
cout << "\nEnter No of elements in the array : ";
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)//input
cin >> arr[i];
Countsort(arr, n);
cout << "\nSorted Array : ";
for (int i = 0; i < n; i++)//output
cout << arr[i] << " ";
}
51 changes: 51 additions & 0 deletions Radix Sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <iostream>
#include <math.h>
using namespace std;

void Countsort(int a[], int n, int e)
{
int k=9;//0-9
// int k = 0; //max element
// for (int i = 0; i < n; i++)
// if (a[i] > k)
// k = a[i];
int count[k+1] = {0}, temp[n];
for (int i = 0; i < n; i++)
count[(a[i]/e)%10]++;
for (int i = 1; i <= k; i++)
count[i] = count[i - 1] + count[i];
for (int i = n - 1, j = k - 1; i >= 0; i--)
{
temp[count[(a[i]/e)%10]-1] = a[i];
count[(a[i]/e)%10]--;
}
for (int i = 0; i < n; i++)
a[i] = temp[i];
}

void Radixsort(int a[], int n)
{
int m = 0; //max element
for (int i = 0; i < n; i++)
if (a[i] > m)
m = a[i];
int d = log10(m); // no. of max digits
for(int i=0;i<d;i++)
{
Countsort(a,n,pow(10,i));
}
}

int main()
{
int n;
cout << "\nEnter No of elements in the array : ";
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)//input
cin >> arr[i];
Radixsort(arr, n);
cout << "\nSorted Array : ";
for (int i = 0; i < n; i++)//output
cout << arr[i] << " ";
}