forked from rachitiitr/DataStructures-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from 7phalange7/master
Order n sorting algorithms
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] << " "; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] << " "; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] << " "; | ||
} |