diff --git a/Bucket Sort.cpp b/Bucket Sort.cpp new file mode 100644 index 0000000..b46dae5 --- /dev/null +++ b/Bucket Sort.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +void Insertionsort(int a[],int n) +{ + for(int i=1;i=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] << " "; +} \ No newline at end of file diff --git a/Counting Sort.cpp b/Counting Sort.cpp new file mode 100644 index 0000000..db721ca --- /dev/null +++ b/Counting Sort.cpp @@ -0,0 +1,36 @@ +#include +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] << " "; +} diff --git a/Radix Sort.cpp b/Radix Sort.cpp new file mode 100644 index 0000000..7c8732a --- /dev/null +++ b/Radix Sort.cpp @@ -0,0 +1,51 @@ +#include +#include +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> 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] << " "; +}