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;                              
            break;                                     
        }
 
    if (found)                                         
        if (list[loc] == searchItem)                   
            return loc;                                
        else                                           
            return -1;                                 
    else                                               
        return -1;                                     
}

Open in new window

doublevalues.txt
tomsterkwAsked:
Who is Participating?

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

x
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.

Frosty555Commented:
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.
0
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?
0
Infinity08Commented:
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.
0
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++?

http://www.cplusplus.com/reference/algorithm/sort.html

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

http://www.cplusplus.com/reference/algorithm/binary_search.html

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

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
SreejithGCommented:
In this case I would suggest "Insertion Sort".
0
evilrixSenior Software Engineer (Avast)Commented:
Why?
0
tomsterkwAuthor Commented:
Great reference. Thats exactly what I needed.
Thank you!
0
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
C++

From novice to tech pro — start learning today.