I am using first array to store all database of numbers where some numbers are duplicates.
I have went through this array to see which items are duplicated and am adding index of duplicated items to second array.
Now, I must loop through first array and add all but duplicated values to third array (assuming that we know which fields are duplicated).
But how to do this correctly? I can't make it stop adding every item from first array to third array.
Assuming I can't use HashSet().
The purpose of this is to demonstrate how to move one array to other with removed duplicated in O(N) time complexity.
Input numbers: 00, 11, 11, 22, 33, 44, 55, 55, 66, 77, 88, 99
Output which index are duplicated: 1, 2, 6, 7
Output I get: 00, 11, 11, 22, 33, 44, 55, 55, 66, 77, 88, 99 (same as the input)
Code:
public void dups()
{
int[] b = new int[100];
int[] c = new int[100];
int k = 0;
int n = 0;
int p = 0;
for (int i = 0; i < nElems; i++)
for (int j = 0; j < nElems; j++)
if(a[j].equals(a[i]) && j != i)
b[k++] = i;
for (int l = 0; l < k; l++)
System.out.print(b[l] + " ");
for (int m = 0; m < nElems; m++)
if (m != b[p + 2])
c[m] = (Integer) a[n++];
System.out.print("\n");
for (int o = 0; o < nElems; o++)
System.out.print(c[o] + " ");
}
It can be done in a simpler way:
Set<Integer> uniqueSet = new HashSet<Integer>();
uniqueSet.addAll(list);
//not uniqueSet contains only unique elements from the list.
The reason it works is that a Set cannot contain duplicates. So while adding elements to a Set, it ignores those that are duplicates.
You can use a HashSet instead of Array to store all database of numbers.
After that use Collections.toArray() to get your desired Array.
I see that the question got edited and we don't want to use HashSet anymore.
Anyways, your problem is already answered here, Algorithm: efficient way to remove duplicate integers from an array
Instead of marking all duplicates you could mark all that have already been seen earlier.
Instead of:
for (int i = 0; i < nElems; i++)
for (int j = 0; j < nElems; j++)
if(a[j].equals(a[i]) && j != i)
b[k++] = i;
Use something like:
for (int i = 0; i < nElems; i++)
for (int j = i+1; j < nElems; j++)
if(a[j].equals(a[i]))
b[k++] = j;
You should then see:
Output which index are duplicated: 2, 7
Which should be much easier to work with.
Here's a working solution - although I wouldn't do it this way:
public class Test {
Integer[] a = {00, 11, 11, 22, 33, 44, 55, 55, 66, 77, 88, 99};
int nElems = a.length;
public void dups() {
int[] b = new int[100];
int[] c = new int[100];
int k = 0;
int n = 0;
int p = 0;
for (int i = 0; i < nElems; i++) {
for (int j = i + 1; j < nElems; j++) {
if (a[j].equals(a[i])) {
b[k++] = j;
}
}
}
for (int l = 0; l < k; l++) {
System.out.print(b[l] + " ");
}
for (int m = 0; m < nElems; m++) {
if (m != b[p]) {
c[n++] = a[m];
} else {
p += 1;
}
}
System.out.print("\n");
for (int o = 0; o < nElems - k; o++) {
System.out.print(c[o] + " ");
}
}
public static void main(String args[]) {
new Test().dups();
}
}
which prints:
2 7
0 11 22 33 44 55 66 77 88 99
I don't know if this is what you were looking for. But it removes duplicate. Give it a shot,
int[] b = { 00, 11, 11, 22, 33, 44, 55, 55, 66, 77, 88, 99 };
List<Integer> noDups = new ArrayList<Integer>();
Boolean dupliceExists;
for (int i = 0; i < b.length; i++) {
dupliceExists = Boolean.FALSE;
for (Integer integ : noDups) {
if (Integer.valueOf(b[i]).equals(integ)) {
dupliceExists = Boolean.TRUE;
//Get index if you need the index of duplicate from here
}
}
if (!dupliceExists) {
noDups.add(b[i]);
}
}
for (int i = 0; i < noDups.size(); i++) {
System.out.println(noDups.get(i));
}
I am going to coding for copying non-duplicate elements from one Array to another Array.
/*
* print number of occurance of a number in an array beside it
*
* */
class SpoorNumberOfOccuranceInArray{
public static void main(String[] args){
int[] arr={65,30,30,65,65,70,70,70,80};
int[] tempArr=new int[arr.length];//making size compatible to source Array.
int count=0;
for(int i=0;i<arr.length;i++){
int temp=arr[i];
for(int j=0;j<tempArr.length;j++){
if(temp==tempArr[j]){ //Comparing the Availability of duplicate elements in the second Array.---------
break;
}
else{ //Inserting if value is not in the second Array.---------------
if( tempArr[j]==0){
tempArr[j]=temp;
break;
}//end if
}//end else
}//end if
}//end for
for(int i=0;i<tempArr.length;i++){
for(int j=0;j<arr.length;j++){
if(tempArr[i]==arr[j])
count++;
}
System.out.println(tempArr[i]+" "+count);
count=0;
}
}
}
Related
I am pretty new to java and am just learning 2D arrays. I need to get the top 5 numbers and have tried everything I could think of. I was able to get the highest number using an If statement but am not able to get past that. I figured that I would try and get the second number and then move on to the rest. My friend said he got it done using for loops but I also could not get that to work. Any help would be appreciated. Thanks!
This is the code that I used:
package secondAssignment;
import java.util.Random;
public class BiggestNumbersRectangular {
public static void main(String[] args) {
Random rand = new Random();
int[][] arrSize = new int [4][5];
for (int i = 0; i < arrSize.length; i++) {
for (int j=0; j< arrSize.length; j++) {
arrSize[i][j] = rand.nextInt(89) + 10;
System.out.print(arrSize[i][j] + " ");
}
System.out.println();
}
int max = arrSize [0][0];
int largeNumTwo = arrSize [0][0];
for (int i = 0; i < arrSize.length; i++) {
for (int j = 0; j < arrSize.length; j++) {
if (max < arrSize[i][j]) {
max = arrSize [i][j];
if (largeNumTwo < max) {
arrSize [i][j] = largeNumTwo;
}
}
}
}
System.out.println("Highest Number: " + max);
System.out.println("Second Highest Number:" + largeNumTwo);
}
}
The output that I get is this:
45 10 44 70
36 87 35 38
68 14 30 79
34 69 50 92
Highest Number: 92
Second Highest Number:45
The code that I used for the second number is outputting only the first randomly generated number.
I am not sure how to fix this.
If you're allowed to use a List, then I'd do it this way.
There's nothing terribly fancy in here:
import java.util.*;
class Main {
public static void main(String[] args) {
Random rand = new Random();
int[][] arrSize = new int [4][5];
for (int row = 0; row < arrSize.length; row++) {
for (int col=0; col < arrSize[row].length; col++) {
arrSize[row][col] = rand.nextInt(89) + 10;
}
}
displayArray(arrSize);
List<Integer> top5 = new ArrayList();
for (int row = 0; row < arrSize.length; row++) {
for (int col=0; col < arrSize[row].length; col++) {
int current = arrSize[row][col];
// see if current is larger than anything in top5
boolean added = false;
for(int i=0; i<top5.size() && !added; i++) {
if (current >= top5.get(i)) {
// insert new top 5 number in correct spot
top5.add(i, current);
added = true;
}
}
if (!added && top5.size() < 5) {
top5.add(current); // add it to the end
}
else if (added && top5.size() > 5) {
top5.remove(top5.size() - 1); // remove 6th largest
}
}
}
// display the top 5 numbers from the 2D array
for(int i=0; i<top5.size(); i++) {
System.out.println("#" + (i+1) + ": " + top5.get(i));
}
}
public static void displayArray(int[][] numbers) {
for(int[] row : numbers) {
System.out.println(Arrays.toString(row));
}
}
}
Sample run:
[38, 63, 19, 17, 11]
[49, 42, 98, 71, 32]
[54, 74, 89, 44, 56]
[56, 91, 52, 72, 49]
#1: 98
#2: 91
#3: 89
#4: 74
#5: 72
This is basically the same approach that Javohir Xoldorov took, but the code is a little easier to understand because we can easily add, insert, and delete elements from a List without having to manually shift everything like you would in an Array.
Maybe you can see this solution.
Approach:
you can declare five variable but then You will use most if so we can:
Declare static array size = 5
Fill array which possible min val INT_MIN
We get one element given matrix and compare five element If which element < firstArray[i] then it changed and before matrix element one step move right side
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random rand = new Random();
int[][] arr = new int[4][5];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
arr[i][j] = rand.nextInt(89) + 10;
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
int[] max_element = new int[5];
for (int i = 0; i < max_element.length; i++) {
max_element[i] = Integer.MIN_VALUE;
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
for (int k = 0; k < max_element.length; k++) {
if (max_element[k] < arr[i][j]) {
int before = max_element[k], current;
for (int z = k + 1; z < max_element.length; z++) {
current = max_element[z];
max_element[z] = before;
before = current;
}
max_element[k] = arr[i][j];
break;
}
}
}
}
for (int i = 0; i < max_element.length; i++) {
System.out.println((i + 1) + " Highest Number: " + max_element[i]);
}
}
}
UPDATE 2: After testing, I changed my algorithm now finally to work really correctly if the highest value of all is contained within the first five entries. And I produced code:
package examples.stackoverflow.q75461466;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
public class Q75461466 {
public static void main(String[] args) {
int[][] myArray = initRandomArray(4, 5, 79, 10);
// for testing it's better to work with a fixed dataset
// int[][] myArray = {{94, 61, 17, 27, 67}, {68, 74, 46, 61, 98}, {42, 79, 95, 77, 39}, {41, 42, 97, 50, 76}};
System.out.print("Original data: ");
System.out.println(Arrays.deepToString(myArray));
List<Integer> topFive = new ArrayList<>();
Integer min = Integer.MIN_VALUE;
for (int[] row : myArray) {
for (int value : row) {
if ((value > min) || topFive.size() < 5) {
topFive.add(value);
if (topFive.size() > 5) {
topFive.remove(min);
}
min = topFive.stream().min(Integer::compareTo).orElse(Integer.MIN_VALUE);
}
}
}
System.out.print("The 5 highest numbers are: ");
System.out.println(topFive);
}
private static int[][] initRandomArray(int height, int width, int upper, int lower) {
int[][] randomArray = new int[height][width];
Random rand = new Random();
for (int row = 0; row < randomArray.length; row++) {
for (int col = 0; col < randomArray[row].length; col++) {
randomArray[row][col] = rand.nextInt(upper + lower) + lower;
}
}
return randomArray;
}
}
First the array is initialized and filled with data in the method initRandomArray. Please note that for testing it is better if you work with a fixed array, it will get you reproducible results where you also can simulate "problematic" cases.
Then the list topFive is used - a name a bit misleading, because it does not contain five elements all the time.
I loop over all the entries in the arrays with two nested loops (the code is written to be independent of the actual array dimensions, otherwise further optimizations could have been done).
Array indexes are not necessary, the for-each variant is sufficient, as I only need each value once.
I make sure that the first five entries are always put into the topFive list, and I always track the minimum value inside that list (in min). For this I use a bit "stream magic", the orElse(Integer.MIN_VALUE) in the end is needed since the min method returns an Optional in the case the list is empty. Should never happen in this algorithm, but the Optional has to be handled properly.
After five values are in the list, it is relevant if the next input value is bigger than the smallest value already in the list - if so, then the input value is added to the list, and the current minimum value is removed.
The new minimum value has to be calculated, as the value just inserted is not necessarily the new minimum.
After all values of the arrays have been processed, the list contains five values at maximum, so it can be directly output as the result.
The resulting list will not be sorted, but will contain the values in the order of their first occurrence.
In the case that the first five input values contain a value more than once, it is possible that the output list contains the same value also more than once.
I'd suggest doing this using Java's PriorityQueue class. While it might be overkill for 5 items, it's quite straightforward to use and will perform extremely well for larger target sets.
The default constructor gives a min priority queue, which is exactly what we want. Add the first desiredQty items to the priority queue. The minimum of those will be at the front of the queue, and can be inspected using peek(). For all subsequent values in the array, compare them to the current minimum value. If the new value is larger than the current minimum, toss the min and add the current value. When you get to the end of the array the priority queue will contain the desiredQty largest values, so you can just iterate through and print them (or do whatever else suits your fancy).
import java.util.Arrays;
import java.util.Random;
import java.util.PriorityQueue;
class Top5 {
public static void main(String[] args) {
Random rand = new Random();
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
int[][] arr = new int [4][5];
int desiredQty = 5;
for (int row = 0; row < arr.length; row++) {
for (int col=0; col < arr[row].length; col++) {
arr[row][col] = rand.nextInt(89) + 10;
if (pq.size() < desiredQty) {
// If we don't yet have desiredQty items, just add the latest one to pq.
pq.add(arr[row][col]);
} else {
// If pq has the desiredQty and the latest value is bigger than
// the current min, remove the min and add the latest value.
if (arr[row][col] > pq.peek()) {
pq.poll();
pq.add(arr[row][col]);
}
}
}
System.out.println(Arrays.toString(arr[row]));
}
System.out.println("\n" + desiredQty + " largest values are:");
System.out.println(Arrays.toString(pq.toArray()));
}
}
Sample output:
[55, 82, 92, 93, 79]
[84, 26, 78, 87, 23]
[49, 67, 76, 37, 18]
[12, 39, 54, 38, 68]
5 largest values are:
[82, 84, 92, 93, 87]
I need help, please! I am building up the following code to extract the minimum value of each column of the distance
I have tried to compute the code but to no avail
public static void main(String[] args) {
int data[] = {2, 4, -10, 12, 3, 20, 30, 11};//,25,17,23}; // initial data
int noofclusters = 3;
int centroid[][] = new int[][]{
{0, 0, 0},
{2, 4, 30}
};
getCentroid(data, noofclusters, centroid);
}
public static int[][] getCentroid(int[] data, int noofclusters, int[][] centroid) {
int distance[][] = new int[noofclusters][data.length];
int cluster[] = new int[data.length];
int clusternodecount[] = new int[noofclusters];
centroid[0] = centroid[1];
centroid[1] = new int[]{0, 0, 0};
System.out.println("========== Starting to get new centroid =========");
for (int i = 0; i < noofclusters; i++) {
for (int j = 0; j < data.length; j++) {
//System.out.println(distance[i][j]+"("+i+","+j+")="+data[j]+"("+j+")-"+centroid[0][i]+"="+(data[j]-centroid[0][i]));
distance[i][j] = Math.abs(data[j] - centroid[0][i]);
System.out.print(distance[i][j] + " ,");
}
System.out.println();
}
int[] result = new int[distance.length];
for (int i = 0; i < distance.length; i++) {
//int min = distance;
int min = distance[i][0];
for (int j = 0; j < distance[0].length; j++) {
if (distance[i][j] < min) {
min = distance[i][j];
}
result[j] = min;
System.out.println(result[j] + ", ");
}
}
return result;
}
}
The result of the computation for distance gives
row 1: 0 ,2 ,12 ,10, 1 ,18 ,28 ,9
row 2: 2 ,0 ,14 ,8 , 1 ,16 ,26 ,7
row 3: 28,26,40 ,18, 27 ,10 ,0 ,19
I want to go through each column to get the minimum value
0 0 12 8 1 10 0 7
Thanks for your help in advance
To get the minimum value in each column, first, you need to iterate the column in the outer loop. By doing so, we can access the matrix colunm-wise.
Assign the first value of each column to a variable. Then iterate through the rows in the inner loop.
Check if the current value is less than the minimum value. If so, assign the smallest value to minimum.
When we use an array, we get an array of minimum values of the column.
To obtain the minimun value in each row, swap the loops and use min[i].
Below is an example code:
int []min = new int[column_lenght];
for(int j = 0; j < column_length; j++) {
min[j] = array[i][j];
for(int i = 0; i < row_length; i++) {
if(array[i][j] < min[j]) {
min[j] = array[i][j];
}
}
}
The min[] will contain the minimum value of each column.
here is the integer array given to me 111,77, 88, 44, 32, 11, 13, 25, 44 I need to sort & display only the odd elements of the array .
I had tried solving it using loops and if condition
i had expected the output as 11 13 25 77 111
import java.lang.reflect.Array;
public class oddsortSolution {
public static void main(String args[]) {
int n[] = { 111, 77, 88, 44, 32, 11, 13, 25, 44 };
int i = 0;
int temp = 0;
while (i < n.length) {
if (n[i] % 2 != 0) {
for (int j = i + 1; j < n.length; j++) {
if (n[j] > n[i]) {
n[j] = temp;
n[j] = n[i];
n[i] = temp;
}
}
}
}
System.out.println(n[1]);
}
}
You basically need to keep track of your odd array size, increment it every time you find an odd value, determine if the value should be swapped somewhere in the existing array (ranging from 0 to oddArraySize) and insert it in the correct position. Try the following code,
public class oddsortSolution {
public static void main(String args[]) {
int n[] = { 111, 77, 88, 44, 32, 11, 13, 25, 44 };
int oddArraySize = 0;
for (int i = 0;i < n.length; i++) {
if (n[i] % 2 != 0) {
oddArraySize++;
for (int j = 0; j < oddArraySize; j++) {
if (j == oddArraySize - 1) {
n[j] = n[i];
} else if (n[j] > n[i]) {
int temp = n[j];
n[j] = n[i];
n[i] = temp;
}
}
}
}
int[] oddArray = Arrays.copyOfRange(n, 0, oddArraySize);
System.out.println( Arrays.toString( oddArray ));
}
}
why isn't this code working?
I'm supposed to write a function which removes the odd numbers from an array. here is my code but I don't know where it went wrong. It's giving me an error.
public class Test{
public static int [] removeOdd(int[] input){
int c = 0;
for(int i=0; i<input.length; i++){
if(input[i]%2==0){
c++;
}
}
int [] a = new int[c];
for(int i=0; i<input.length; i++){
if(input[i]%2==0){
a[i] = input[i];
}
}
return a;
}
public static void main(String [] args){
int [] mixedArray = {21, 33, 44, 66, 11, 1, 88, 45, 10, 9};
for (int i = 0; i < mixedArray.length; i++) {
System.out.print(mixedArray[i] + " ");
}
System.out.println();
int [] noOdd = Test.removeOdd(mixedArray);
for (int i = 0; i < noOdd.length; i++) {
System.out.print(noOdd[i] + " ");
}
}
Thanks in advance :)
You need another index variable to access the items of a and not use i:
public static int [] removeOdd(int[] input){
int c = 0;
for(int i=0; i<input.length; i++){
if(input[i]%2==0){
c++;
}
}
int [] a = new int[c];
int k = 0;
for(int i=0; i<input.length; i++){
if(input[i]%2==0){
a[k] = input[i];
k++;
}
}
return a;
}
The index variable i iterates through input and its values do not match and will exceed the permitted values of the indexes of a so I have used k.
The problem is here:
int [] a = new int[c];
for(int i=0; i<input.length; i++){
if(input[i]%2==0){
a[i] = input[i];
}
}
This loop will iterate through the whole input array, and try to insert just the even ones into a, but a is smaller than input, because you allocated space equal to the amount of just the even numbers in input.
In your test case, your a will have size 4, but will try to access the position 6, giving you an out of bound exception.
The problem is that you are trying to insert at a[i] which you could visualise like this:
index: 0 1 2 3 4
input: [1 2 3 4 5]
a: [ 2 4 ]
However, your a array is smaller than your input array, because you have reduced the size to account for the lack of odd numbers. You actually want to do this:
index: 0 1 2 3 4
input [1 2 3 4 5]
a [2 4]
Notice how the index of both even numbers has changed. You were trying to keep it the same.
One way to solve this would be to keep a separate counter variable which tracks where you're up when inserting into a.
int[] a = new int[c];
int sizeOfA = 0;
for (int i = 0; i < input.length; i++)
{
if (input[i] % 2 == 0)
{
a[sizeOfA] = input[i];
sizeOfA++;
}
}
You can simplify the method removeOdd using Streams:
import java.util.Arrays;
public class Test {
public static int[] removeOdd(int[] input) {
return Arrays.stream(input).filter(i -> i % 2 == 0).toArray();
}
public static void main(String[] args) {
int[] mixedArray = { 21, 33, 44, 66, 11, 1, 88, 45, 10, 9 };
for (int i = 0; i < mixedArray.length; i++) {
System.out.print(mixedArray[i] + " ");
}
System.out.println();
int[] noOdd = Test.removeOdd(mixedArray);
for (int i = 0; i < noOdd.length; i++) {
System.out.print(noOdd[i] + " ");
}
}
}
I am supposed to create an algorithm that sorts according to these steps:
Method 1
Select the lowest number in the list and swap with the first number.
Select the lowest number in the list and swap with the second number.
Start checking from the second number.
Select the lowest number in the list and swap with the third number.
Start checking from the third number.
Select the lowest number in the list and swap with the fourth number.
Start checking from the fourth number.
Repeat…until you reach the last number.
Currently, this is the code I have come up with:
public static void method1() {
int low = 999;
int index = 0;
int safe;
int[] num = new int[] { 33, 22, 8, 59, 14, 47, 60, 27 };
for(int i = 0; i < num.length; i++) {
if(low > num[i]) {
low = num[i];
index = i;
}
}
for (int i = 0; i < num.length; i++) {
safe = num[i];
num[i] = num[index];
low = 999;
for(int j = (i+1); j < num.length; j++) {
if(low > num[j]) {
low = num[j];
}
}
}
for (int i = 0; i < num.length; i++) {
System.out.print(num[i] +", ");
}
}
The output looks like this:
run:
8, 8, 8, 8, 8, 8, 8, 8,
BUILD SUCCESSFUL (total time: 0 seconds)
Why am I only getting values of 8 in the output? As this is homework, please don't tell me the answer. I would only like guidance, thanks!
EDIT:
Code now looks like this:
int low = 999;
int index = 0;
int safe;
int[] num = new int[] { 33, 22, 8, 59, 14, 47, 60, 27 };
for(int i = 0; i < num.length; i++) {
if(low > num[i]){
low = num[i];
index = i;
}
}
for (int i = 0; i < num.length; i++) {
safe = num[i];
num[i] = num[index];
low = 999;
for(int j = (i+1); j < num.length; j++) {
if(low > num[j]){
low = num[j];
index = j;
}
}
}
for (int i = 0; i < num.length; i++) {
System.out.print(num[i] +", ");
}
System.out.println("");
Which gives me an output of:
run:
8, 8, 8, 14, 14, 27, 27, 27,
BUILD SUCCESSFUL (total time: 0 seconds)
The date for this homework has been and gone, but thought I would add some step-by-step methodology.
The way I would approach this is to break it down into small steps. Each step should be a method or function.
1. The first step is to find the smallest number in the array, starting from N.
So the method for this would be:
private int findLowestStartingAtNth( int n ) {
int lowest = Integer.MAX_VALUE;
for( int i = n ; i < numbers.length ; i++ ) {
if( numbers[i] < lowest ) {
lowest = numbers[i];
}
}
return lowest;
}
2. Then we need to swap two arbitrary numbers in an array.
This is quite simple:
private void swapNumbers( int i, int j ) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
3. But if we want the output of findLowestStartingAtNth() to feed into the input of swapNumbers(), then we need to return the index not the number itself.
So the method from step 1. is altered to be:
private int findLowestStartingAtNth( int n ) {
int lowest = Integer.MAX_VALUE;
int index = n;
for( int i = n ; i < numbers.length ; i++ ) {
if( numbers[i] < lowest ) {
lowest = numbers[i];
index = i;
}
}
return index;
}
4. Let's use what we have to achieve step one
Select the lowest number in the list and swap with the first number.
The first number is the zero-th in the array.
int numbers = new int[] {33, 22, 8, 59, 14, 47, 60, 27};
int found = findLowestStartingAtNth( 0 );
swapNumbers(0, found);
5. We have a pattern. Start checking from 1st, swap with 1st. Start checking from 2nd, swap with 2nd. Start checking with X, swap with X.
So let's wrap this pattern in a further method:
private int[] numbers = new int[] {33, 22, 8, 59, 14, 47, 60, 27};
private void sort() {
for( int i = 0 ; i < numbers.length ; i++ ) {
int j = findLowestStartingAtNth( i );
swapNumbers(i, j);
}
}
6. Finally, wrap it in a class, and trigger it from main() method. See how clear the code is because it is broken into small steps.
The entire resulting class would be:
public class Method1 {
public static void main(String[] args) {
Method1 m = new Method1();
m.numbers = new int[] {33, 22, 8, 59, 14, 47, 60, 27};
m.sort();
System.out.println(Arrays.toString(m.numbers));
}
private int[] numbers;
private int findLowestStartingAtNth( int n ) {
int lowest = Integer.MAX_VALUE;
int index = n;
for( int i = n ; i < numbers.length ; i++ ) {
if( numbers[i] < lowest ) {
lowest = numbers[i];
index = i;
}
}
return index;
}
private void swapNumbers( int i, int j ) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
private void sort() {
for( int i = 0 ; i < numbers.length ; i++ ) {
int j = findLowestStartingAtNth( i );
swapNumbers(i, j);
}
}
}