Skip to main content

Java Source Code: Sort Numbers in Selection Sort

Get A Website Plus a Free Domain Name in Just 1 Hour!

Bring the new technology in your hands! Share your skills, improve and impress. Get Your Own Website and a Free Domain Name Here!

Random Tips for Readers

Make your internet connection faster and bypass all the restricted websites in your country using PD-Proxy VPN Service. If you feel that you are not getting the freedom you deserve while surfing online or you feel you do not have the absolute privacy that you deserve while you pay so high for your internet connection, here is your solution! This VPN service will help you not just to make your internet connection faster, but it will also protect your identity online. Through PD-Proxy VPN service, you will have the chance to:

  • Connect with varieties of international servers worldwide
  • Connect to fast online gaming servers
  • Connect to fast torrent download servers
  • View movies and video clips online with absolute speed
  • Chance to keep your identity hidden and protect it from identity thieves
  • Chance to avoid unauthorized access of your information without your permission
  • Chance to access restricted sites like facebook and the likes, if you are from a country with lots of restrictions

For more details and information, visit PD-Proxy VPN Service.

Below is the sample java source code on sorting numbers using selection sort. Selection Sort in java is much prepared by the programmers when it comes on sorting. Here, I will present to you on how the selection sort works.

How Selection Sort works in Java?

Assuming the user entered 6 numbers which are:

4 5 1 3 2 6

The program will sort it in an ascending order using selection sort. The program will simply compare the first number which is 4 to the second number, third number and so on, and then sort. See below on how it works.

From the entered numbers 4 5 1 3 2 6

Sorted To:

5 1 3 2 4 6

1 3 2 4 5 6

1 2 3 4 5 6 //sorting ends here and this will be output to the screen.

Sorting in selection sort is pretty fast than bubble sort. But If you are interested on comparing other methods of sorting like bubble sort , you can visit the link below.

Below is the java source code for Selection Sort.

Java Source Code: How to Sort Numbers using Selection Sort

//java class

public class SelectionSort

     public void SelectionSort(int[] arr){
     for(int i=0; i<arr.length; i++)
        for(int j=i+1; j<arr.length; j++)
            if(arr[i] > arr[j] )
                int temp = arr[j];
                arr[j] = arr[i];
                arr[i] = temp;

     for(int i=0; i<arr.length; i++)
         System.out.print(arr[i] + " ");

//main class

import java.util.Scanner;

public class Main

    public static void main(String[] args)

        Scanner input = new Scanner(;

        System.out.print("Enter the size of the array: ");
        int n = input.nextInt();
        int[] x = new int[n];

        System.out.print("Enter "+ n +" numbers: ");
        for(int i=0; i<n; i++)
            x[i] = input.nextInt();

        SelectionSort access = new  SelectionSort();
        System.out.print("The Sorted numbers: ");


Java Tutorials and Tips

Scroll to Continue

Sample Output:

Enter the size of the array: 10

Enter 10 numbers: 500 600 250 1000 35 50 10 15 20 1

The Sorted numbers: 1 10 15 20 35 50 250 500 600 1000

Other Java Source Code Examples


halfdeadguy on May 17, 2016:

xD thanks

raji on August 07, 2014:

do you have code for sorting names in descending

john on September 26, 2013:

do you have a codes for shell sort that have a ascending and descending order?

joey on July 05, 2013:

do you have a code for selection sort using descending order?

joey on July 05, 2013:

do you have a code for descending order using the selection sort?

Rasna Aisha (author) from Manila, Philippines on July 21, 2012:

Hi Alex...

This is a selection sort, you can see bubble sort example here:

Alex on April 12, 2012:

I'm sorry to say, but it looks more like a bubble sort to me.

Rasna Aisha (author) from Manila, Philippines on February 06, 2012:

Thank you for the comment web-tools...:)

Related Articles