0

I'm trying to take a file and run it through a sorting algorithm, insertion sort in this case. After I pass it through the algorithm and print the array, it doesn't look sorted. My thinking is because I'm trying to pass an array to the function, and view the information through a pointer, but I think I'm messing it up.

void insertionSort(int arr[]) {
    int key, j;
    for(int i = 1; i < (sizeof(arr) / sizeof(int)); i++) {
        int key = arr[i];
        int j = i-1;
        while(j >= 0 && arr[j] > key) {
            arr[j+1] = arr[j];
            j = j - 1;
        }
        arr[j+1] = key;
    }
}

I have the function set to void because I thought I'd be able to just access the array since it is a pointer. Would it be better if I returned the array? My logic is obviously wrong, so any pointers (no pun intended) would be great.

Here is the block of code from my main()

case 2:
    printf("Filling array...\n");

    fp = fopen("Random.txt", "r");
    if (fp == NULL) {
        printf("\nError opening Random.txt\n");
        exit(1);
    }

    for (int i = 0; i < (sizeof(arr) / sizeof(int)); i++) {
        fscanf(fp, "%d", &arr[i]);
    }

    insertionSort(arr);

    for (int i = 0; i < (sizeof(arr) / sizeof(int)); i++) {
        printf("%d\n", arr[i]);
    }

    fclose(fp);
    break;
}
5
  • Hint 1: void insertionSort(int arr[]) { is exactly the same as void insertionSort(int *arr) { (yes it's a pointer even when you write []). Hint 2: what's sizeof(int*)/sizeof(int)? Commented Jun 22, 2018 at 1:08
  • sizeof(arr) in the insertionSort routine will be sizeof(int*) which is 8 byte, so what you are actually doing is 8 / 4 , which is 2 Commented Jun 22, 2018 at 1:09
  • @immibis I'm using sizeof to iterate through the entire array. I'm under the impression that I can't use .length() for example because C is not OOP? @0.sh Read above comment. Using sizeof to iterate through array. If there's a better method, I'm open to it. Commented Jun 22, 2018 at 1:14
  • 1
    sizeof(arr) / sizeof(int) = 2 on x86_64 and 1 on x86. Commented Jun 22, 2018 at 1:15
  • @InfoSecNick except arr (in the function) isn't an array. Commented Jun 22, 2018 at 2:07

1 Answer 1

1
void insertionSort(int arr[]) {

Arrays decay to pointers when passed to functions, so this is basically

void insertionSort(int *arr) {

which means this will not give you the number of elements in the array

for(int i = 1; i < (sizeof(arr) / sizeof(int)); i++) {

You need to explicitly pass the size of the array to the function.

void insertionSort(int arr[], int size) {
    for (int i = 1; i < size; i++) {
        int key = arr[i];
        int j = i - 1;
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}

To call the function:

insertionSort(arr, sizeof(arr) / sizeof(int));

In main(), where the array is created, you can use sizeof(A) / sizeof(int), because main() knows the size of the array.

Sign up to request clarification or add additional context in comments.

2 Comments

In other words, sizeof (array) only reports the size of the array in the scope in which array is declared. Otherwise, it's just sizeof (a pointer).
Ah, I didn't know that an array decays to a pointer. Is there any reason to this? Or is this just C? Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.