Skip to content

Instantly share code, notes, and snippets.

@amankharwal
Created March 20, 2021 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amankharwal/59eefd75d2a50faf25f41efcc9700f1f to your computer and use it in GitHub Desktop.
Save amankharwal/59eefd75d2a50faf25f41efcc9700f1f to your computer and use it in GitHub Desktop.
#include<iostream>
using namespace std;
void countsort(int arr[], int n){
int k = arr[0];
for(int i = 0; i < n; i++){
k = max(k, arr[i]);
}
int count[k] = {0};
for(int i = 0; i < n; i++){
count[arr[i]]++;
}
for(int i = 1; i <= k; i++){
count[i]+=count[i-1];
}
int output[n];
for(int i=n-1; i>=0; i--){
output[--count[arr[i]]] = arr[i];
}
for(int i=0; i<n; i++){
arr[i] = output[i];
}
}
int main(){
int arr[] = {1, 3, 2, 3, 4, 1, 6, 4, 3};
countsort(arr, 9);
for(int i=0; i<9; i++){
cout<<arr[i]<<" ";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment