如何使用bucket排序来对c ++中的负数进行排序

时间:2015-03-02 22:55:16

标签: c++ bucket-sort

我刚刚完成了铲斗排序,没有for功能就可以正常工作:

for (int i=1; i<=n; i++)
    {
    double c = sqrt(i)/sqrt(n);
    b[i]=c;
    cout<<c<<endl;
    }

但我要对这个功能做的是尝试找到半径并设置为桶。我该怎么编辑呢?谢谢你的看法。这是完整的代码:

    void bucketSort(vector<double> &arr, int n)
    {
    // 1) Create n empty buckets
    vector<double> b[n];

    //fnid the ring and set bucket
    for (int i=1; i<=n; i++)
    {
    double c = sqrt(i)/sqrt(n);
    b[i]=c;
    cout<<c<<endl;
    }

    // 2) Put array elements in different buckets
    for (int i=0; i<n; i++)
    {
       int bi = n*arr[i]; // Index in bucket
       b[bi].push_back(arr[i]);
    }

    // 3) Sort individual buckets
    for (int i=0; i<n; i++)
    {
       sort(b[i].begin(), b[i].end());
    }

    // 4) Concatenate all buckets into arr[]
    int index = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < b[i].size(); j++)
    {
          arr[index++] = b[i][j];
    }
    }
}

/* Driver program to test above funtion */
int main()
{
    vector<double> A;

    double numbers;

    while (cin>>numbers)
    {
    A.push_back(numbers);
    }

    int n = A.size();
    bucketSort(A, n);
    cout<<"Sort numbers: "<<endl;

    for (int i=0; i<n; i++)
    {
    cout<<A[i]<<" ";
    }
}

这是输出:

bucketSort_2.cpp:19:6: error: no match for 'operator=' (operand types are 'std::vector<double>' and 'double')
  b[i]=c;
      ^

0 个答案:

没有答案
相关问题