Thread: Selection sort on a vector

  1. #1
    Internet Superhero
    Join Date
    Sep 2006

    Selection sort on a vector

    I did this excercise to get better at debugging, so i chose something i knew i could do without to much hassle. I was wrong though, and it bugs me, since it's really a quite simple task.

    The program reads in some data from a textfile, sticks it in a vector containing a struct that fits the data, and then i'm trying to sort it ascendingly and print it to the screen, using selection sort, but it's not working. The problem here being that if i try to debug this fairly short program the debugger starts stepping into all the vector functions in the STL, which i obviously do not need to debug, and it makes it impossible for me to maintain overview and debug my own code.

    My first question i guess is: How can i tell my debugger not to show me std::vector.size() and and so on, i don't care about the STL functions and all those templates and long function calls in a small terminal window makes my head dizzy. Is there a way for me to tell the debugger to stay in my own cpp files?
    I'm using MingW GCC and GDB on Windows XP 32-bit with MSYS.

    The second question is, what is wrong with the code, i looked up some pseudo code on wiki and tried to replicate it using std::vector and c++, but it's not working:

    #include <fstream>
    #include <iostream>
    #include <vector>
    struct Data {
    unsigned int DataMember;
    unsigned int LineNumber;
    } DataBuffer;
    int main()
    	std::ifstream DataStream("data.txt");
    	std::vector<Data> MyData;
    	while( DataStream >> DataBuffer.LineNumber >> DataBuffer.DataMember )
    		std::cout << DataBuffer.LineNumber << " " << DataBuffer.DataMember << std::endl;
    	std::cout << std::endl << std::endl;
    	unsigned int Size = MyData.size();
    	unsigned int SmallestElement;
    	for(unsigned int i = 0; i < Size; ++i)
    		SmallestElement = i;
    		for(unsigned int j = i+1; j < Size-1; ++j)
    			if(MyData[j].DataMember < MyData[SmallestElement].DataMember)
    				SmallestElement = j;
    		std::swap(MyData[SmallestElement], MyData[i]);
    	for(unsigned int k = 0; k < Size; ++k)
    		std::cout << MyData[k].LineNumber << " " << MyData[k].DataMember << std::endl;
    	return 0;
    The data.txt file looks like this:
    1 1234
    2 5678
    3 9123
    4 4567
    5 8912
    6 6324
    7 9084
    8 3287
    ...the first digit here being the linenumber, and the second digit is the actual "data" itself.

    The output i get on the screen after running it is sorted right in the first 3 or 4 elements, but the last couple of elements are left untouched, so something IS getting std::swapped, but it's not properly sorting the vector.

    I'm certain this is something simple, i was certain i could do this easily, guess i was overconfident.

    And i know about std::sort() but i want to be able to do something simple like this without using a prewritten function!
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  2. #2
    Join Date
    Dec 2007
    Use next to step over a call to the next call, rather than into it.

    This is a good GDB tutorial.

  3. #3
    Internet Superhero
    Join Date
    Sep 2006
    Ahh okay, i thought next and step were equivalent so i've just been using step. Thanks for clearing that up. I'll have a go at debugging it myself then
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programing
    By flame82 in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 02:35 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Selection Sort
    By Drew in forum C++ Programming
    Replies: 16
    Last Post: 09-18-2003, 07:19 AM
  4. Selection Sort
    By volk in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2003, 06:54 AM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM