Home > OS >  Swapping lowest value in the array to order from least to greatest
Swapping lowest value in the array to order from least to greatest

Time:09-30

I have this code

public static int[] swapLowest(int[] array)
{
    int smallestValue = 0;
    for(int i = 0; i < array.length; i  )
    {
        for(int k = i   1; k < array.length; k  )
        {
            if(array[i] > array[k])
            {
                smallestValue = array[i];
                array[i] = array[k];
                array[k] = smallestValue;
            }
        }
    }
}

and it works how I want it to: swapping values to make it from least to greatest (e.g [5, 1, 2, 1, 6] would turn to [1, 1, 2, 5, 6]).

However, the logic behind this is wrong. Apparently, I did not meet the criteria:

  • Find the smallest number in the array from the starting point
  • Swap the lowest value with the current value, if necessary
  • Move to the next index 
  • Repeat until it reaches the last index

I'm not too sure why. Did I understand it wrong? Regardless, I need to fix my code, and any pointers to help me understand/what to do instead will be greatly appreciated

CodePudding user response:

'I don't know if I got that correctly. Did you mean "How to get elements of an array in ascending order", perhaps. If that's what you meant, than this could be a way to do it. The output is {1, 2, 3, 4, 5, 6, 6, 7}.'

public class Test2 {
public static void main(String[] args) {
    int[] array = {1, 4, 3, 6, 2, 7, 6, 5};
    
    for(int i=0; i<array.length-1; i  ) {
        for(int j=i 1; j<array.length; j  ) {
            if(array[i]>array[j]) {
                swap(array, i, j);
            }
        }
    }
    
    printArray(array);
}

public static void swap(int[] array, int i, int j) {
    int temp;
    temp = array[i];
    array[i]=array[j];
    array[j]=temp;
}

// Prints the array
public static void printArray(int[] array) {
    System.out.print("{");
    for(int i=0; i<array.length; i  ) {
        System.out.print(array[i]);
        if(i<array.length-1) {
            System.out.print(", ");
        }
    }
    System.out.println("}");
}

}

CodePudding user response:

'If you don't want to create swap(), you can simply substitute like this'

public class Test2 {
public static void main(String[] args) {
    int[] array = {1, 4, 3, 6, 2, 7, 6, 5};
    
    for(int i=0; i<array.length-1; i  ) {
        for(int j=i 1; j<array.length; j  ) {
            if(array[i]>array[j]) {
                int temp;
                temp = array[i];
                array[i]=array[j];
                array[j]=temp;
            }
        }
    }
    
    printArray(array);
}

// Prints the array
public static void printArray(int[] array) {
    System.out.print("{");
    for(int i=0; i<array.length; i  ) {
        System.out.print(array[i]);
        if(i<array.length-1) {
            System.out.print(", ");
        }
    }
    System.out.println("}");
}

}

CodePudding user response:

Seems like you are trying to write a variation of selection sort (exchange sort) and there is nothing wring with your code. There are couple of ways you could implement selection sort.

  1. swap the elements outside the inner for loop
  2. swap the elements inside the inner for loop

you are following up the second method

inside your inner for loop you are looking for the smallest value compared to your current value and if there is a one you are swapping them.

All the steps mentioned in the question you have them done successfully

following article has complete guide on selection sort [https://www.javatpoint.com/selection-sort-in-java]

  • Related