All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Selection Sort In Java

Last Updated : Mar 11, 2024

Selection Sort In Java

In this article we will show you the solution of selection sort in java, as usual you need to import package, in case you using eclipse software then itself that will imports.

After we defined main class with two methods, sorting() for process selection iteration and print() for display sorted array values.

In main method we creating object for main class then calling separately defined methods.

Step By Step Guide On Selection Sort In Java :-

In SelSort() class defined two methods definition, sorting function sorting array values in selection sort concept. That means we have to find smallest number in the array then rearranging that value.

Found smallest number will placed on first position then remaining considered as subarray needs to sort.

Likewise, we finding small digit then placing at front with order. The print() method printing sorted array at last.

At main method initialized array arr with some values and created object for main class to access sorting and displaying methods.

//Selection Sort Example
package Sorts;
class SelSort
{
    void sorting(int arr[])
    {
        int len = arr.length;
        for (int i = 0; i < len-1; i++)
        {
            int sml = i;
            for (int j = i+1; j < len; j++)
                if (arr[j] < arr[sml])
                    sml = j;
            int temp = arr[sml];
            arr[sml] = arr[i];
            arr[i] = temp;
        }
    }
    void print(int arr[])
    {
        int len = arr.length;
        for (int i=0; i<len; ++i)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
    public static void main(String args[])
    {
     SelSort obj = new SelSort();
        int arr[] = {18,25,6,12,2};
        obj.sorting(arr);
        System.out.println("Sorted array");
        ob.print(arr);
    }
}
  1. The ‘Package Sorts’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. First we defined main class 'SelSort', then we defined sorting() method. There we collection array length by 'length' property and stored on variable len.
  3. To iterate array values we defined for loop, variable 'i' doing the iteration initially we sets its value to '0'. Then we sets condition statement to until last index value.
  4. We stores array values first index to variable 'sml', then we created another loop with iterating variable 'j'. There we sets initial value as next index of first loop value 'i'.
  5. In second loop you will compare the first value with all remaining values in array. When you finds lowest number from the remaining thats sent to first index by swapping inside second loop.
  6. Likewise, each time finding next lowest value if found sent to near first index value and starts the next iteration.
  7. If not found that will not need to take a move remains at the index. Finally you ordered everything in array.
  8. In print() method we displayed all sorted array by using for loop. In main method you need to create object 'obj' for main class to call the functions orderedly.
  9. As well as we initialized array with some numeric in main method and passed it to both function call to access.
  10. The ‘public static void main(String[] args)’ is defined that's starting point from where compiler starts program execution.
  11. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  12. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to do selection sorting in java. To compile java program you need to tell the java compiler which program to compile through ‘javac SelSort.java’.

After successful compilation you need to execute ‘java SelSort‘line for gets the output.

Then you can see text 'Sorted Array' with '2 6 12 18 25' sorted values of output, that may be vary if you change the defined array values.

On eclipse just press of run button then you will get output inside terminal.

I hope this article on selection sort in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪