Merge-sorting a list problem

I'm required to sort a list using merge sort. I've put together this code, but I'm running into a weird error.

My list is populated by random numbers. However, after sorting, it only displays numbers that are larger than the first element of the linked list, in sorted order.
For Example:
Numbers before sorting: 50, 80, 15, 75, 25, 100
Numbers after sorting: 50, 75, 80, 100

Here's some of my code:
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
struct elem
{
	int key; 
	elem *next;
} 
*start=NULL;

elem* Split(elem* my_node)
{
    elem* secondNode;

    if (my_node == NULL) return NULL;
    else if (my_node->next == NULL) return NULL;
    else {
        secondNode = my_node->next;
        my_node->next = secondNode->next;
        secondNode->next = Split(secondNode->next);
        return secondNode;
    }
}
elem* Merge(elem* firstNode, elem* secondNode)
{
    if (firstNode == NULL) return secondNode;
    else if (secondNode == NULL) return firstNode;
    else if (firstNode->key <= secondNode->key) //if I reverse the sign to >=, the behavior reverses
    {
        firstNode->next = Merge(firstNode->next, secondNode);
        return firstNode;
    }
    else 
    {
        secondNode->next = Merge(firstNode, secondNode->next);
        return secondNode;
    }
}
elem* MergeSort(elem *my_node)
{
    elem *secondNode;

    if (my_node == NULL)
        return NULL;
    else if (my_node->next == NULL)
        return my_node;
    else
    {
        secondNode = Split(my_node);
        return Merge(MergeSort(my_node),MergeSort(secondNode));
    }
}

Thanks in advance!
Last edited on
Topic archived. No new replies allowed.