0

This is my code. I don't know how to get the code to run. Im having problems with quicksorting for string.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <time.h>
#include <string>
#include <ctime>
#include <stdio.h>



using namespace std;

int count;
template <class T>
void printArray(T ar[], int sz);
template <class T>
void bubbleSort(T ar[], int sz);
void quickSortMain(string items[], int ct);
void quickSort(string items[], int left, int right);

//////////////////////////////////////////////////////////////////////////
// Main Function Implementation
//////////////////////////////////////////////////////////////////////////

int main() {
    int numOfData = 50000;
    string line, temp;
    ofstream resultFile;
    ofstream tableFile;
    double data[100][2];
    string patient[numOfData];
    ifstream dataFile("shufflePatient.txt");
    int i;
    int SIZE = 0;

    cout << "Program to shuffle data" << endl << endl;
    cout << "This program will calculate swapping processes and running time.";


    /*Storing data*/
    cout << "Reading data in process.." << endl;
    if (dataFile.is_open()) {
        i=-1;
        while (dataFile.good()) {
            getline (dataFile, line);
            if (i>=0) patient[i] = line;
            i++;
        }
        dataFile.close();
    }

    SIZE = 5;
    quickSortMain(patient, 5);


    /*Writing to file*/
    cout << "Writing to file.." << endl;
    resultFile.open ("test.txt");
    for (int i=0 ; i<numOfData ; i++) {
        resultFile << patient[i] << "\n";
    }
    resultFile.close();
    system("pause");
    return 0;
}


void quickSortMain(string items[], int ct)
{
  quickSort(items, 0, ct-1);
}


void quickSort(string items[], int left, int right)
{
  int i, j;
  char *x;
  string temp[10];

  i = left;
  j = right;
  x = items[(left+right)/2];

  do {
    while((strcmp(items[i],x) < 0) && (i < right)) {
       i++;
    }
    while((strcmp(items[j],x) > 0) && (j > left)) {
        j--;
    }
    if(i <= j) {
      strcpy(temp, items[i]);
      strcpy(items[i], items[j]);
      strcpy(items[j], temp);
      i++;
      j--;
   }
  } while(i <= j);

  if(left < j) {
     quickSort(items, left, j);
  }
  if(i < right) {
     quickSort(items, i, right);
  }
}









//----------------------------------------------------------------------------
// prints array of size size
//----------------------------------------------------------------------------
template <class T>
void printArray(T patient[], int size)
{
  for(int i = 0; i < size; i++)
    cout << patient[i] << " ";
  cout << endl;
}




//----------------------------------------------------------------------------
// sorts array of size size by Bubble Sort method
//----------------------------------------------------------------------------
template <class T>
void bubbleSort(T patient[], int size) //returning an int
{
    bool noChange = true;
    for(int i = size; i > 0; i--)
  {
    noChange = true;
    for(int j = 1; j < i; j++)
    {
      if(patient[j] < patient[j - 1])
      {
        swap(patient[j], patient[j-1]);
        count = count + 1;
        noChange = false;
      }
    }
    if (noChange)
      return ;
  }
}

The error i got was in this line:

 x = items[(left+right)/2];

I changed

char x*; to string x;

I don't know if that was correct. Now the error im getting is that strcmp and strcpy are not declared. Any help on what i should do next?

3
  • VLAs are not standard. Commented Nov 7, 2013 at 5:07
  • Strings can be compared with < and > and assigned with =. No need for strcmp and strcpy. Commented Nov 7, 2013 at 5:12
  • 1. Get rid of the left and right, You only need len if you're remotely proficient with pointer math. 2. Stop using C runtime functions for things provided for your by the C++ runtime, such as the operator < overload for std::string Commented Nov 7, 2013 at 5:27

1 Answer 1

2

That's correct, now change

strcmp(items[i],x) < 0

to

items[i] < x

and

strcmp(items[j],x) > 0

to

items[j] > x

and

strcpy(temp, items[i]);

to

temp = items[i];

etc.

But really this is a case of RTFM. If you're competent enough to write a quicksort routine you should be competent enough to look up how std::string works. Being able to access reference information is a very important skill for a programmer.

Sign up to request clarification or add additional context in comments.

5 Comments

Also change string temp[10]; to string temp; although std::swap would be a better choice to swap items.
@RetiredNinja Right I missed that. I guess it used to be char temp[10];. std::swap is a better choice but I left it as is to show the equivalence of strcpy and =. Maybe the OP can practice his research skills by looking up std::swap.
thank you for all the comments!! last question. could you guys guide we as to where i should research std::string and std::swap from? most of the websites i open dont really explain both of these that much in regard with quicksort.
@user2959974 This one's OK en.cppreference.com/w. Look up std::string and std::swap, but look them up for themselves, not just for quicksort.
@user2959974 Note: that website is the website for all things related to the C++ language and standard library. It is heads and tails better than anything else you will find online.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.