-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL7Q1_Alphabetical.java
83 lines (75 loc) · 1.75 KB
/
L7Q1_Alphabetical.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
/***
* 03-04-2020
*
* Lab 7: Question 1
*
* Description;
* Write a java program that takes in a String. Use Bubble sort to sort the
* letters of the String into alphabetical order. Print the alphabetically
* ordered String out.
*
* Sample Input 1: hello
* Sample Output 1: ehllo
*
* Algorithm:
*
* Step 1: Take in user input as String
*
* Step 2: Construct an array from characters of the String
*
* Step 3: Sort the array
*
* Step 4: Construct a String from the sorted array and print
*
***/
import java.util.Scanner;
public class L7Q1_Alphabetical
{
public static void main(String[] args)
{ //Step 1:
Scanner scan = new Scanner(System.in);
String input = scan.nextLine();
scan.close();
String[] a = makeArray(input);
bSort(a);
makeString(a);
} //End main
public static String[] makeArray(String input)
{ //Step 2:
input = input.toLowerCase(); //Make any input lowercase to avoid issues with .compareTo() method's evaluation
String[] a = new String[input.length()];
for(int i = 0 ; i < a.length; i++)
{
a[i] = "" + input.charAt(i);
}
return a;
} //End makeArray()
public static void bSort(String[] a)
{ //Step 3:
String t = a[0];
boolean isSorted = true;
for(int i = 0; i < a.length - 1; i++)
{
for(int j = i + 1; j < a.length; j++)
{
if(a[i].compareTo(a[j]) > 0)
{
t = a[j];
a[j] = a[i];
a[i] = t;
isSorted = false;
}
} //End inner-loop
if(isSorted) return;
} //End outer-loop
} //End bSort()
public static void makeString(String[] a)
{ //Step 4:
String s = new String("");
for(int i = 0; i < a.length; i++)
{
s += a[i];
}
System.out.println(s);
} //End makeString()
}//End class