I tried to bubble sort, array.sort and it does not verify. Any other ideas? Or, can you tell me what I am doing wrong?
How can I get the minimum and maximum without sorting the array? It seems I can not modify it.
package com.codegym.task.task11.task1123;
import java.util.Arrays;
public class Solution {
public static void main(String[] args) throws Exception {
int[] data = new int[]{1, 2, 3, 5, -2, -8, 0, 77, 5, 5};
Pair<Integer, Integer> result = getMinimumAndMaximum(data);
System.out.println("The minimum is " + result.x);
System.out.println("The maximum is " + result.y);
}
public static Pair<Integer, Integer> getMinimumAndMaximum(int[] array) {
if (array == null || array.length == 0) {
return new Pair<Integer, Integer>(null, null);
}
//write your code here
Arrays.sort(array);
/*
for(int j = array.length; j > 0; j--){
for(int e = 0; e < array.length-1; e++){
if(array[e]> array[e+1]){
int temp = array[e];
array[e] = array[e+1];
array[e+1]= temp;
}
}
}*/
int x = array[0];
int y = array[array.length-1];
return new Pair<Integer, Integer>(x,y);
}
public static class Pair<X, Y> {
public X x;
public Y y;
public Pair(X x, Y y) {
this.x = x;
this.y = y;
}
}
}