# Thread: problem with my bucket sort

1. Member
Join Date
Apr 2011
Posts
18
Rep Power
0

## problem with my bucket sort

import java.io.DataInputStream;
import java.io.FileInputStream;

/**
*
* @author sss
*/
public class tryyyy444 {

//method BUCKET_SORT(A)
public static int[] bucketSort(int[] A, int n){

//let B[0 .. n – 1] be a new array
int[][] B = new int[A.length-1][5];

//for i = 0 to n – 1
//make B[i] an empty list;
for (int i=0; i<B.length; i++){
B[i][5]=0;
}

//for i = 1 to n
// insert A[i] into list B[ floor(n . A[i]) ];

for (int i=0; i<A.length; i++){
// B[A[i]]++;
int t = (int) Math.floor(n*A[i]);
B[t][5] = A[i];
}
///============================= insertion Code ====
// for i = 0 to n – 1
//sort list B[i] with insertion sort;
//================================================== ==

for (int j = 1; j < B.length; j++) {
int key;
int i;
key = B[j][5];
i = j - 1;
while (i >= 0 && B[i][5] > key) {
B[i + 1] = B[i];
i = i - 1;
// count++;
}
B[i + 1][5] = key;

//concatenate the lists B[0], B[1], …, B[n-1] together in order;
int [] C = new int[i];
key = C[i];
}
return A;
}

2. Senior Member
Join Date
Aug 2010
Posts
127
Rep Power
0

3. Member
Join Date
Apr 2011
Posts
18
Rep Power
0
any one have any idea?

4. Senior Member
Join Date
Aug 2010
Posts
127
Rep Power
0
Yes, I have plenty of ideas.

5. Member
Join Date
Apr 2011
Posts
18
Rep Power
0
ideas of what?

I need a help on this code

6. Senior Member
Join Date
Aug 2010
Posts
127
Rep Power
0
Originally Posted by someone
ideas of what?
On various things.

Originally Posted by someone
I need a help on this code

Any more questions?

7. Member
Join Date
Apr 2011
Posts
34
Rep Power
0
What exactly is the problem? You might get better responses here if you provided more details of what you need help with.

8. Member
Join Date
Apr 2011
Posts
18
Rep Power
0
Hello

I tried to implement a bucket sort program using 2D array:

I modify it more:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package assignment3;

/**
*
* @author sssss
*/
public class mybookbucketsort {

public static int[] bucketsort(int [] A, int c){

int rows=A.length;
int [] bucket=new int [5];
int columns= bucket.length;
int [][] B= new int [rows][columns];

for( int i=0 ; i<rows-1 ; i ++){
for( i=1 ; i<columns ; i ++){

B[rows][columns]=0;
}
}
for (int i=0; i<A.length; i++){
// B[A[i]]++;
int t = (int) Math.floor(rows*A[i]);
B[rows][columns] = A[t];
}

// ====== insertion sort

for ( int i=0 ; i< B.length ; i++){

int key;
for (int j = 1; j < c; j++)
{
key = B[rows][columns];
i = j - 1;
while (i >= 0 && B[rows][columns] > key)
{
B[i + 1] = B[i];
i = i - 1;
B[rows][columns] = key;
}
}
}

return A;
}

public static void main(String[] args) {

int [] A = { 1,3,4,5};
bucketsort(A, 3);
System.out.println();
}
}

9. It's very important that you ask specific questions. Vague questions like "Here is my code, help" will not receive great attention..as you have seen. What is your problem? What doesn't work? How does it not work? What have you done to try to fix it? What does it produce? What were you expecting? Does it compile? If not, what compilation errors did you get(copy/paste EXACT errors, not paraphrasing)? What do you think they mean? What do you think can be done to fix the errors?

A lot of people here are not going to scan your code and give you the answer unless you put in the effort.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•