Tuesday, May 22, 2012

Program to Implement Selection Sort in Java+Explanation




Selection sort is said to be an efficient algorithm when considering small computations involving limited memory/resource but it is quite inefficient when compared to other techniques when huge computations are required.Selection sort is said to be efficient for small list of items because of its simplicity.

The following steps Explains the working of the Selection sort Algorithm


  1. Obtain the Item with the Minimal value from a List/Collection of items
  2. Swap the item with the Item in the first position of the Collection/List
  3. Repeat the Process for the rest of the Items in the Collection


The Following is the Complete Program Source Code in Java for Implementing Selection Sort Algorithm
 import java.io.*;
import java.lang.*;
class array
{
 DataInputStream get;
 int a[];
 int i,j,n;
 void getdata()
 {
 try
  {
   get=new DataInputStream(System.in);
   System.out.println("Enter the limit");
   n=Integer.parseInt(get.readLine());
   a=new int[n];
   System.out.println("Enter the elements in the array");
   for(i=0;ia[j])
   {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
   }
  }
 }
 System.out.println("Elements in ascending order is:");
 for(i=0;i=0;i--)
 System.out.print(" "+a[i]);
 }
}
class selectionsort
{
 public static void main(String arg[])
 {
  array obj=new array();
  obj.getdata();
  obj.sorting();
 }
}

9 comments:

  • Wouter Oet says:
    May 24, 2012 at 10:37 AM

    Don't know where to start:

    Class-names should be capitalized.
    Fields should be encapsulated.
    Some fields could be local.
    Field names are not informative.
    Fields j and key are never used.
    Using deprecated methods.
    High coupling with System.in and System.out
    Low cohesion
    Exception handling which loses the stacktrace
    "catch-all" exception handling.
    The lines in the main method don't even compile.
    Selection sort has a time complexity of O(n2) which is
    very inefficient for large lists.
    And you're basically reinventing the wheel because good
    and well tested sorting functionality is available: Arrays.sort();

  • Techman says:
    May 25, 2012 at 2:29 AM

    Hi Wouter,

    It is good to know that people with good Technical Knowledge like you are Visiting my Blog.
    Thanks for Reporting the Performance Limitations.
    May i bring to your notice that this article is intended for People with Basic Knowledge in Sorting Techniques,It does not take into consideration the Performance requirements or Exception Handling as this code is not for Deployment into real world scenarios.This code particularly is provided for students who need to know the Basics of Java.
    Again,Thanks for the Comment.Please check out other articles that are meant for Advanced Programmers like you.

  • Jason says:
    May 25, 2012 at 8:49 AM

    I'd like to add to the point raised by Wouter Oet that an explanation of Array.sort()followed by a sound implementation of the functionality it provides would be more beneficial then code that doesn't follow any kind of coding standard, has no comments, and doesn't compile properly as this poses a barrier for beginners whilst simultaneously ignoring the power of the language in question. Most languages such as Python, Perl, Ruby, PHP, and C# all have powerful sort functions built-in. It would also be useful to use syntax highlighting as it would allow people to consume your content in a more palatable fashion.

  • Techman says:
    May 25, 2012 at 9:08 AM

    @Jason I'm sorry that the code had errors which rendered it uncompilable.The Errors has been rectified and Compilable Code is Posted Above.Thanks for the Comment.

  • This comment has been removed by a blog administrator.
    Domain Registration says:
    December 21, 2012 at 11:09 PM

    This comment has been removed by a blog administrator.

  • yogesh says:
    November 2, 2015 at 2:27 AM

    nice one

  • Hitesh Kumar says:
    December 13, 2015 at 1:06 AM

    Selection Sort in C

    Selection sort is simplest way to sort array elements. Selection sort is based of maximum and minimum value. First check minimum value in array list and place it at first position (position 0) of array, next find second smallest element in array list and place this value at second position (position 1) and so on. Same process is repeated until sort all element of an array.

  • akmal niazi khan says:
    June 24, 2016 at 12:07 PM

    This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from

Post a Comment

Subscribe

The Source Codes Published in this Blog can be used freely for Educational purposes but should not be reproduced on any other Blog or Website without the consent of the author.