-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
58 lines (44 loc) · 1.67 KB
/
Copy pathMain.java
File metadata and controls
58 lines (44 loc) · 1.67 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
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// System.out.println(Arrays.toString(getIntegers(5)));
int[] unsortedArray = getIntegers(5);
System.out.println(Arrays.toString(unsortedArray));
int[] sortedArray = sortIntegers(unsortedArray);
System.out.println(Arrays.toString(sortedArray));
printArray(sortedArray);
}
public static int[] getIntegers(int size) {
Scanner s = new Scanner(System.in);
int[] myArray = new int[size];
for (int i = 0; i < size; i++) {
System.out.println("Please enter the integer for the " + i + " position index of your array: ");
myArray[i] = s.nextInt();
}
return myArray;
}
public static int[] sortIntegers(int[] unsortedArray) {
int[] sortedArray = Arrays.copyOf(unsortedArray, unsortedArray.length);
boolean flag = true;
int temp;
while (flag) {
flag = false;
for (int i = 0; i < sortedArray.length -1; i++) {
if (sortedArray[i] < sortedArray[i + 1]) {
temp = sortedArray[i];
sortedArray[i] = sortedArray[i + 1];
sortedArray[i + 1] = temp;
flag = true;
}
}
}
return sortedArray;
}
public static void printArray(int[] sortedArray) {
int[] printArray = Arrays.copyOf(sortedArray, sortedArray.length);
for (int i = 0; i < printArray.length; i++) {
System.out.println("Element " + i + " contents " + printArray[i]);
}
}
}