Selection Sorting Arrays in Java

  • Context: Java 
  • Thread starter Thread starter blue_soda025
  • Start date Start date
  • Tags Tags
    Arrays Java Sorting
Click For Summary

Discussion Overview

The discussion revolves around implementing a selection sort algorithm in a Java program that collects user input for an array of integers. Participants are exploring how to integrate sorting functionality into an existing code structure.

Discussion Character

  • Homework-related
  • Technical explanation

Main Points Raised

  • One participant shares a Java program that initializes an array and collects user input for its values.
  • Another participant suggests copying the sorting methods into the existing class to use them directly.
  • A follow-up question asks for clarification on the correct syntax for invoking the sorting method on the array.
  • A later reply indicates that the placement of the sorting methods affects how they can be called, offering two approaches: placing them in the same class or in a separate class.
  • One participant expresses gratitude for the clarification received.

Areas of Agreement / Disagreement

Participants generally agree on the methods of integrating the selection sort into the existing program, but there are differing opinions on the best practice for organizing the code.

Contextual Notes

There are unresolved questions regarding the specific syntax for method calls and the implications of method placement within the class structure.

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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K