Written by Sai Sameera Thota on February 21, 2013 in Java > Programming

Java Program to implement Selection Sort

GET ALERTS:

Get our Latest updates delivered to your mailbox!

We have already written a C Program to Implement Selection sort. The concept of sorting is the same but we have written this in Java. For explanation, refer the C Program.

import java.io.*;
class Selection_sort 
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.print("\nEnter the size of array : ");
		int n = Integer.parseInt(br.readLine());
		int[] arr = new int[n];
		System.out.println("\nEnter the elemnts in the array : ");
		for(int i=0;i<n;i++)
		{
			arr[i] = Integer.parseInt(br.readLine());
		}
		for(int i=0;i<n;i++)
		{
			for(int j=i+1;j<n;j++)
			{
				if(arr[i] > arr[j])
				{
					int temp = arr[i];
					arr[i] = arr[j];
					arr[j] = temp;
				}
			}
		}
		System.out.println("\nSorted List : ");
		for(int i=0;i<n;i++)
		{
			System.out.println(arr[i]);
		}
	}
}

Output:
Selection_sort

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: