0

I just finished the bucket sort, it works fine without a for function:

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

but what I wan to do with this function is try to find the radii and set to the bucket. How what should I edit for it? Thanks for your view. Here is the whole code:

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

Here is the out put:

bucketSort_2.cpp:19:6: error: no match for 'operator=' (operand types are 'std::vector<double>' and 'double')
  b[i]=c;
      ^
BengDai
  • 51
  • 3
  • 10
  • This first thing I see when I look at this is that it is going to be difficult to maintain. The function is doing a lot of iteration, that may be able to be moved into it's own sub-functions and called. – Evan Bechtol Mar 02 '15 at 22:57
  • @EvanBechtol *"what I wan to do with this function is try to find the radii and set to the bucket. How what should I edit for it?"* seems to indicate that a change in code functionality is wanted, which is off-topic for Code Review. Code Review is about code that is already working as intended. Additionally, an error message has been added to the bottom of the question after your comment, so it is clear that this is not CR material. – Simon Forsberg Mar 02 '15 at 23:02
  • 1
    I think your entire "//fnid the ring and set bucket" section makes literally no sense. What are you trying to do there? – Mooing Duck Mar 02 '15 at 23:06
  • Removed my comment! Thanks for the notice – Evan Bechtol Mar 02 '15 at 23:14

0 Answers0