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?
 
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
 
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.