0

What I am trying to do is create a program which creates and fills the array with user input, which then the smallest number is found and swapped with the first cell of the array.

The most trouble I'm having is how to find the smallest integer but be able to swap it because when I do this:

        for(int i = 0; i < swap.length; i++){
        if(smallest > swap[i]){
            smallest = swap[i];

it makes it into an integer and when I try to swap it.

        int temp = swap[0];
        swap[0] = smallest;
        smallest = temp;

It doesn't give me the output I want and am wondering how I can find the smallest number and keep the array cell number so I can use it to swap.

This is the full current code:

import java.util.Scanner;
import java.util.Arrays;

public class SmallestSwap {
  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Size of array?");
    int n = sc.nextInt();

    int [] swap = new int[n];
    int smallest = Integer.MAX_VALUE;
    for(int i = 0; i <swap.length; i++){
        System.out.println("Please enter a number: ");
        swap[i] = sc.nextInt();

    }
    for(int i = 0; i < swap.length; i++){
        if(smallest > swap[i]){
            smallest = swap[i];


        }
    }
    int temp = swap[0];
    swap[0] = smallest;
    smallest = temp;

    System.out.println("\n");
    for(int element : swap){

        System.out.println(element);
    }
}

}

5
  • 2
    You should also remember the index of the smallest element so you can put the first element there, swap[smallestIndex] = temp; Commented Apr 17, 2020 at 9:55
  • You should maintain the index of element as well , like int smallestIndex=0; for(int i = 0; i < swap.length; i++){ if(smallest > swap[i]){ smallest = swap[i]; smallestIndex=i; } } int temp = swap[0]; swap[0] = smallest; smallest = temp; swap[smallestIndex]=smallest; Commented Apr 17, 2020 at 10:00
  • 1
    Does this answer your question? Effective swapping of elements of an array in Java Commented Apr 17, 2020 at 10:06
  • Thanks, never knew you could do that. :) Commented Apr 17, 2020 at 10:08
  • @Mindaugas Thats the post that I used to swap. Commented Apr 17, 2020 at 10:10

1 Answer 1

0

It's a problem of pointer and values.

smallest = swap[i];

doesn't save the reference to the real item in the array swap[i]. In smallest you will find only the smallest value. So, if you what to swap values, you have to save the index. Here is the code

 import java.util.Scanner;
import java.util.Arrays;

public class swap {
  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Size of array?");
    int n = sc.nextInt();

    int [] swap = new int[n];
    int index_smallest = 0;
    int smallest = Integer.MAX_VALUE;
    for(int i = 0; i <swap.length; i++){
        System.out.println("Please enter a number: ");
        swap[i] = sc.nextInt();

    }
    for(int i = 0; i < swap.length; i++){
        if(smallest > swap[i]){
            smallest = swap[i];
            index_smallest = i;

        }
    }
    int temp = swap[0];
    swap[0] = swap[index_smallest];
    swap[index_smallest] = temp;

    System.out.println("\n");
    for(int element : swap){

        System.out.println(element);
    }
  }
}


Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.