Selection Sorting Arrays in Java

In summary, the conversation discusses the task of creating a program that creates a new array with 20 slots and prompts the user to enter a value for each index. The program then needs to sort the values from smallest to largest using selection sort. The conversation also provides the necessary code for the selection sort method and discusses how to implement it into the original program.
  • #1
blue_soda025
26
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
  • #2
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.
 
  • #3
What would be the correct syntax for using the method on the array? I can't seem to get this to work.
 
  • #4
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!
 
  • #5
Oh, I see now. Thanks a lot.
 

What is selection sorting in Java?

Selection sorting is a sorting algorithm used to arrange elements in an array in a specific order. In this method, the smallest element is selected and placed at the beginning of the array, and the process is repeated until the entire array is sorted.

How does selection sorting work in Java?

To perform selection sorting in Java, the algorithm iterates through the array and compares each element with the rest of the elements to find the smallest one. The smallest element is then swapped with the current element in the iteration, and the process continues until the array is fully sorted.

Is selection sorting efficient in Java?

Selection sorting has a time complexity of O(n^2), which means it is not the most efficient sorting algorithm. However, it can be efficient for small arrays or in situations where the array is partially sorted.

What is the best case scenario for selection sorting in Java?

The best case scenario for selection sorting in Java is when the array is already sorted. In this case, the algorithm will only perform n-1 comparisons and no swaps, making it more efficient than the average or worst case scenarios.

Can selection sorting be used for sorting different data types in Java?

Yes, selection sorting can be used to sort different data types in Java as long as the elements in the array are comparable. For example, numbers and strings can be sorted using selection sorting in Java.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
25
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
13
Views
3K
  • Programming and Computer Science
Replies
3
Views
732
  • Programming and Computer Science
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
2
Views
493
Back
Top