Last active
April 22, 2018 09:32
-
-
Save Darkborderman/de9d1b51338d0067982889d05b42a70d to your computer and use it in GitHub Desktop.
A counting sort algorithm
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
#include<iostream> | |
using namespace std; | |
void countingSort(int* data,int size,int value); | |
int main() | |
{ | |
int input[10]={1,6,5,0,7,2,9,3,4,8}; | |
int size=10,value=9; | |
countingSort(input,size,value); | |
for(int i=0;i<=size-1;i++) cout<<input[i]<<" "; | |
return 0; | |
} | |
void countingSort(int* data,int size,int value) | |
{ | |
int counter[value+1],result[size]; | |
for(int i=0;i<=value;i++) counter[i]=0; | |
for(int i=0;i<=size-1;i++) counter[data[i]]++; | |
for(int i=1;i<=value;i++) counter[i]=counter[i]+counter[i-1]; | |
for(int i=size-1;i>=0;i--) | |
{ | |
result[counter[data[i]]-1]=data[i]; | |
counter[data[i]]--; | |
} | |
for(int i=0;i<=size-1;i++) data[i]=result[i]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment