I'm working on an algorithm that sorts inputted integers in ascending order.
The input should be of sort
5
32
45
32
55
67
and the output of sort
32
32
45
55
67
where the first integer input is the number of integers that will be inputted.
n.b. the integer inputted n is 1 <= n <= 10,000,000.
Here is my code:
#include <stdio.h>
int main() {
int N;
scanf("%d", &N); // receives the number of integers that will be inputted
int countArray[10000000] = {0}; // creates an array that will store the number of times a number is inputted. the inputted number is the corresponding address e.g. countArray[number-1] stores the amount of times the number 'number' is inputted.
int inputArray[N]; // creates an array of the numbers inputted
for (int i=0; i<N; i++) {
scanf("%d", &inputArray[i]); // receives the numbers inputted and stores them in the inputArray
}
for (int i=0;i<N;i++) {
countArray[inputArray[i]-1]++; // counts the number of times a number occurs in inputArray and increments the corresponding value in the countArray.
}
int i=0;
while (i<10000000) {
if (countArray[i] > 0) {
for (int j=0;j<countArray[i];j++) { // prints out the numbers only if count is greater than 0.
printf("%d\n", i+1);
}
}
i++;
}
return 0;
}
I know my code isn't the most efficient way to sort the inputs; I'm just curious as to why I get a "Thread 1: EXC_BAD_ACCESS" error when the size of the array is 10,000,000 but not when it is 1,000,000. It works just fine if the array is of size 1,000,000 but not when it is 10,000,000.