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

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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
tomsterkwAuthor Commented:
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?
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.
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

evilrixSenior Software Engineer (Avast)Commented:
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 :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
In this case I would suggest "Insertion Sort".
evilrixSenior Software Engineer (Avast)Commented:
tomsterkwAuthor Commented:
Great reference. Thats exactly what I needed.
Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.