# problem with my bucket sort

• 04-11-2011, 03:21 PM
someone
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;
}
• 04-11-2011, 03:31 PM
imorio
• 04-11-2011, 03:36 PM
someone
any one have any idea?
• 04-11-2011, 03:39 PM
imorio
Yes, I have plenty of ideas.
• 04-11-2011, 03:52 PM
someone
ideas of what?

I need a help on this code
• 04-11-2011, 03:55 PM
imorio
Quote:

Originally Posted by someone
ideas of what?

On various things.

Quote:

Originally Posted by someone
I need a help on this code

Any more questions?
• 04-11-2011, 04:12 PM
snotmare
What exactly is the problem? You might get better responses here if you provided more details of what you need help with.
• 04-11-2011, 05:33 PM
someone
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();
}
}
• 04-11-2011, 11:09 PM
sunde887
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.