|
1 | | -// Patrick Yevsukov |
2 | | - |
3 | | -// 2013 CC BY-NC-SA 4.0 http://patrick.yevsukov.com |
4 | | - |
| 1 | +// Patrick Yevsukov |
| 2 | + |
| 3 | +// 2013 CC BY-NC-SA 4.0 http://patrick.yevsukov.com |
| 4 | + |
5 | 5 | // github.com/PatrickYevsukov/Sorting-Algorithms |
6 | | - |
7 | | -public static void RadixSort(ArrayList<Integer> list) { |
8 | 6 |
|
9 | | - final int NUM_BASE = 10; |
| 7 | +package PatrickYevsukov; |
10 | 8 |
|
11 | | - int maximum = 0; |
| 9 | +import java.util.ArrayList; |
12 | 10 |
|
13 | | - ArrayList<ArrayList<Integer>> buckets; |
| 11 | +public class radix_sort { |
14 | 12 |
|
15 | | - buckets = new ArrayList<ArrayList<Integer>>(NUM_BASE); |
| 13 | + public static void RadixSort(ArrayList<Integer> list) { |
16 | 14 |
|
17 | | - // New buckets are added to the bucket list; one for each digit |
18 | | - // in the numeral system of the data being sorted. |
19 | | - |
20 | | - for (int ii = 0; ii < NUM_BASE; ii++) { |
| 15 | + final int NUM_BASE = 10; |
21 | 16 |
|
22 | | - buckets.add(new ArrayList<Integer>()); |
23 | | - } |
| 17 | + int maximum = 0; |
24 | 18 |
|
25 | | - // Identifying the list item with the most place values. |
26 | | - |
27 | | - for (int ii = 0; ii < list.size(); ii++) { |
| 19 | + ArrayList<ArrayList<Integer>> buckets; |
28 | 20 |
|
29 | | - if (list.get(ii) > maximum) { |
| 21 | + buckets = new ArrayList<ArrayList<Integer>>(NUM_BASE); |
30 | 22 |
|
31 | | - maximum = list.get(ii); |
| 23 | + // New buckets are added to the bucket list; one for each digit |
| 24 | + // in the numeral system of the data being sorted. |
| 25 | + |
| 26 | + for (int ii = 0; ii < NUM_BASE; ii++) { |
| 27 | + |
| 28 | + buckets.add(new ArrayList<Integer>()); |
32 | 29 | } |
33 | | - } |
34 | 30 |
|
35 | | - // List items are sorted by place value. The place value is |
36 | | - // multiplied by the numeric base with each pass of the loop. |
37 | | - |
38 | | - for (int power = 1; maximum / power != 0; power *= NUM_BASE) { |
| 31 | + // Identifying the list item with the most place values. |
39 | 32 |
|
40 | 33 | for (int ii = 0; ii < list.size(); ii++) { |
41 | 34 |
|
42 | | - // List items are added to the bucket which corresponds |
43 | | - // to the place value which they are being sorted by. |
44 | | - |
45 | | - buckets.get(list.get(ii) / power % NUM_BASE) |
46 | | - .add(list.get(ii)); |
| 35 | + if (list.get(ii) > maximum) { |
| 36 | + |
| 37 | + maximum = list.get(ii); |
| 38 | + } |
47 | 39 | } |
48 | 40 |
|
49 | | - int index = 0; |
| 41 | + // List items are sorted by place value. The place value is |
| 42 | + // multiplied by the numeric base with each pass of the loop. |
| 43 | + |
| 44 | + for (int power = 1; maximum / power != 0; power *= NUM_BASE) { |
50 | 45 |
|
51 | | - for (int ii = 0; ii < buckets.size(); ii++) { |
| 46 | + for (int ii = 0; ii < list.size(); ii++) { |
52 | 47 |
|
53 | | - for (int jj = 0; jj < buckets.get(ii).size(); jj++) { |
| 48 | + // List items are added to the bucket which corresponds |
| 49 | + // to the place value which they are being sorted by. |
54 | 50 |
|
55 | | - // The buckets, sorted by the current place value |
56 | | - // under consideration, have their values written |
57 | | - // back to original list, overwriting its previous |
58 | | - // contents. |
59 | | - |
60 | | - list.set(index, buckets.get(ii).get(jj)); |
61 | | - index++; |
| 51 | + buckets.get(list.get(ii) / power % NUM_BASE).add(list.get(ii)); |
| 52 | + } |
| 53 | + |
| 54 | + int index = 0; |
| 55 | + |
| 56 | + for (int ii = 0; ii < buckets.size(); ii++) { |
| 57 | + |
| 58 | + for (int jj = 0; jj < buckets.get(ii).size(); jj++) { |
| 59 | + |
| 60 | + // The buckets, sorted by the current place value |
| 61 | + // under consideration, have their values written |
| 62 | + // back to original list, overwriting its previous |
| 63 | + // contents. |
| 64 | + |
| 65 | + list.set(index, buckets.get(ii).get(jj)); |
| 66 | + |
| 67 | + index++; |
| 68 | + } |
62 | 69 | } |
63 | | - } |
64 | 70 |
|
65 | | - // At the end of each pass of the loop, the contents of the |
66 | | - // buckets are dumped out. |
67 | | - |
68 | | - for (int ii = 0; ii < buckets.size(); ii++) { |
| 71 | + // At the end of each pass of the loop, the contents of the |
| 72 | + // buckets are dumped out. |
69 | 73 |
|
70 | | - buckets.get(ii).clear(); |
| 74 | + for (int ii = 0; ii < buckets.size(); ii++) { |
| 75 | + |
| 76 | + buckets.get(ii).clear(); |
| 77 | + } |
71 | 78 | } |
72 | 79 | } |
73 | | -} |
74 | | - |
| 80 | + |
75 | 81 | // Above is my implementation of a radix sort. I |
76 | 82 | // wrote it to be as human readable as possible. |
| 83 | + |
| 84 | +} |
0 commit comments