ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Java Source Code: Sort Numbers in Selection Sort

Updated on October 31, 2013

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: ");


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


    0 of 8192 characters used
    Post Comment
    • profile image


      4 years ago

      xD thanks

    • profile image


      6 years ago

      do you have code for sorting names in descending

    • profile image


      6 years ago

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

    • profile image


      7 years ago

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

    • profile image


      7 years ago

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

    • aisha91 profile imageAUTHOR

      Rasna Aisha 

      8 years ago from Manila, Philippines

      Hi Alex...

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

    • profile image


      8 years ago

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

    • aisha91 profile imageAUTHOR

      Rasna Aisha 

      8 years ago from Manila, Philippines

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


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at:

    Show Details
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the or domains, for performance and efficiency reasons. (Privacy Policy)
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
    ClickscoThis is a data management platform studying reader behavior (Privacy Policy)