Selection sort not sorting integer array

140
April 26, 2021, at 2:20 PM

I'm trying to implement a selection sort that sorts an integer array but it doesn't do that. I can't figure out what's wrong so maybe another set of eyes can figure it out.

public static void main(String[] args) {
    int[] array = {900, 200, 23, -3, 1, 30, 55, -70, 100, 9};
    System.out.println(Arrays.toString(array));
    for (int i = array.length - 1; i > 0; i--) {
        int largest = 0;
        for (int j = 1; j <= i; j++) {
            if (array[j] > array[largest]) {
            }
            largest = j;
        }
        swap(array, largest, i);
    }
    System.out.println(Arrays.toString(array));
}
private static void swap(int[] arr, int i, int j) {
    if (i == j)
        return;
    int temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}
Answer 1

Move largest = j into the if block

      if(array[j] > array[largest]){
        largest = j;
      }
READ ALSO
How To Implement Screen Recording Feature In Asp.Net MVC

How To Implement Screen Recording Feature In Asp.Net MVC

As part of recently assigned research and development task, I am looking for ways to implement screen recording functionality in AspNet MVC

118
Determining the Euclidean Distance of all tuples against each other in MySQL

Determining the Euclidean Distance of all tuples against each other in MySQL

Say I have a table T with two columns, X and Y, and I want to find all the pairs of tuples where the result of calculating their Euclidean Distance using each tuples X and Y is equal to some value D

101
Why is SimpleImputer in ColumnTransformer creating additional Columns?

Why is SimpleImputer in ColumnTransformer creating additional Columns?

I am following the machine learning book from Aurelion Geron

180