+ 1

Output of counting sort far from what I expected

This is a counting sort algorithm. I was expecting it to output an ascending order array but it outputs something else. I've been working for these for the past 30 minutes and I'm really confused. Can someone hint me how I should fix this? Is there something wrong with my code? PS: This is just a snippet, so it really doesn't work here https://code.sololearn.com/cKwTwJTYdsQm/?ref=app

2nd Mar 2020, 1:57 PM
かんでん
かんでん - avatar
6 Respostas
+ 1
if you sort int numbers, change core like this: //... int loc = 0, tindex=-1; // added tindex for(int j: temp) { tindex++; // added if(j > 0) { for(int k = 0; k < j; k++) { //arr[loc] = index(temp, j)+1; arr[loc] = tindex; //temp[index(temp, j)-1] = -1; loc++; } } } //... or use for(int j =0; j<temp.length; j++) instead, then tindex= j
3rd Mar 2020, 2:25 AM
zemiak
+ 1
tindex is a position in temp, j is only value at this position it works if you sort int numbers, in this case it corresponds with value in output array. if you sort something other eg chars, you need to get value for output array different way, eg for chars it is (char) tindex.
3rd Mar 2020, 3:11 AM
zemiak
+ 1
zemiak I got it working now, thank you so much!
3rd Mar 2020, 4:09 AM
かんでん
かんでん - avatar
0
~ swim ~ I am confident that there is something wrong with the countingSort method. I'll try to post the full code later. Thanks
2nd Mar 2020, 11:36 PM
かんでん
かんでん - avatar
0
zemiak I'm sorry, what does the "tindex" do in that case?
3rd Mar 2020, 2:47 AM
かんでん
かんでん - avatar