Write a java program to sort an array elements using selection sorting technique.

Program to sort an array elements using selection sorting technique.


program:

import java.util.*;
public class Array_Sort
{
public static void main(String args[])
{
int n,i,j;
Scanner sc = new Scanner(System.in);
System.out.println("Enter how many elements u want :");
n = sc.nextInt();

int a[] = new int[n];
System.out.print("Enter elements of an Array:" );
for( i=0;i<n;i++)
{
a[i] = sc.nextInt();
}

// selection sort
for( i=0;i<n-1;i++)
{
for( j=i+1;j<n;j++)
{
if(a[i] > a[j])
{
int t=a[i];
a[i] = a[j];
a[j] = t;
}
}
}
System.out.println("sorted elements of an Array:" );
for( i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}


Output :
Enter how many elements u want :
4
Enter elements of an Array:
3
6
4
2
sorted elements of an Array:
2
3
4
6

Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.

Java Database Connectivity (JDBC) ,Steps to connect to the database in Java