100 Days of Code (48 Part Series)
1 Day 1: Two Sum
2 Day 2: Check Prime
… 44 more parts…
3 Day 3: Hex Colour
4 Day 4: Repeating Numbers
5 Day 5: Nearest Prime Numbers
6 Day 6: Anagram Detector
7 Day 7: Poker ID
8 Day 8: Bubble Sort
9 Day 9: Password Generator
10 Day 10: Array Search
11 Day 11: Rock, Paper, Scissors
12 Day 12: List Shuffle
13 Day 13: Validate ISBN
14 Day 14: HTML Markup Generator
15 Day 15: Pascal’s Triangle
16 Day 16: Pascal’s Pyramid
17 Day 17: d20 Dice Roller
18 Day 18: Base64
19 Day 19: Vigenère cipher
20 Day 20: Seven-segment display
21 Day 21: Insertion Sort
22 Day 22: Binary-Decimal
23 Day 23: Ducci sequence
24 Day 24: Shell Sort
25 Day 25: Calculator
26 Day 26: Merge Sort
27 Day 27: Bucket Sort
28 Day 28: Counting Sort
29 Day 29: Heap Sort
30 Day 30: Quick Sort
31 Day 31: Radix Sort
32 Day 32: Shaker Sort
33 Day 33: Stooge Sort
34 Day 34: Consuming a RESTful API with React
35 Day 35: Adding Machine
36 Day 36: React Form Validation
37 Day 37: Time Format Converter
38 Day 38: React Stopwatch
39 Day 39: Blackjack Checker
40 Day 40: Password Guessing Game
41 Day 41: String Case
42 Day 42: Tip Calculator
43 Day 43: Twitter Auto-reply
44 Day 44: React Currency Converter
45 Day 45: Phonebook
46 Day 46: Sudoku Generator
47 Day 47: Binary Search Tree
48 Day 48: AVL Tree
import java.util.Random;
public class Bucket_Sort
{
static int[] sort(int[] sequence, int maxValue)
{
// Bucket Sort
int[] Bucket = new int[maxValue + 1];
int[] sorted_sequence = new int[sequence.length];
for (int i = 0; i < sequence.length; i++)
Bucket[sequence[i]]++;
int outPos = 0;
for (int i = 0; i < Bucket.length; i++)
for (int j = 0; j < Bucket[i]; j++)
sorted_sequence[outPos++] = i;
return sorted_sequence;
}
static void printSequence(int[] sorted_sequence)
{
for (int i = 0; i < sorted_sequence.length; i++)
System.out.print(sorted_sequence[i] + " ");
}
static int maxValue(int[] sequence)
{
int maxValue = 0;
for (int i = 0; i < sequence.length; i++)
if (sequence[i] > maxValue)
maxValue = sequence[i];
return maxValue;
}
public static void main(String args[])
{
System.out
.println("Sorting of randomly generated numbers using BUCKET SORT");
Random random = new Random();
int N = 20;
int[] sequence = new int[N];
for (int i = 0; i < N; i++)
sequence[i] = Math.abs(random.nextInt(100));
int maxValue = maxValue(sequence);
System.out.println("\nOriginal Sequence: ");
printSequence(sequence);
System.out.println("\nSorted Sequence: ");
printSequence(sort(sequence, maxValue));
}
}
Enter fullscreen mode Exit fullscreen mode
100 Days of Code (48 Part Series)
1 Day 1: Two Sum
2 Day 2: Check Prime
… 44 more parts…
3 Day 3: Hex Colour
4 Day 4: Repeating Numbers
5 Day 5: Nearest Prime Numbers
6 Day 6: Anagram Detector
7 Day 7: Poker ID
8 Day 8: Bubble Sort
9 Day 9: Password Generator
10 Day 10: Array Search
11 Day 11: Rock, Paper, Scissors
12 Day 12: List Shuffle
13 Day 13: Validate ISBN
14 Day 14: HTML Markup Generator
15 Day 15: Pascal’s Triangle
16 Day 16: Pascal’s Pyramid
17 Day 17: d20 Dice Roller
18 Day 18: Base64
19 Day 19: Vigenère cipher
20 Day 20: Seven-segment display
21 Day 21: Insertion Sort
22 Day 22: Binary-Decimal
23 Day 23: Ducci sequence
24 Day 24: Shell Sort
25 Day 25: Calculator
26 Day 26: Merge Sort
27 Day 27: Bucket Sort
28 Day 28: Counting Sort
29 Day 29: Heap Sort
30 Day 30: Quick Sort
31 Day 31: Radix Sort
32 Day 32: Shaker Sort
33 Day 33: Stooge Sort
34 Day 34: Consuming a RESTful API with React
35 Day 35: Adding Machine
36 Day 36: React Form Validation
37 Day 37: Time Format Converter
38 Day 38: React Stopwatch
39 Day 39: Blackjack Checker
40 Day 40: Password Guessing Game
41 Day 41: String Case
42 Day 42: Tip Calculator
43 Day 43: Twitter Auto-reply
44 Day 44: React Currency Converter
45 Day 45: Phonebook
46 Day 46: Sudoku Generator
47 Day 47: Binary Search Tree
48 Day 48: AVL Tree
原文链接:Day 27: Bucket Sort
暂无评论内容