-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount_Sort.java
49 lines (38 loc) · 1.42 KB
/
Count_Sort.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
import javax.swing.JOptionPane;
import java.util.*;
public class Count_Sort {
public static void main(){
// get the array size from the user
int size = 0;
while(size <= 0){
size = Integer.parseInt(JOptionPane.showInputDialog("How big is the array you want to sort?"));
if(size <= 0){
JOptionPane.showMessageDialog(null, "Array size must be greater than 0");
}
}
// create a new array and populate each element with the users input
double[] array = new double[size];
for(int i = 0;i < size; i++){
array[i] = Double.parseDouble(JOptionPane.showInputDialog("Please enter element " + (i + 1) + " for this array"));
}
// output the original array
System.out.print("\n Original Array: " + array[0]);
for(int i = 1; i < size; i++){
System.out.print(", " + array[i]);
}
// sort the array
countSort(array, size);
// output the sorted array
System.out.print("\n Sorted Array: " + array[0]);
for(int i = 1; i < size; i++){
System.out.print(", " + array[i]);
}
}
static void countSort(double[] array, int size){
double[] c = new double[size+1];
Arrays.fill(c, 0);
for(int i : input){
c[i] += 1;
}
}
}