-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
125 lines (111 loc) · 3.89 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.*;
import BreezySwing.*;
/*
Arjun Shah
1/6/2020
B Block
Program Description:
This program takes in a list of numbers and sorts it using
radix sort. Works for negative numbers as well.
*/
public class Main extends GBFrame {
// GUI components
private JLabel inputL, sortedL, ecL;
private JTextField inputF, sortedF;
private JButton sortB;
// default constructor for setting GUI components
public Main() {
inputL = addLabel("Numbers", 1, 1, 1, 1);
inputF = addTextField("", 1, 2, 1, 1);
sortB = addButton("Sort", 3, 1, 1, 1);
sortedL = addLabel("Sorted", 2, 1, 1, 1);
ecL = addLabel("Extra Credit - Works with negative numbers", 3, 2, 1, 1);
sortedF = addTextField("", 2, 2, 1, 1);
sortedF.setEditable(false);
}
// performing actions when buttons are clicked
public void buttonClicked(JButton button) {
if (button == sortB) { // when sort button is clicked
try {
// parse numbers into list
ArrayList<Integer> numbers = parse(inputF.getText());
// split numbers into postive and negative
ArrayList<Integer> positiveList = getPositive(numbers);
ArrayList<Integer> negativeList = getNegative(numbers);
// make all negative numbers positive
for (int i = 0; i < negativeList.size(); i++) {
negativeList.set(i, negativeList.get(i) * -1);
}
// sort positive and negative lists
ArrayList<Integer> sortedPositiveList = new ArrayList<Integer>();
ArrayList<Integer> sortedNegativeList = new ArrayList<Integer>();
Radix r2 = new Radix(negativeList);
Radix r1 = new Radix(positiveList);
if (!positiveList.isEmpty()) { // sort if there are positive numbers
sortedPositiveList = r1.radixSort();
}
if (!negativeList.isEmpty()) { // sort if there are negative numbers
sortedNegativeList = r2.radixSort();
}
// change numbers back to negative and reverse order
for (int i = 0; i < sortedNegativeList.size(); i++) {
sortedNegativeList.set(i, sortedNegativeList.get(i) * -1);
}
Collections.reverse(sortedNegativeList);
sortedNegativeList.addAll(sortedPositiveList);
sortedF.setText(sortedNegativeList + "");
} catch (Exception e) {
messageBox("Error! Invalid Inputs!");
}
}
}
// parsing the string into individual numbers
private static ArrayList<Integer> parse(String dataSet) {
String number = "";
ArrayList<Integer> numbers = new ArrayList<Integer>();
// looping through each character of string
for (int i = 0; i < dataSet.length(); i++) {
if (dataSet.charAt(i) != ',') { // if character not a comma
number += "" + dataSet.charAt(i); // add it to the number
} else if (dataSet.charAt(i) != ' ') { // if a comma is reached
// add the whole number to the array of numbers
numbers.add(Integer.parseInt(number.trim()));
number = "";
}
}
// add last number reached to the array
numbers.add(Integer.parseInt(number.trim()));
return numbers;
}
// getting positive numbers from the list
private static ArrayList<Integer> getPositive(ArrayList<Integer> nums) {
ArrayList<Integer> positive = new ArrayList<Integer>();
// add only positive numbers
for (int i = 0; i < nums.size(); i++) {
if (nums.get(i) >= 0) {
positive.add(nums.get(i));
}
}
return positive;
}
// getting negative numbers from the list
private static ArrayList<Integer> getNegative(ArrayList<Integer> nums) {
ArrayList<Integer> negative = new ArrayList<Integer>();
// add only negative numbers
for (int i = 0; i < nums.size(); i++) {
if (nums.get(i) < 0) {
negative.add(nums.get(i));
}
}
return negative;
}
// main method
public static void main(String[] args) {
Main theGUI = new Main();
theGUI.setSize(1000, 150);
theGUI.setVisible(true);
theGUI.setTitle("Radix Sort");
}
}