package com.codegym.task.task08.task0826;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int[] array = new int[20];
for (int i = 0; i < array.length; i++) {
array[i] = Integer.parseInt(reader.readLine());
}
sort(array);
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
System.out.println(array[3]);
System.out.println(array[4]);
}
public static void sort(int[] array) {
//write your code here
Arrays.sort(array);
int arrayLength = array.length;
int lastIndex = arrayLength - 1;
int numOfIterations = arrayLength / 2;
for(int i = 0; i <= numOfIterations; i++){
int temp = array[lastIndex];
array[lastIndex] = array[i];
array[i] = temp;
lastIndex--;
}
}
}
I get expected results , but CodeGym validator does not validate. Do you have ideas?
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Alexandru Ovcinicov
27 July 2019, 00:46
i found another way of solving this, i called it genius algorythm:
1) sort container
2) elements * -1
3) sort container
4) elements * -1
Voila, if u need more , call me
0
Rupa Joshi
25 July 2019, 01:43useful
as array is already sorted, i don't think you need i <= numOfIterations rather just try with i < numOfIterations otherwise I don't see any problem with the code.
+1