Skip to content

Commit

Permalink
Merge pull request #5 from 7phalange7/master
Browse files Browse the repository at this point in the history
Order n sorting algorithms
  • Loading branch information
supershivam13 authored Oct 3, 2020
2 parents 9b9279b + a039b67 commit 77beeaa
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
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] << " ";
}

0 comments on commit 77beeaa

Please sign in to comment.