Write a program to enter weight of ten people and sort them using selection sort technique using Scanner class
Answers
Rajdeep here...
Here's your answer:
import java.util.*;
class SSort
{
public static void main (String args[])
{
Scanner sc = new Scanner (System.in);
int wt[] = new int[10];
int minpos, i, j, temp;
System.out.println("Enter the weight of 10 people: ");
for (i = 0; i < 10; i++)
{
System.out.print("Number " + (i+1) + ": ");
wt[i] = sc.nextInt();
}
for (i = 0; i < 9; i++)
{
minpos = i;
for (j = (i+1); j < 10; j++)
{
if (wt[j] < wt[minpos])
minpos = j;
}
if (i != minpos)
{
temp = wt[i];
wt[i] = wt[minpos];
wt[minpos] = temp;
}
}
System.out.println("The new sorted weights is: ");
for (i = 0; i < 10; i++)
{
System.out.print(wt[i] + " ");
}
}
}
Thanks!!
import java.util.*;
class selection_sort
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
System.out.println("Enter the weights of 10 people);
for(int i=0;i<10;i++)
{
a[i]=sc.nextInt();
}
int min=0;
int temp=0;
for(int i=0;i<9;i++)
{
min=i;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
System.out.println("Sorted weights are ");
for(int i=0;i<9;i++)
{
System.out.println(a[i]);
}
}
}