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

Posted on 2008-11-09
Medium Priority
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

ID: 22918936
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

ID: 22919011
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

ID: 22919870
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.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 40

Accepted Solution

evilrix earned 375 total points
ID: 22928718
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 :)

Expert Comment

ID: 22937643
In this case I would suggest "Insertion Sort".
LVL 40

Expert Comment

ID: 22944650

Author Closing Comment

ID: 31514923
Great reference. Thats exactly what I needed.
Thank you!

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

840 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