Core Dump Issue

closed account (yqoET05o)
Hey, trying to do a bubble sort and my code is the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#define SIZE 10000

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

void swap(int array[], int i, int j) {
    int temp = array[i];
    array[i] = array[j];
    array[j] = temp;
}

void randomize(int array[]) {
    srand(time(NULL));
    int i;
    for (i = 0; i < SIZE; i++)
        array[i] = rand();
}

int check(int array[]) {
    int i;
    for (i = 1; i < SIZE; i++) {
        if (array[i] < array[i-1])
            return 0;
    }
    return 1;
}


void insertion(int array[]) {
    int i, j;
    for (i = 0; i < SIZE; i++)
        for (j = i; j > 0 && array[j] < array[j-1]; j--)
            swap(array, j, j-1);
}

void selection(int array[]) {
    int i, j, k;
    for (i = 0; i < SIZE; i++) {
        k = i;
        for (j = i; j < SIZE; j++) {
            if (array[j] < array[k])
                k = j;
        }
        swap(array, i, k);
    }
}

void bubble(int array[]) {
    int i, j;
	for (i = 0; i < SIZE; i+= 1) {
		for (j = i - 1; j >= 0 && array[j] > array[j + 1]; j+= 1) {
			int temp = array[j];
                        array[j] = array[j + 1];
        	        array[j + 1] = temp;
		}
	}
}

int main() {
    int array[SIZE];
    
    randomize(array); 
    insertion(array);
    printf("Insertion: %s\n",(check(array) ? "sorted" : "not sorted"));
    
    randomize(array); 
    selection(array);
    printf("Selection: %s\n",(check(array) ? "sorted" : "not sorted"));
    
    randomize(array); 
    bubble(array);
    printf("Bubble: %s\n",(check(array) ? "sorted" : "not sorted"));
    
    return 0;
}


I'm getting a core dump and I don't understand why? I assumed my code was correct but I must be mistaken, could anyone offer some help? I've never used C before so no idea what the issue might be.
Last edited on
Running it in the debugger I see that it fails on line 52 (in your listing above).
j at that point has (for me) a value of 11243, 1244 elements past the last element in the array. That's strange. The stopping condition is >= 0, which indicates that it should be counting downwards. But it's being incremented instead! Change that to j-- and it works.

Other points:
* in modern C, we declare variable close to their first use, not necessarily at the beginning of a block.
* you should only call srand once in the entire run of the program, so it should be done as one of the first statements in main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 10000

void swap(int *a, int i, int j) { int t = a[i]; a[i] = a[j]; a[j] = t; }

void randomize(int* a) { for (int i = 0; i < SIZE; i++) a[i] = rand(); }

int check(int *a) {
    for (int i = 1; i < SIZE; i++) if (a[i] < a[i-1]) return 0;
    return 1;
}

void insertion(int *a) {
    for (int i = 0; i < SIZE; i++)
        for (int j = i; j > 0 && a[j] < a[j-1]; j--)
            swap(a, j, j - 1);
}

void selection(int *a) {
    for (int i = 0; i < SIZE; i++) {
        int k = i;
        for (int j = i; j < SIZE; j++)
            if (a[j] < a[k])
                k = j;
        swap(a, i, k);
    }
}

void bubble(int *a) {
    for (int i = 0; i < SIZE; i+= 1)
        for (int j = i - 1; j >= 0 && a[j] > a[j + 1]; j--)
            swap(a, j, j + 1);
}

void test(const char *name, void (*f)(int*)) {
    int a[SIZE];
    randomize(a); 
    f(a);
    printf("%s: %s\n", name, check(a) ? "sorted" : "not sorted");
}

int main() {
    srand(time(NULL));
    test("Insertion", insertion);
    test("Selection", selection);
    test("Bubble",    bubble);
    return 0;
}

Topic archived. No new replies allowed.