Counting Sort in C++

ecom

Senior member
Feb 25, 2009
479
0
0
I need to write a counting sort using c++ and I have put it here:
http://codepad.org/MpTugOur

Everything works, but counting sort is really slow. My program has been running for almost 12 hours and hasn't gone half way through.

Specifically, my counting sort starts with line 564. My main program file is here: http://codepad.org/qI5yLfxV

The instructor was us to run the program with 4 million in the command line argument. Specifically, the program seems to be taking longest doing counting sort using 4 million.

I am not to modify the main.cpp file. Only the sorting file is modified in the assignment. Did I write the counting sort correctly?
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
A couple of things I see.

1. You are using functions very frequently for values that don't change. Don't do that. For example. 576, count begin shouldn't change at all in that loop, so why go out and find out what it is every iteration?

2. I'm not sure that you are implementing the counting sort right. you should only have to go through the data once to do a counting sort right, you are doing it several more times. Check your algorithm, make sure you are doing it right.