Sort help

Need some help with Bin Sort/ Radix sort.
I know how the algorithm works now but I wonder why it doesn't work in my code. It gave me an error in the third count for-loop. and I am not sure what is wrong with it. Also, is Radix sort are different from Bin sort? In class, I was taught with the Bin loop. I understand the concept but I was lost when the instructor uses the front and back pointer and decided to do my own study. It seems this code is similar and much easier to understand.
Any help will be appreciated. Thanks!
my input from a file:
2
9
3
5
10
7
3
9
3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//get max 
int getMax(int a[], int size) {
	int max = arr[0];
	for (int i = 1; i < size; i++) {
		if (a[i] > max){
			max = a[i];
		return max;
}
//bin sort
void binSort(int a[], int size) {
	int max = getMax(a, size);
	for (int pos = 1; max / pos > 0; pos=10) {
		countSort(a, size, pos);
	}
}
Last edited on
Line 7: This is allocating a single int. Use new int [size].
Line 30: This line is not a part of your if statement block. Misleading indentation. Of course, the return statement shouldn't be there either way.
Last edited on
@Ganado I change line 7 and deleted line 6 also fix the indentation on line 30.
It still gave me the same result..

I check the debuger and it showing that I was reading all the value and accounted it in the correct arr in countSort.
+ count 0x00eff488 {1, 0, 1, 3, 1, 1, 0, 1, 0, 2} int[10] when i=10

after that it seems to loop back to i =0 and this is where I believe I start to loose the data.

+ count 0x00eff488 {-858993459, -858993459, -858993458, -858993455, -858993454, -858993453, 0, 1, 0, 2} int[10] when i=5

it does not seems it went to the last for loop to actually copy the data to the arr.
I'm not sure if I'm reading the debugger correctly.
getMax is still always returning a result after the very first iteration. That is definitely part of the problem, but I make no guarantee it's the complete problem.
Last edited on
Ganadothank you I fix all the problems!
Last edited on
Topic archived. No new replies allowed.