Using bubbleSort method to perform a search in a text file.

Posted on 2008-11-09
Last Modified: 2012-05-05
Good evening. I've got to write a program to test the function seqOrderedSearch, using function bubbleSort to sort the list, prior to conducting the search.

I'll attached the individual functions, although im having trouble with the main() function.
Here's what I have. . . (see below)

I've also attached the input file. The data must be read from a text document named doublevalues.txt

Thanks for any support you may provide!

void bubbleSort(int list[], int length)


    int temp;

    int iteration;

    int index;

    for (iteration = 1; iteration < length; iteration++)


        for (index = 0; index < length - iteration; index++)

            if (list[index] > list[index + 1]) 


                temp = list[index];

                list[index] = list[index + 1];

                list[index + 1] = temp;





int seqOrderedSearch(const int list[], int listLength, 

                     int searchItem)


    int loc;                                           

    bool found = false;                                

    for (loc = 0; loc < listLength; loc++)             

        if (list[loc] >= searchItem)                   


            found = true;                              




    if (found)                                         

        if (list[loc] == searchItem)                   

            return loc;                                


            return -1;                                 


        return -1;                                     


Open in new window

Question by:tomsterkw
    LVL 31

    Expert Comment

    Correct me if I'm wrong, but you have to read the entire file and build the list[] array, right? That means you have to touch on every item in the file at least once anyways. Why sort and search?

    If you really want to do it though, you will also want to look into doing a binary search as opposed to a sequential seach. That's the whole point of sorting it. A binary search will take advantage of the fact that the data is sorted by cutting the search data in half for every step it takes. A sequential search will not, it just steps through one item at a time.

    Author Comment

    Correct Frosty.

    I'd first have to start with a for loop that would take in all the data from the .txt file. . .
    Something like:

    ifstream infile;
    for (counter = 0; counter < 25; counter++)
    infile >> getList[counter];

    My biggest challenge is when it comes to the functions. I just cant seem to comprehend how to use the bubbleSort function and then send the array data back to the main function. Any ideas?
    LVL 53

    Expert Comment

    Your bubbleSort function does an in-place sort - ie. it modifies the array that was passed to it as argument. So, you just pass it an array of int's as well as its size, and when the function has finished, that array should be sorted.
    LVL 39

    Accepted Solution

    Is there any specific reason for writing your own bubble sort? Is this an exercise? If not, have you considered just using the standard std::sort() function that comes as standard with C++?

    Once sorted, you can use std::binary_search to search the file.

    Obviously, this is only applicable if this is not an academic exercise :)
    LVL 3

    Expert Comment

    In this case I would suggest "Insertion Sort".
    LVL 39

    Expert Comment


    Author Closing Comment

    Great reference. Thats exactly what I needed.
    Thank you!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now