-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedArray.java
More file actions
67 lines (61 loc) · 2.49 KB
/
Copy pathSortedArray.java
File metadata and controls
67 lines (61 loc) · 2.49 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
package Arrays;
import java.util.Arrays;
import java.util.Scanner;
public class SortedArray {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
/*
Create a program using arrays that sorts a list of integers in descending order.
Descending order is highest value to lowest.
In other words if the array had the values in it 106, 26, 81, 5, 15 your program should
ultimately have an array with 106, 81, 26, 15, 5 in it.
Set up the program so that the numbers to sort are read in from the keyboard.
Implement the following methods - getIntegers, printArray, and sortIntegers
getIntegers returns an array of entered integers from keyboard
printArray print out the contents of the array
sortIntegers should sort the array and return a new array containing the sorted numbers
you will have to figure out how to copy the array elements from the passed array into a
new array and sort them and return the new sorted array.
*/
int[] myIntegers = getIntegers(5);
// System.out.println(Arrays.toString(myIntegers));
int[] sorted = sortIntegers(myIntegers);
System.out.println(Arrays.toString(sorted));
printArray(sorted);
}
private static int[] getIntegers(int number) {
System.out.println("Enter " + number + " integer values.\r");
int[] array = new int[number];
for (int i = 0; i < array.length; i++) {
array[i] = scanner.nextInt();
}
scanner.close();
System.out.println(Arrays.toString(array));
return array;
}
private static int[] sortIntegers(int[] array) {
int[] sortedArray = new int[array.length];
for (int i = 0; i < array.length; i++) {
sortedArray[i] = array[i];
}
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;
}
private static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.println("Element " + i + " contents " + array[i]);
}
}
}