I'm having trouble inserting a bubble sort for my program, any help would be appreciated.

#include <iostream>
#include <string> 
using namespace std; 

int main() 
    string food[100]; 
    string lookup; 
    int calories[100]; 
    int x = -1;
        x++; cout << "Enter a menu item (enter 'done' when finished): "; 
        if (food[x] != "done") 
            cout << "Enter the number of calories: "; 
            cin >> calories[x]; 

    }while (food[x] != "done");
            bool found = false; 
            cout << "Enter a product to look up: " ; 
            getline(cin, lookup); 
            for (int y = 0; y < x; y++) 
                if (lookup == food[y]) 
                    cout << food[y] << " has " << calories[y] << " calories." << endl; 
                    found = true; 
                if ((found == false) && (lookup != "done")) 
                    cout << lookup << " was not found." << endl; } 

                } while (lookup != "done"); 

Edited by mike_2000_17: Fixed formatting

9 Years
Discussion Span
Last Post by StuXYZ

If I'm reading your intentions with this program correctly -- to display the food and its corresponding calories -- you wouldn't necessarily want to do a bubble sort on parallel array. While it will work, it probably wouldn't give valid data.

Use a 2D array for this


After the user has entered the data, sort it using a bubble sort

When you search for the data, search for it using a binary search

thats what i'm looking to get at

//========================================================= bubbleSortRange
// After a pass in bubble sort, it's only necessary to sort from just 
// below the first exchange (small values may move lower)
// to just before the last exchange (largest values won't move higher). 
// Everything that wasn't exchanged must be in the correct order. 
// After each pass, the upper and lower bounds for the next pass are
// set from the positions of the first and last exchanges on prev pass.

void bubbleSortRange(int x[], int n) {
    int lowerBound = 0;    // First position to compare.
    int upperBound = n;    // First position NOT to compare.
    //--- Continue making passes while there is a potential exchange.
    while (lowerBound <= upperBound) {
        int firstExchange = n;  // assume impossibly high index for low end.
        int lastExchange  = -1; // assume impossibly low index for high end.
        //--- Make a pass over the appropriate range.
        for (int i=lowerBound; i<upperBound; i++) {
            if (x[i] > x[i+1]) {
                //--- exchange elements
                int temp = x[i];  x[i] = x[i+1];  x[i+1] = temp;
                //--- Remember first and last exchange indexes.
                if (i<firstExchange) { // true only for first exchange.
                    firstExchange = i;
                lastExchange = i;
        //--- Prepare limits for next pass.
        lowerBound = firstExchange-1;
        if (lowerBound < 0) {
            lowerBound = 0;
        upperBound = lastExchange;
}//end bubbleSortRange

First off: and most important
Fatsbear please read the first announcement in this forum about code tags. Then use them.

Second: The problem is the association between calories and the food name.

You will find other posts, by your classmates I suspect, who have solved the problem by sorting on calories and then making the same moves on the foodname. This is an ugly solution. Especially, if you then have more information to associate, e.g. the price, the age .

Your best solution is to associate the food name with the calories in a class. Create an array/vector of you food class and sort the group as a whole.

The idea of a 2d array is simply not the way forward. It doesn't represent the underlying problem structure and in very difficult to extend.

Overload the operator< or use a comparison function and use the
standard sort algorithm, or write you own.

Finally, bubble sort is one of the worst sorts available and should never be used (except some very rare cases). It is simply too slow.
Despite JLopeman's careful minimization of the loop passes, on a long random list it is still horrifically slow.

Anyway, you should write some code and post that for comment/problem solving.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.