tracing merge sort algorithm with cout

Is it possible to trace the merge sort algorithm I have below with cout statements. I have an assignment that I am struggling with to find the following:
Upfront this is a homework assignment. I am only asking for some direction how to trace this with cout statements.

What I am trying to accomplish:

Present an example of walking through the merge sort.


Post the following:

- Start with an unordered list of at least NINE elements.

- List all the recursive calls that would be made to sort the list, going forward.

- Show which pair of sublists will be merged each time, going back.

- Show the ordering of the sublist after each recursive call returns.

My code:

and an attempt to print the requirements.
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

// global constants
const int LIST_SIZE = 9;

void merge(int arr[], int size1, int size2);
void mergeSort(int arr[], int size);


int main()
{
 	//int list[LIST_SIZE] = {9, 2, 1, 4, 6, 8, 5, 3, 7};
    int list[LIST_SIZE] = {9, 8, 7, 6, 5, 4, 3, 2, 1};
    int first = 0;            // first initialized to first index in array
    int last = LIST_SIZE - 1; // last initialized to last index in array
 	
 	cout << "Original List: ";
 	for (int index = 0; index < LIST_SIZE; index++)
 	    cout << list[index] << " ";
 	    
    cout << endl; 	    
 	    
    mergeSort(list, LIST_SIZE);
 	    
 	    
 	cout << "Sorted List:   ";	    
 	for (int index = 0; index < LIST_SIZE; index++)
 	    cout << list[index] << " ";


    cout << endl << endl;
    system("PAUSE");
    return 0;
}


void merge(int arr[], int size1, int size2) 
{
    int temp[size1+size2];
    int ptr1=0, ptr2=0;
    
    
 
    while (ptr1+ptr2 < size1+size2) {
        if (ptr1 < size1 && arr[ptr1] <= arr[size1+ptr2] || ptr1 < size1 && ptr2 >= size2)
        {
            temp[ptr1+ptr2] = arr[ptr1++];
            //scout << temp[ptr1+ptr2] << " ";
	    }
 
        if (ptr2 < size2 && arr[size1+ptr2] < arr[ptr1] || ptr2 < size2 && ptr1 >= size1)
        {
            temp[ptr1+ptr2] = arr[size1+ptr2++];
            //cout << temp[ptr1+ptr2] << endl;
    	}
    }
     //cout << " size " << size1+size2 << endl;
    cout << "sort: ";
    for (int i=0; i < size1+size2; i++)
    {
        arr[i] = temp[i];
        cout << arr[i] << " ";
	}
	
	cout << endl;
}
 
void mergeSort(int arr[], int size) 
{
    if (size == 1)
        return;  
 
    int size1 = size/2;
	int size2 = size-size1;
	


    
    cout << "(" << "array, " << size1 << ")  <- recursive call" << endl;   
    cout << "(" << "array" << "+" << size1 << "," << size2 << ") <- recursive call" << endl; 
	
    mergeSort(arr, size1);   
	//
	
    mergeSort(arr+size1, size2);
    merge(arr, size1, size2);
}
Last edited on
Topic archived. No new replies allowed.