Skip to content
This repository was archived by the owner on Oct 16, 2021. It is now read-only.
Open
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
38 changes: 38 additions & 0 deletions CountSort/count_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#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[10]={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;
}
2 changes: 2 additions & 0 deletions CountSort/count_sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Count Sort Algorithm
Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values, then doing some arithmetic to calculate the position of each object in the output sequence.