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);
    }
}

}

No.1 Phan
  • 5
  • 7
  • 2
    You should also remember the index of the smallest element so you can put the first element there, `swap[smallestIndex] = temp;` – Joakim Danielson Apr 17 '20 at 09: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; – Bharat Apr 17 '20 at 10:00
  • 1
    Does this answer your question? [Effective swapping of elements of an array in Java](https://stackoverflow.com/questions/13766209/effective-swapping-of-elements-of-an-array-in-java) – Mindaugas Apr 17 '20 at 10:06
  • Thanks, never knew you could do that. :) – No.1 Phan Apr 17 '20 at 10:08
  • @Mindaugas Thats the post that I used to swap. – No.1 Phan Apr 17 '20 at 10:10

1 Answers1

1

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);
    }
  }
}


Vincenzo Manto
  • 886
  • 1
  • 11
  • 30