-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBucketSortv2.java
More file actions
110 lines (102 loc) · 3.74 KB
/
Copy pathBucketSortv2.java
File metadata and controls
110 lines (102 loc) · 3.74 KB
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
import java.io.*;
import java.util.*;
import java.security.SecureRandom;
public class BucketSortv2{
private static TreeMap<Character,ArrayList<String>> mainbucket=new TreeMap<Character,ArrayList<String>>();
private static String[] stored;
public static void main(String[] args){
int option=0,count=0,length=0;
while(option!=4){
System.out.println("This is a demo of Bucket Sort");
System.out.println("1. Generate Strings");
System.out.println("2. View Strings");
System.out.println("3. Sort Strings");
System.out.println("4. Exit");
System.out.print("Mode : ");
Scanner modeInput= new Scanner(System.in);
option=modeInput.nextInt();
switch(option){
case 1:
System.out.print("How many characters you want? ");
Scanner lenInput= new Scanner(System.in);
length=lenInput.nextInt();
System.out.print("How many strings you want? ");
Scanner countInput= new Scanner(System.in);
count=countInput.nextInt();
stored=new String[count];
inputArray(count, length);
System.out.println();
break;
case 2:
System.out.println();
showStrings();
System.out.println();
break;
case 3:
readArray(count);
sortStrings();
moveToArray();
System.out.println();
break;
case 4:
modeInput.close();
break;
default:
System.out.println("Wrong input");
System.out.println();
}
}
}
private static void inputArray(int count,int length){
for(int x=0;x<count;x++){
stored[x]=generateStrings(length);
}
System.out.println("Strings has been generated");
}
private static void showStrings(){
for(String res:stored){
System.out.print(res+" ");
}
}
private static String generateStrings(int length){
String listRandom="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ";
StringBuffer RandomStr=new StringBuffer(length);
SecureRandom secRandom=new SecureRandom();
for(int x=0;x<length;x++){
RandomStr.append(listRandom.charAt(secRandom.nextInt(listRandom.length())));
}
return RandomStr.toString();
}
private static void readArray(int count){
String value;
Character key;
int x=0;
while(x<count){
value=stored[x];
key=Character.toLowerCase(value.charAt(0));
if(mainbucket.get(key)==null){
mainbucket.put(key,new ArrayList<String>());
}
mainbucket.get(key).add(value);
x++;
}
}
private static void sortStrings(){
for(Map.Entry<Character,ArrayList<String>> itr:mainbucket.entrySet()){
Collections.sort(itr.getValue());
}
System.out.println("Strings has been sorted");
}
private static void moveToArray(){
int x=0,y;
for(Map.Entry<Character,ArrayList<String>> itr:mainbucket.entrySet()){
ArrayList<String> temp = itr.getValue();
int z=temp.size()-1;
y=0;
while(z>=0){
stored[x++]=temp.get(y++);
z--;
}
}
}
}