Computer Science, asked by tanmaypandey030, 7 months ago

1. WRITE A PROGRAM TO SORT A LIST OF INTEGERS USING SELECTION SORT METHOD.​

Answers

Answered by anindyaadhikari13
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