簡體   English   中英

字符串數組的快速排序

[英]Quicksort for string array

在遍歷各處的文章,教程和解決方案之后,我還沒有發現任何解釋如何對字符串數組使用quicksort的內容。 我發現的所有示例都是“ int”或“ char”的。

這是我編輯的代碼。

#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 ;
  }
}

我得到的錯誤是在這一行:

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

我變了

char x*; string x;

我不知道那是不是正確的。 現在出現的錯誤是未聲明strcmpstrcpy 我下一步應該做什么?

string patient[numOfData];

template <class T>
void quickSortMain(char items[][10], int ct)

quickSortMain(patient, 5);

quickSortMain需要一個char[10]數組,而不是string[] 不僅如此,它還取決於模板參數。 刪除template <class T> ,並將char[][10]替換為string[]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM