Hi all, I've been working on a practice problem (not homework) involving the merge sort algorithm. I keep getting a segfault when I run my code.
Here are the instructions:
*Please note I'm really just trying to get mergeSort() to sort the numbers without fault right now.
Order from Chaos
Filename: order.c
Your math teacher, Mr. Conway, has a bad habit of writing the problem numbers for your class’ homework assignments in a random order. Additionally, sometimes he won’t realize that he has written down a problem number already and will end up writing the same number multiple times! This makes the task of doing your homework much harder. If you decide to solve them in order, you need to spend several minutes sorting them before you begin. On the other hand, if you work through the problems in the order that Mr. Conway wrote them, you may end up solving the same problem twice.
To deal with this predicament, you decide to write a program that prints out the list of problems the way that a logical math teacher would write them. Since you would like the reorganized list to be as neat as possible, you set the following constraints:
•
•
•
•
In the new list, the numbers should be printed in order from lowest to highest.
There should be no duplicates.
Any two or more consecutive problem numbers should be condensed into a single segment, represented by a starting number, followed by a hyphen and an ending number.
For example, “7, 8, 9” would condense down to “7-9”.
The numbers should be condensed into as few segments as possible.
Different problems or ranges should be separated with a comma and a space. For example, given these rules, the problem numbers “2, 7, 1, 4, 1, 6, 8” should be condensed down to “1-2, 4, 6-8”.
The Problem:
Help make your life easier by reorganizing Mr. Conway’s homework assignment.
The Input:
The first line will contain a single, positive integer, d, representing the number of days of homework. For each day, there will be two lines. The first line will contain a single integer, p (1 ≤ p ≤ 10 5 ), representing the number of problems written by Mr. Conway on the board. The next line will contain p integers, n i (1 ≤ n i ≤ 10 5 ), representing the assigned problems.
The Output:
For each day, output a single line, starting with “Day #s: ” where s is the day number in the input (starting with 1). On the same line, output the condensed version of the problems, following the above constraints. Separate different problems or ranges within each day with a comma and a space.
Output a blank line after each day.
Sample Input:
4
7
2 7 1 4 1 6 8
2
6 8
10
2 7 8 14 2 10 15 1 3 2
5
1 2 3 4 5
Sample Output:
Day #1: 1-2, 4, 6-8
Day #2: 6, 8
Day #3: 1-3, 7-8, 10, 14-15
Day #4: 1-5
My code is attached below. I am wondering do all of the recursive calls happen before it moves on to the while loop? I'm confused about what happens when I think.
Thanks!