public class BucketSort { public static int[] input; public static int length; public static void main(String[] args) { String inputString = "4143675351981074"; length = inputString.length(); input = new int[length]; int min = input[0]; int max = input[0]; for (int i = 0; i < length; i++) { input[i] = Character.getNumericValue(inputString.charAt(i)); if (input[i] < min) { min = input[i]; } if (input[i] > max) { max = input[i]; } } System.out.println("Unsorted list:"); printList(); System.out.println(); System.out.println("Min value: " + min); System.out.println("Max value: " + max); int numOfBuckets = (int) Math.sqrt(max - min) + 1; System.out.println("Number of buckets: " + numOfBuckets); System.out.println(); int[][] buckets = new int[numOfBuckets][length]; int[] counter = new int[numOfBuckets]; for (int i = 0; i < length; i++) { int bucketNum = input[i] / ((int) Math.sqrt(max - min)); buckets[bucketNum][counter[bucketNum]] = input[i]; counter[bucketNum]++; } for (int i = 0; i < numOfBuckets; i++) { int[] bucketArray = new int[counter[i]]; System.out.println("Bucket " + i + " unsorted:"); for (int j = 0; j < counter[i]; j++) { bucketArray[j] = buckets[i][j]; System.out.print(buckets[i][j] + " "); } System.out.println(); System.out.println("Bucket " + i + " sorted:"); InsertionSort_Implemented insertionSort = new InsertionSort_Implemented(bucketArray); bucketArray = insertionSort.insertionSort(); for (int a = 0; a < bucketArray.length; a++) { buckets[i][a] = bucketArray[a]; System.out.print(bucketArray[a] + " "); } System.out.println(); System.out.println(); } int overallCounter = 0; for (int i = 0; i < numOfBuckets; i++) { for (int j = 0; j < counter[i]; j++) { input[overallCounter] = buckets[i][j]; overallCounter++; } } System.out.println("Sorted list:"); printList(); } public static void printList() { for (int i = 0; i < input.length; i++) { System.out.print(input[i] + " "); } System.out.println(); } }
public class InsertionSort_Implemented { private static int[] input; private static int length; private static boolean isPrint = true; public static void main(String[] args) { InsertionSort_Implemented insertionSort = new InsertionSort_Implemented(); insertionSort.insertionSort(); } public static int[] insertionSort() { if (isPrint) { System.out.println("Unsorted list:"); printList(); System.out.println(); for (int i = 0; i < length; i++) { insert(input[i], i); printList(); System.out.println(); } System.out.println("Sorted list:"); printList(); } else { for (int i = 0; i < length; i++) { insert(input[i], i); } } return input; } public InsertionSort_Implemented() { String inputString = "4143675351981074"; length = inputString.length(); input = new int[length]; for (int i = 0; i < length; i++) { input[i] = Character.getNumericValue(inputString.charAt(i)); } } public InsertionSort_Implemented(int[] inputArray) { isPrint = false; input = inputArray; length = input.length; } public static void insert(int removed_value, int removed_index) { if (removed_value <= input[0]) { for (int i = removed_index; i >= 1; i--) { input[i] = input[i - 1]; } input[0] = removed_value; } else { int i; for (i = 1; i < removed_index - 1; i++) { if (removed_value >= input[i - 1] && removed_value <= input[i]) { break; } } if (i < removed_index - 1) { for (int j = removed_index; j >= i; j--) { input[j] = input[j - 1]; } input[i] = removed_value; } else if (i == removed_index - 1) { if (input[removed_index - 1] > input[removed_index]) { int temp = input[removed_index - 1]; input[removed_index - 1] = input[removed_index]; input[removed_index] = temp; } } } } public static void printList() { for (int i = 0; i < input.length; i++) { System.out.print(input[i] + " "); } System.out.println(); } }