// JAVA , A little fix on my code I need to changeMain.java to ask \"ENter the a
ID: 3847147 • Letter: #
Question
// JAVA , A little fix on my code I need to changeMain.java to ask "ENter the array size"and input all the element for it"
// help me change Main.java
thanks
public class Selection
{
static int min;
static int temp;
static int[] selectionSort(int[] a)
{
for(int i=0;i<a.length;i++)
{
min = i;
for(int j=i+1;j<a.length;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
return a;
}
}
Main.java
package somePackage;
public class Main {
public static void main(String[] args)
{
int[] a = {4,9,12,2,3,6,1,8};
Selection.selectionSort(a,0,7);
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+",");
}
}
}
Explanation / Answer
ChangeMain.java
import java.util.Scanner;
public class ChangeMain {
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the array size: ");
int n = scan.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++){
System.out.print("Enter the value "+(i+1)+": ");
a[i]=scan.nextInt();
}
Selection.selectionSort(a);
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+",");
}
}
}
Selection.java
public class Selection
{
static int min;
static int temp;
static int[] selectionSort(int[] a)
{
for(int i=0;i<a.length;i++)
{
min = i;
for(int j=i+1;j<a.length;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
return a;
}
}
Output:
Enter the array size:
5
Enter the value 1: 4
Enter the value 2: 5
Enter the value 3: 3
Enter the value 4: 1
Enter the value 5: 2
1,2,3,4,5,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.