I'm trying to debug my mergesort program, which seems to be problematic somewhere inside the "merge" function when i try to use "push_back" to add values from either the vector "left" or "right" to "mergedList". The following is an excerpt from the gdb debugging session (followed by my complete program code below that)
This occurs at the first call to merge; I was able to access the values of "vector left" using "print left[0]" and i got a value I expected (all numbers in the vector "left" are between 1-50000), but after the executing the line of code when i = 0:
"mergedList.push_back(left[i]);" and then using the debugger to print mergedList[0], it appears that adding left[0] was unsuccessful.
Am I misunderstanding push_back? or vectors? Sorry if my question is unclear--please let me know how I could improve it!
GDB debugging session:
(gdb) print left[0]
$1 = (int &) @0x100104760: 14108
(gdb) print mergedList[0]
$2 = (int &) @0x7fff5fbfdbf0: 1066800
(gdb)
(gdb) info locals
t = 0
i = 0
mergedList = {
<std::_Vector_base<int,std::allocator<int> >> = {
_M_impl = {
<std::allocator<int>> = {
<__gnu_cxx::new_allocator<int>> = {<No data fields>}, <No data fields>},
members of std::_Vector_base<int,std::allocator<int> >::_Vector_impl:
_M_start = 0x7fff5fbfdbf0,
_M_finish = 0x7fff5fbfdc68,
_M_end_of_storage = 0x7fff5fbfdb90
}
}, <No data fields>}
j = 0
sizeOfLeft = 1
sizeOfRight = 1
next = 1
(gdb)
-uuu:**-F1 *gud-p1* Bot L28 (Debugger:run)--------------------------------------
------------------------------------------------------------------------------------------------------------------
while (iss >> n)
{
v.push_back(n);
}
}
return v;
}
vector<int> merge(vector<int> left, vector<int> right){
int i = 0;
int j = 0;
int sizeOfLeft = left.size();
int sizeOfRight = right.size();
vector<int> mergedList;
while (i < sizeOfLeft || j < sizeOfRight){
if (i < sizeOfLeft && j < sizeOfRight){
if (left[i] < right[j]) {
mergedList.push_back(left[i]);
=> i++;
}else{
mergedList.push_back(right[j]);
j++;
-uu-:---F1 main.cpp 21% L47 (C++/l Abbrev)--------------------------------------------------------------------------------------------------------------------------------------------------------
#COMPLETE MERGESORT PROGRAM
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
vector<int> getNums(){
ifstream infile("/Users/christinedeist/Documents/Algorithms/Practice/TestProject/IntegerArray.txt");
string line;
vector<int> v;
while (getline(infile, line))
{
istringstream iss(line);
int n;
while (iss >> n)
{
v.push_back(n);
}
}
return v;
}
vector<int> merge(vector<int> left, vector<int> right){
int i = 0;
int j = 0;
int sizeOfLeft = left.size();
int sizeOfRight = right.size();
vector<int> mergedList;
while (i < sizeOfLeft || j < sizeOfRight){
if (i < sizeOfLeft && j < sizeOfRight){
if (left[i] < right[j]) {
mergedList.push_back(left[i]);
i++;
}else{
mergedList.push_back(right[j]);
j++;
}
}else if (i < sizeOfLeft){
mergedList.push_back(left[i]);
i++;
}else if (j < sizeOfRight){
mergedList.push_back(right[j]);
j++;
}
}
return mergedList;
}
vector<int> sortVector(vector<int> nums){
int sizeOfNums = nums.size();
if (sizeOfNums == 1){
return nums;
}
vector<int> left;
vector<int> right;
int midpoint = sizeOfNums/2;
for (int i = 0; i < midpoint; i++){
left.push_back(nums[i]);
}
for (int j = midpoint; j < sizeOfNums; j++){
right.push_back(nums[j]);
}
left = sortVector(left);
right = sortVector(right);
return merge(left, right);
}
int main (int argc, char *argv[]) {
vector<int> nums = getNums();
vector<int> sorted = sortVector(nums);
for(int i = 0; i < nums.size(); i++){
cout << nums[i] <<endl;
}
return 0;
}