繁体   English   中英

如何使用快速排序对二维双打向量进行排序?

[英]How can I sort a 2D vector of doubles with Quick Sort?

我有一个二维向量,我需要使用快速排序对其进行排序。 但是,当我打印所有步骤时,它似乎没有按应有的方式工作。 我的向量是一个全局变量,我尝试对每一行进行排序并在每次迭代后打印当前向量。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM