字符串数组的快速排序

Quicksort for string array

本文关键字:快速排序 数组 字符串      更新时间:2023-10-16

在浏览了各地的文章、教程和解决方案后,我还没有遇到任何解释如何使用字符串数组快速排序的内容。我找到的所有示例都是"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[]