"Abort trap" error in c++

Thread Starter

asilvester635

Joined Jan 26, 2017
73
I have an array of size 8, and I send only the second half of the array to my insertion sort. It successfully sorts only the second half of the array, but there is a statement at the bottom, "Abort trap: 6". Below are my main.cc and my insertionSort.cc files. What is causing the "Abort trap: 6" error?


Here is the output:
Before sorting
9 8 10 2 4 6 50 1
After sorting
9 8 10 2 1 4 6 50
Abort trap: 6


main.cc
Code:
#include <cstdio>
#include "insertionSort.h"

int main() {

    // create array
    int array[] = {9, 8, 10, 2, 4, 6, 50, 1};

    // get size of the array
    int size = sizeof(array) / sizeof(array[0]);

    // display array before reversing
    printf("Before sorting\n");
    for (int i = 0; i < size; i++) {
        printf("%d ", array[i]);
    } 

    // new line
    printf("\n");

    // sort only the second half of the array
    insertionSort(array+(size/2), size);

    // display the array again after reversing
    printf("After sorting\n");
    for (int i = 0;i < size; i++) {
        printf("%d ", array[i]);

    } // end of for loop

    // new line
    printf("\n");
   
} // end of main

insertionSort.cc
Code:
#include "insertionSort.h"

void insertionSort(int* array, int size) {

    for (int i = 1; i < size; i++) {
        int key = array[i];
        int j = i - 1;

        while (j >= 0 && array[j] > key) {
            array[j+1] = array[j];
            j--;
        } // end of while loop 

        // swap
        array[j+1] = key;

        } // end of for loop

} // end of insertionSort
 

Thread Starter

asilvester635

Joined Jan 26, 2017
73
My program successfully sorts the second half of the array. My array size is 8. The first half contains 4, the other half (which is what I wanted to sort) is also 4, therefore I send only half of what the size. What is your point?
 

Papabravo

Joined Feb 24, 2006
22,082
What happens if your array has an odd number of elements. Integer division will truncate any remainder. That is to say that 9 / 4 = 4
 

WBahn

Joined Mar 31, 2012
32,823
My program successfully sorts the second half of the array. My array size is 8. The first half contains 4, the other half (which is what I wanted to sort) is also 4, therefore I send only half of what the size. What is your point?
Well, if the only array that you ever want to sort the second half of is an array of size 8, then I guess you are fine.

But should you ever want to sort the second half of an array other than one that is of size 8, perhaps you might consider those other possibilities.

Is the following statement true for every nonnegative value of the integer variable size?

size == (size/2) + (size/2)

Test it.

Run the following code:

Code:
for (int size = 0; size < 10; size++)
   printf("%i %s (%i/2) + (%i/2)\n", size, (size == (size/2) + (size/2))? "==" : "!=", size, size);
What do you notice?
 

WBahn

Joined Mar 31, 2012
32,823
I see. I've added an if else statement inside my insertionSort that checks to see if the size is odd or even.
And what do you do if it is odd?

You can certainly do it with an if statement, but you can also calculate it directly.

If I tell you the size of the array and the size of the first part of the array, can you use that information to tell me how many elements are in the rest of the array?

Well, size/2 tells you how many elements are in the first "half" of the array. So how many elements are in the second "half" of the array?
 

Papabravo

Joined Feb 24, 2006
22,082
I was rather hoping that he might spend some time figuring out the solution to this small problem instead of having it spoon fed to him.
I don't usually do that, but this one was kinda trivial. It's kinda like watching a favorite pet suffer -- I just couldn't bear it.
 
Top