1. WRITE A PROGRAM TO SORT A LIST OF INTEGERS USING SELECTION SORT METHOD.
Answers
Answered by
1
Question:-
Write a program to sort a list of integers using selection sort.
Program:-
import java.util.*;
class Sort
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of elements for the array: ");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter the numbers...");
for(int i=0;i<n;i++)
{
System.out.prinr(">> ");
a[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
int x=a[i];
a[i]=a[j];
a[j]=x;
}
}
}
System.out.println("Array after sorting... ");
for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
}
}
Similar questions
Math,
3 months ago
Math,
3 months ago
Science,
3 months ago
Social Sciences,
7 months ago
Social Sciences,
1 year ago
Chemistry,
1 year ago
Math,
1 year ago