here there is the mergesort between 2 printfs, the printfs give exactly the same results: for(l = 0; lpnt.x, a[l]->pnt.y, a[l]->pvertex->pnt.x, a[l]->pvertex->pnt.y); printf("before merge sort\n"); mergesort(a, j, sizeof(struct qentry *),(int (*)(const void *, const void *)) heap_cmp); printf("after merge sort\n"); printf("--------------------\n"); for(l = 0; lpnt.x, a[l]->pnt.y, a[l]->pvertex->pnt.x, a[l]->pvertex->pnt.y); -------------------------------------------------------------------------------- the compare function: static int heap_cmp(struct qentry *e1, struct qentry *e2) { printf("Comparing %d %d with %d %d\n", e1->pnt.x, e1->pnt.y, e2->pnt.x, e2->pnt.y); if(e1->pnt.y == e2->pnt.y) return e2->pnt.x - e1->pnt.x; else return e1->pnt.y - e2->pnt.y; } ------------------------------------------------------------------------------------- what i get printed (from the whole programm): printing vertex: 4424 2826, and next one: 3749 1851 printing vertex: 4841 2646, and next one: 4424 2826 printing vertex: 4841 5546, and next one: 4841 2646 printing vertex: 3449 3576, and next one: 4841 5546 printing vertex: 2744 4003, and next one: 3449 3576 printing vertex: 2174 3726, and next one: 2744 4003 printing vertex: 1368 4181, and next one: 2174 3726 printing vertex: 974 2376, and next one: 1368 4181 printing vertex: 4874 3576, and next one: 974 2376 printing vertex: 1574 276, and next one: 4874 3576 printing vertex: 2690 54, and next one: 1574 276 printing vertex: 5474 2076, and next one: 2690 54 printing vertex: 4747 2299, and next one: 5474 2076 printing vertex: 3749 1851, and next one: 4747 2299 before merge sort Comparing 134539712 134539840 with 134539840 134539968 Comparing 134539840 134539968 with 134539968 0 Comparing 134539840 134539712 with 134539712 134539968 Comparing 134539968 0 with 0 0 Comparing 134538432 134538560 with 134538560 134538688 Comparing 134538688 134538816 with 134538816 134538944 Comparing 134538944 134539072 with 134539072 134539200 Comparing 134539200 134539328 with 134539328 134539456 Comparing 134539456 134539584 with 134539584 134539840 Comparing 134538432 134538560 with 134538688 134538816 Comparing 134538688 134538816 with 134538560 134538688 Comparing 134538944 134539072 with 134539200 134539328 Comparing 134539200 134539328 with 134539072 134539200 Comparing 134539456 134539584 with 134539840 134539712 Comparing 134539840 134539712 with 134539584 134539840 Comparing 134539584 134539840 with 134539712 134539968 Comparing 134538432 134538560 with 134538944 134539072 Comparing 134538944 134539072 with 134538560 134538688 Comparing 134538944 134539072 with 134538688 134538816 Comparing 134538944 134539072 with 134538816 134538944 Comparing 134538432 134538560 with 134539584 134539456 Comparing 134539584 134539456 with 134538560 134538688 Comparing 134539584 134539456 with 134538688 134538816 Comparing 134539584 134539456 with 134538816 134538944 Comparing 134539584 134539456 with 134538944 134539072 Comparing 134539584 134539456 with 134539072 134539200 Comparing 134539584 134539456 with 134539200 134539328 Comparing 134539584 134539456 with 134539328 134539584 Comparing 134539328 134539584 with 134539456 134539840 after merge sort -------------------- printing vertex: 4424 2826, and next one: 3749 1851 printing vertex: 4841 2646, and next one: 4424 2826 printing vertex: 4841 5546, and next one: 4841 2646 printing vertex: 3449 3576, and next one: 4841 5546 printing vertex: 2744 4003, and next one: 3449 3576 printing vertex: 2174 3726, and next one: 2744 4003 printing vertex: 1368 4181, and next one: 2174 3726 printing vertex: 4874 3576, and next one: 974 2376 printing vertex: 974 2376, and next one: 1368 4181 printing vertex: 2690 54, and next one: 1574 276 printing vertex: 1574 276, and next one: 4874 3576 printing vertex: 4747 2299, and next one: 5474 2076 printing vertex: 5474 2076, and next one: 2690 54 printing vertex: 3749 1851, and next one: 4747 2299 -->**>3749 1851 Comparing 3749 1851 with 4747 2299 Comparing 3749 1851 with 4747 2299 Comparing 3749 1851 with 4424 2826 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 -->**>5474 2076 Comparing 5474 2076 with 2690 54 Comparing 5474 2076 with 4747 2299 Comparing 5474 2076 with 2690 54 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 -->**>4747 2299 Comparing 4747 2299 with 5474 2076 Comparing 4747 2299 with 3749 1851 -->**>1574 276 Comparing 1574 276 with 4874 3576 Comparing 1574 276 with 4874 3576 Comparing 1574 276 with 2690 54 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 -->**>2690 54 Comparing 2690 54 with 1574 276 Comparing 2690 54 with 1574 276 Comparing 2690 54 with 5474 2076 Comparing 1574 276 with 4874 3576 Comparing 1574 276 with 4874 3576 Comparing 1574 276 with 2690 54 -->**>974 2376 Comparing 974 2376 with 1368 4181 Comparing 974 2376 with 1368 4181 Comparing 974 2376 with 4874 3576 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 -->**>4874 3576 Comparing 4874 3576 with 974 2376 Comparing 4874 3576 with 1574 276 YOU ARE INSERTING A DIAGONAL, between 4874 3576, and 0 0 Comparing 4874 3576 with 974 2376 Comparing 4874 3576 with 1574 276 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 both of the vertices that the diagonal connects, are split vertices -->**>1368 4181 Comparing 1368 4181 with 2174 3726 Comparing 1368 4181 with 974 2376 -->**>2174 3726 Comparing 2174 3726 with 2744 4003 Comparing 2174 3726 with 2744 4003 Comparing 2174 3726 with 1368 4181 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 -->**>2744 4003 Comparing 2744 4003 with 3449 3576 Comparing 2744 4003 with 2174 3726 -->**>3449 3576 Comparing 3449 3576 with 4841 5546 Comparing 3449 3576 with 4841 5546 Comparing 3449 3576 with 2744 4003 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 -->**>4841 5546 Comparing 4841 5546 with 4841 2646 Comparing 4841 5546 with 3449 3576 -->**>4841 2646 Comparing 4841 2646 with 4424 2826 Comparing 4841 2646 with 4424 2826 Comparing 4841 2646 with 4841 5546 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 -->**>4424 2826 Comparing 4424 2826 with 3749 1851 Comparing 4424 2826 with 4841 2646 YOU ARE INSERTING A DIAGONAL, between 4424 2826, and 0 0 Comparing 4424 2826 with 3749 1851 Comparing 4424 2826 with 4841 2646 Comparing 0 0 with 0 0 Comparing 0 0 with 0 0 both of the vertices that the diagonal connects, are split vertices