Problem 120 (Selection sort)

Back to General discussions forum

KARASIQUE99     2019-11-14 13:22:57

Hello everybody! Can't understand what's wrong with my solution. In the example case it works absolutely correctly, but when i try to submit my solution it breaks somewhere.

Here's my solution (on Java):

import java.util.*;

public class Main { public static void main(String[] args) { System.out.println(task());

}

public static String task(){
    StringBuilder out = new StringBuilder();
    Scanner s = new Scanner(System.in);
    s.nextLine();
    int[] array = getIntArray(s.nextLine());

    for(int i = array.length-1; i>0 ;i--){
        int index = maxIndex(array,i);
        int max = array[index];
        int t = array[i];
        array[i] = max;
        array[index] = t;
        out.append(index);
        out.append(" ");
    }


    return out.toString().trim();
}

public static int maxIndex(int[] array, int end){
    int max = array[0],  index=0;
    for(int i = 0; i < end; i++){
        if(array[i]>max){
            max = array[i];
            index = i;
        }
    }
    return index;
}

public static int[] getIntArray(String input){
    String[] values = input.split(" ");
    int[] outputArray = new int[values.length];
    for(int i = 0;i<values.length;i++){
        outputArray[i] = Integer.parseInt(values[i]);
    }
    return outputArray;
}

}

Please login and solve 5 problems to be able to post at forum