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