Bubble Sort function

closed account (G60iz8AR)
Do you know how to do this bubble sort function with these words india xray charlie foxtrot alpha echo golf delta bravo hotel ? if so, please help
#include <fstream>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <ctime>
#include <algorithm>

using namespace std;
const int MAX_WORDS = 20;

void printWords( string arr[], int cnt );
void bubbleSort( string arr[], int cnt );

int main( int argc, char**argv )
{
if (argc < 2 )
{
cout << "Hey you idiot! put the filename after the a.exe!\nLIKE THIS: a.exe words.txt\n";
exit(0);
}

ifstream infile;
infile.open( argv[1], ios::in );
if (!infile)
{
cout << "\nIO error attempting to open argv[1]\n";
exit(0);
}
int wordCnt=0;
string word;
string wordList[MAX_WORDS];
while( wordCnt < MAX_WORDS && infile >> word )
wordList[wordCnt++] = word;
infile.close(); // Done reading. Close it

cout << "Original order: " ;
printWords( wordList, wordCnt );

bubbleSort( wordList, wordCnt );

cout << "Sorted order: " ;
printWords( wordList, wordCnt );

return 0;
} // END MAIN //###############################################################



void bubbleSort( string arr[], int cnt )
{
/* for stop = cnt-2 downto 0
for i = 0 to stop
if arr[i] > arr[i+1]
swap them
*/
}

void printWords( string arr[], int cnt )
{
for (int i=0 ; i<cnt ; i++ )
cout << arr[i] << " ";
cout << endl;
}
You already have some pseudocode for the method. Can't you translate it? For example this is what the first line would turn into:
1
2
3
4
5
6
7
//for stop = cnt - 2 downto 0
for (int stop = cnt - 2; stop >= 0; stop--)
{
    //for i = 0 to stop
        //if arr[i] > arr[i+1]
            //swap them
}

See if you can translate the rest of the pseudocode to real code.
Last edited on
closed account (G60iz8AR)
Thats what I cant try to figure out I cant translate can you please help me out with that ?
Topic archived. No new replies allowed.