snippetjavaMinor
Bucket sort implementation
Viewed 0 times
sortimplementationbucket
Problem
I want to optimize my code for performance and fine-tuned logic.
```
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Collections;
import java.util.Comparator;
/**
* Created by ShrivasA on 1/30/2015.
*/
/* Take an Element and Apply the following conditions:
1. The element should not already be there.
2. The element should have an element which is last element-1 otherwise create a new one.
3. in case the element is eligible in all the lists add the element in the list which has least number of elements.
*/
public class TeamFormation {
/**
* Bucket sort
* @param array array to be sorted
* @param bucketCount number of buckets
* @return array sorted in ascending order
*/
public static int[] bucketSort(int[] array, int bucketCount) {
if (bucketCount high) high = array[i];
if (array[i] buckets[] = new ArrayList[bucketCount];
for (int i = 0; i 0) {
//Scanner inputTeam = (new Scanner(System.in));
// startTime= System.currentTimeMillis();
Integer lng = scanInput.nextInt();
if(lng!=0){
int[] sortArray= new int[lng];
long stime=System.currentTimeMillis();
for (int i = 0; i lst = new ArrayList(5);
int len = sortArray.length;
//System.out.println("Array Size::::"+len);
for (int i = 0; i temp = new ArrayList(lst);
for (List strlist : temp) {
if (!strlist.contains(test) && strlist.contains(test - 1)) {
strlist.add(test);
lst.add(strlist);
test = 0;
}
}
//lst.toArray();
if (test != 0) {
List
```
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Collections;
import java.util.Comparator;
/**
* Created by ShrivasA on 1/30/2015.
*/
/* Take an Element and Apply the following conditions:
1. The element should not already be there.
2. The element should have an element which is last element-1 otherwise create a new one.
3. in case the element is eligible in all the lists add the element in the list which has least number of elements.
*/
public class TeamFormation {
/**
* Bucket sort
* @param array array to be sorted
* @param bucketCount number of buckets
* @return array sorted in ascending order
*/
public static int[] bucketSort(int[] array, int bucketCount) {
if (bucketCount high) high = array[i];
if (array[i] buckets[] = new ArrayList[bucketCount];
for (int i = 0; i 0) {
//Scanner inputTeam = (new Scanner(System.in));
// startTime= System.currentTimeMillis();
Integer lng = scanInput.nextInt();
if(lng!=0){
int[] sortArray= new int[lng];
long stime=System.currentTimeMillis();
for (int i = 0; i lst = new ArrayList(5);
int len = sortArray.length;
//System.out.println("Array Size::::"+len);
for (int i = 0; i temp = new ArrayList(lst);
for (List strlist : temp) {
if (!strlist.contains(test) && strlist.contains(test - 1)) {
strlist.add(test);
lst.add(strlist);
test = 0;
}
}
//lst.toArray();
if (test != 0) {
List
Solution
Code which is commented out is dead code which should be deleted to increase readability.
Using correct indention will add readability.
looks strange and not readable while
looks much more readable. Your IDE will have some kind of keyboard shortcut to automatically format your source.
You should always code against interfaces rather than against concrete implementations.
should be
Using braces
Variables should be declared as near as possible to their usage.
should be
You should let your variables have some space to breathe.
should be
Using correct indention will add readability.
while (testCases > 0) {
Integer lng = scanInput.nextInt();
if(lng!=0){
int[] sortArray= new int[lng];
long stime=System.currentTimeMillis();
for (int i = 0; i < lng; i++) {
Integer test = scanInput.nextInt();
sortArray[i] = test;
}looks strange and not readable while
while (testCases > 0) {
Integer lng = scanInput.nextInt();
if(lng!=0){
int[] sortArray= new int[lng];
long stime=System.currentTimeMillis();
for (int i = 0; i < lng; i++) {
Integer test = scanInput.nextInt();
sortArray[i] = test;
}looks much more readable. Your IDE will have some kind of keyboard shortcut to automatically format your source.
You should always code against interfaces rather than against concrete implementations.
ArrayList buckets[] = new ArrayList[bucketCount];should be
List buckets[] = new ArrayList[bucketCount];Using braces
{} for single line if statements also, makes your code less error prone. Variables should be declared as near as possible to their usage.
double interval = ((double)(high - low + 1))/bucketCount; //range of one bucket
ArrayList buckets[] = new ArrayList[bucketCount];
for (int i = 0; i < bucketCount; i++) { //initialize buckets
buckets[i] = new ArrayList();
}
for (int i = 0; i < array.length; i++) { //partition the input array
buckets[(int)((array[i] - low)/interval)].add(array[i]);
}should be
ArrayList buckets[] = new ArrayList[bucketCount];
for (int i = 0; i < bucketCount; i++) { //initialize buckets
buckets[i] = new ArrayList();
}
double interval = ((double)(high - low + 1))/bucketCount; //range of one bucket
for (int i = 0; i < array.length; i++) { //partition the input array
buckets[(int)((array[i] - low)/interval)].add(array[i]);
}You should let your variables have some space to breathe.
double interval = ((double) (high - low + 1))/bucketCount;should be
double interval = ((double) (high - low + 1)) / bucketCount;Code Snippets
while (testCases > 0) {
Integer lng = scanInput.nextInt();
if(lng!=0){
int[] sortArray= new int[lng];
long stime=System.currentTimeMillis();
for (int i = 0; i < lng; i++) {
Integer test = scanInput.nextInt();
sortArray[i] = test;
}while (testCases > 0) {
Integer lng = scanInput.nextInt();
if(lng!=0){
int[] sortArray= new int[lng];
long stime=System.currentTimeMillis();
for (int i = 0; i < lng; i++) {
Integer test = scanInput.nextInt();
sortArray[i] = test;
}ArrayList<Integer> buckets[] = new ArrayList[bucketCount];List<Integer> buckets[] = new ArrayList[bucketCount];double interval = ((double)(high - low + 1))/bucketCount; //range of one bucket
ArrayList<Integer> buckets[] = new ArrayList[bucketCount];
for (int i = 0; i < bucketCount; i++) { //initialize buckets
buckets[i] = new ArrayList();
}
for (int i = 0; i < array.length; i++) { //partition the input array
buckets[(int)((array[i] - low)/interval)].add(array[i]);
}Context
StackExchange Code Review Q#82577, answer score: 6
Revisions (0)
No revisions yet.