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 Day32
{
public static void printSequence(int[] sorted_sequence)
{
for (int i = 0; i < sorted_sequence.length; i++)
System.out.print(sorted_sequence[i] + " ");
}
public static int[] shakerSort(int[] array) {
for (int i = 0; i < array.length/2; i++) {
boolean swapped = false;
for (int j = i; j < array.length - i - 1; j++) {
if (array[j] < array[j+1]) {
int tmp = array[j];
array[j] = array[j+1];
array[j+1] = tmp;
}
}
for (int j = array.length - 2 - i; j > i; j--) {
if (array[j] > array[j-1]) {
int tmp = array[j];
array[j] = array[j-1];
array[j-1] = tmp;
swapped = true;
}
}
if(!swapped) break;
}
return array;
}
public static void main(String args[])
{
System.out
.println("Sorting of a randomly generated array with shaker 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));
System.out.println("\nOriginal: ");
printSequence(sequence);
System.out.println("\nSorted: ");
printSequence(shakerSort(sequence));
}
}
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 32: Shaker Sort
暂无评论内容