forked from kalaskarpranav/Java-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraySort.java
More file actions
47 lines (41 loc) · 1.34 KB
/
arraySort.java
File metadata and controls
47 lines (41 loc) · 1.34 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
import java.util.Scanner;
public class Main {
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int[] myIntegers = getIntegers(5);
int[] sorted = sortIntegers(myIntegers);
printArray(sorted);
}
public static int[] getIntegers(int capacity){
int[] array = new int[capacity];
System.out.println("Enter " + capacity +" integer values: \r");
for(int i=0; i<array.length; i++) {
array[i] = scanner.nextInt();
}
return array;
}
public static void printArray(int[] array){
for(int i = 0; i <array.length; i++){
System.out.println("Element " + i + " Contents " + array[i]);
}
}
public 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;
}
}
}
return sortedArray;
}
}