Solved

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

Posted on 2008-11-09
648 Views
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;

}
``````
doublevalues.txt
0
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.
0

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?
0

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

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++?

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

LVL 3

Expert Comment

In this case I would suggest "Insertion Sort".
0

LVL 39

Expert Comment

Why?
0

Author Closing Comment

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

## Featured Post

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…