-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
79 lines (64 loc) · 2.38 KB
/
SelectionSort.java
File metadata and controls
79 lines (64 loc) · 2.38 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
public class SelectionSort {
public static void IncreasingSelectionSort (int a[]) {
for (int j = 0 ; j < a.length ; j++){ // n
int min = a[j];
int minIndex = j;
for (int i = j ; i < a.length ; i++){ // n; n inside n is n^2
if (a[i] < min) {
min = a[i];
minIndex = i;
}
}
int temp = a[j];
a[j] = min;
a[minIndex] = temp;
}
}
public static void DecreasingSelectionSort (int a[]) {
for (int j = 0; j < a.length; j++) {
int max = a[j];
int maxIndex = j;
for (int i = j; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
maxIndex = i;
}
}
int temp = a[j];
a[j] = max;
a[maxIndex] = temp;
}
}
public static void SortingStrings (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) { // compareTo is a method to sort strings
minIndex = j;
}
}
String temp = a[minIndex];
a[minIndex] = a[i];
a[i] = temp;
}
}
public static void main(String[] args) {
int arr[] = {6,3,2,8,6,1,9,4};
IncreasingSelectionSort(arr);
System.out.println("Increasing: ");
for (int i = 0 ; i < arr.length ; i++){
System.out.print(arr[i] + " ");
}
DecreasingSelectionSort(arr);
System.out.println("\nDecreasing: ");
for (int i = 0 ; i < arr.length ; i++){
System.out.print(arr[i] + " ");
}
String arr2[] = {"y" , "a" , "l" , "c" , "z" , "k" , "v" , "j"};
SortingStrings(arr2);
System.out.println("\nAlphabetical Order:");
for (int i = 0; i < arr2.length; i++) {
System.out.print(arr2[i] + " ");
}
}
}