如何使用快速排序对双精度的2D向量进行排序?

时间:2019-11-12 19:11:41

标签: c++ algorithm sorting

我有一个双精度的2D向量,我需要使用quicksort对其进行排序。但是,当我打印所有步骤时,似乎无法正常工作。 我的向量是一个全局变量,我尝试对每一行进行排序并在每次迭代后打印当前向量。

vector<vector<double>> vect;
int rows, cols;
void Print2DArray() {
    for (int i = 0;i < vect.size();++i) {
        for (int j = 0;j < vect[i].size();++j) {
            cout << vect[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}

int partition(int low, int high, int ind) {
    double pivot = vect[ind][high];
    int i = low;
    double tmp;
    for (int j = low;j <= high - 1;++j) {
        if (vect[ind][j] <= pivot) {
            tmp = vect[ind][j];
            vect[ind][j] = vect[ind][i];
            vect[ind][i] = tmp;
            i++;
        }
    }
    tmp = vect[ind][high];
    vect[ind][high] = vect[ind][i];
    vect[ind][i] = tmp;
    Print2DArray();
    return i;
}
void Sort(int low, int high, int ind) {
    if (low < high) {
        int pi = partition(low, high, ind);
        Sort(low, pi - 1, ind);
        Sort(pi + 1, high, ind);
    }
}
void TwoDimSort() {
    for (int i = 0;i < vect.size();++i) {
        Sort(0, vect[i].size() - 1, i);
    }   
    Print2DArray();

}
int main() {
    rows = 3;
    cols = 9;
    srand((unsigned)time(NULL));
    for (int i = 0;i < rows;++i) {
        vector<double> tmp;

        for (int j = 0;j < cols;++j) {
            double num = (rand() % 100) * 0.9;
            if (num > 0.0)
                tmp.push_back(num);
        }
        vect.push_back(tmp);
    }
    Print2DArray();
    TwoDimSort();
    Print2DArray();
    getchar();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我认为您可以使用以下实现。

void swap(double array[], int i, int j) {
  auto h = array[i];
  array[i] = array[j];
  array[j] = h;
}

int partition(double array[], int low, int high) {
  //  Assign the last element to the pivot element
  int pivot = array[high];
  //  Index of smaller element
  int lowIndex = (low - 1);
  //  Iterate from lowest to highest element
  for (int j = low; j <= high - 1; j++) {

    // Swap elements if j-th element is smaller than the pivot element
    if (array[j] < pivot || array[j] == pivot) {
      lowIndex++;
      swap(array, lowIndex, j);
    }
  }
  swap(array, lowIndex + 1, high);
  return (lowIndex + 1);
}

double *quickSort(double array[], int lo, int hi) {
  if (lo < hi) {
    int pi = partition(array, lo, hi);
    //  Recursively sort smaller half of the list
    quickSort(array, lo, pi - 1);
    //  Recursively sort higher half of the list
    quickSort(array, pi + 1, hi);
  }
  //  Return sorted list
  return array;
}

在这种情况下,全局变量是不好的做法!

void Print2DArray(std::vector<std::vector<double>> vect) {
    for (int i = 0;i < vect.size();++i) {
        for (int j = 0;j < vect.at(i).size();++j) {
            std::cout << vecta.at(i).at(j) << " ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}
void TwoDimSort(std::vector<std::vector<double>> vect) {
    for (int i = 0; i < vect.size(); ++i) {
        // Print the sorted row
        print(quickSort(0, vect.at(i).size() - 1, i));
    }   
}

void print(std::vector<double> v){
    for( auto d : v){
        std::cout << d << " ";    
    }
    std::cout << std::endl;
}

您应该考虑使用此打印功能实现quickSort。随后,由于范围检查的原因,我建议您使用vector::at而不是[]