Thursday, September 14, 2017

Bucket Sort in Java

Here is the implementation of Bucket Sort using Java as my programming language. Our program will ask the user how many items to be sorted and then it will ask the user to give a series of numbers and then it will be sorted using selection sort algorithm. I hope you will find my work useful. 

I am currently accepting programming work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.





Sample Program Output


Program Listing

Bucket_Sorting.java

/**
 * 
 */
package bucket_sort;

import java.util.Scanner;

/**
 * @author Jake R. Pomperada
 * Bacolod City, Negros Occidental, Philippines
 * September 14, 2017  Thursday
*/


public class Bucket_Sorting {

/**
* @param args
*/
public int[] bucket_sort_demo(int[] array) {
    
   int[] bucket = new int[10000];   

 
   for (int i = 0; i < array.length; i++)
       bucket[array[i]]++;

   int outPos = 0;
   for (int i = 0; i < bucket.length; i++) { 
    if (bucket[i] > 0)
        array[outPos++] = i;
   }
    
   return array;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
 
Bucket_Sorting bSort = new Bucket_Sorting();      
System.out.print("\n\n");
System.out.print("===== Bucket Sort in Java ====");
System.out.print("\n\n");
System.out.print("===== Written By: Mr. Jake R. Pomperada =====");;
System.out.print("\n\n");
System.out.print("How many items? : ");
        int num = input.nextInt();
        int numbers[] = new int[num];

System.out.println();
        for (int i = 0; i < num; i++) {
            System.out.print ("Give value in item no. " + (i+1) + " : ");
            numbers[i] = input.nextInt();
        }

        System.out.print("\n\n");
System.out.print("Original Number Arrangements" );
System.out.print("\n\n");
        for (int temp : numbers){
        System.out.print(temp);
            System.out.print(", ");
        } 
        System.out.print("\n\n");
System.out.print("Sorted Number Arrangements" );
System.out.print("\n\n");
        
int[] sortedArray = bSort.bucket_sort_demo(numbers);
 
        for(int a:sortedArray){
            System.out.print(a);
            System.out.print(", ");
}
        input.close();
}

}




No comments:

Post a Comment