Java Selection Sorting Arrays in Java

AI Thread Summary
The discussion centers on implementing a selection sort algorithm within a Java program that creates an array of 20 integers, prompts the user to fill it, and then sorts the values from smallest to largest. The original code initializes the array and collects user input, but the user is unsure how to incorporate the provided selection sort methods. The solution involves either copying the sorting methods into the existing class, allowing direct calls within the main method, or placing them in a separate class (e.g., SelectionSort) and calling the sorting method using the class name. The correct syntax for invoking the sorting method is clarified, emphasizing the simplicity of the implementation once the methods are correctly positioned.
blue_soda025
Messages
26
Reaction score
0
So, I have to create a program that makes a new array with 20 slots, then it asks you to enter a value into each index, which I have here:

Code:
import java.io.*;

class ArrayLoop
{
	public static void main (String[] args) throws IOException
	{
		int[] array = new int[20];
		
		for (int i = 0; i <= 19; i++)
		{
			System.out.println("Assign a value to slot " + i + ".");
			array[i] = KeyIn.anInt();
		}
		
		System.out.println();
		System.out.println("The values are: ");
		for (int j = 0; j <= 19; j++)
		{
			System.out.print(array[j] + " ");
		}
	}
}

Except I have to make it so that it sorts each value from smallest to largest and print that out in the last line. I'm supposed to use selection sort for this and I'm given the code:

Code:
static void selection_sort(int a[])
{
	int i, min;
	for (i = 0; i < a.length; i++)
	{
		min = find_minimum(a, i);
		swap(a, i, min);
	}
}

static int find_minimum (int a[], int i)
{
	int j; min = i;
	for (j = i + 1; j < a.length; j++)
	if (a[j] < a[min])
	min = j;
	return min;
}

static void swap(int a[], int i, int j)
{
	int hold = a[i];
	a[i] = a[j];
	a[j] = hold;
}

The problem is, I have no idea how to implement this code to my original program :/. So the question is, how do I use selection sort in that program? Any help would be appreciated.
 
Technology news on Phys.org
Copy and paste them into your class ArrayLoop and then because the methods are static you can just use the method selection_sort on your array.
 
What would be the correct syntax for using the method on the array? I can't seem to get this to work.
 
blue_soda025 said:
What would be the correct syntax for using the method on the array? I can't seem to get this to work.

It depends where you put methods selection_sort(), find_minimum(), and swap().
One way (that I wouldn't suggest) is to put those methods in your class ArrayLoop (copy and paste), parallel with the main() method. Then you could just write inside main:

Code:
selection_sort( array );

The other way is to put those methods in another class (say, SelectionSort). Then inside main you would write:

Code:
SelectionSort.selection_sort( array );

It's that simple!
 
Oh, I see now. Thanks a lot.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top