-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection.java
More file actions
60 lines (53 loc) · 1.71 KB
/
Copy pathSelection.java
File metadata and controls
60 lines (53 loc) · 1.71 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
public class Selection {
public static void main(String[] args) {
String[] words = {"banana", "apple", "kiwi", "pear", "grape"};
System.out.println("Original String Array:");
printArray(words);
sortAlphabetically(words);
System.out.println("\nSorted String Array:");
printArray(words);
int[] arr = {5, 2, 8, 1, 6, 3};
System.out.println("\nOriginal Integer Array:");
printArray(arr);
sort(arr);
System.out.println("\nSorted Integer Array:");
printArray(arr);
}
public static void sort(int[] a){
for(int i = 0; i < a.length; i++){
int minIndex = i;
for(int j = i + 1; j < a.length; j++){
if(a[j] < a[minIndex])
minIndex = j;
}
int temp = a[minIndex];
a[minIndex] = a[i];
a[i] = temp;
}
}
public static void sortAlphabetically(String[] a) {
for (int i = 0; i < a.length; i++) {
int minIndex = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j].compareTo(a[minIndex]) < 0) {
minIndex = j;
}
}
String temp = a[minIndex];
a[minIndex] = a[i];
a[i] = temp;
}
}
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
public static void printArray(String[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println();
}
}