Q:

Selection Sort in Java

belongs to collection: Java Searching and Sorting Programs

0

We can create a java program to sort array elements using selection sort. In selection sort algorithm, we search for the lowest element and arrange it to the proper location. We swap the current element with the next lowest number.

How does selection sort work?

The selection sort algorithm works in a very simple way. It maintains two subarray for the given array.

  • The subarray is already sorted.
  • And the second subarray is unsorted.

With every iteration of selection sort, an element is picked from the unsorted subarray and moved to the sorted subarray.

 

arr[] = 25 35 45 12 65 10  

// Find the minimum element in arr[0...5] and place it at beginning.  

10 25 35 45 12 65   

// Find the minimum element in arr[1...5] and place it at beginning of arr[1...5]  

10 12 25 35 45 65   

// Find the minimum element in arr[2...5] and place it at beginning of arr[2...5]  

No, you can see that the array is already sorted.   

10 12 25 35 45 65   

 

Time Complexity

Best: ?(n^2)
Average: ?(n^2)
Worst: O(n^2)

Space Complexity

O(1)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Selection Sort Java Example

public class SelectionSortExample {  
    public static void selectionSort(int[] arr){  
        for (int i = 0; i < arr.length - 1; i++)  
        {  
            int index = i;  
            for (int j = i + 1; j < arr.length; j++){  
                if (arr[j] < arr[index]){  
                    index = j;//searching for lowest index  
                }  
            }  
            int smallerNumber = arr[index];   
            arr[index] = arr[i];  
            arr[i] = smallerNumber;  
        }  
    }  
       
    public static void main(String a[]){  
        int[] arr1 = {9,14,3,2,43,11,58,22};  
        System.out.println("Before Selection Sort");  
        for(int i:arr1){  
            System.out.print(i+" ");  
        }  
        System.out.println();  
          
        selectionSort(arr1);//sorting array using selection sort  
         
        System.out.println("After Selection Sort");  
        for(int i:arr1){  
            System.out.print(i+" ");  
        }  
    }  
}  

Output:

Before Selection Sort
9 14 3 2 43 11 58 22 
After Selection Sort
2 3 9 11 14 22 43 58 

 

Selection Sort in Java (Another way)

You can also use a method where array is not predefined. Here, user has to put the elements as input.

In the following Java program, we ask user to enter the array elements or number, now compare the array's element and start swapping with the variable temp. Put the first element in the temp and the second element in the first, and then temp in the second number and continue for the next match to sort the whole array in ascending order.

import java.util.Scanner;  
  
public class SelectionSortExample2  
{  
   public static void main(String args[])  
   {  
       int size, i, j, temp;  
       int arr[] = new int[50];  
       Scanner scan = new Scanner(System.in);  
         
       System.out.print("Enter Array Size : ");  
       size = scan.nextInt();  
         
       System.out.print("Enter Array Elements : ");  
       for(i=0; i<size; i++)  
       {  
           arr[i] = scan.nextInt();  
       }  
         
       System.out.print("Sorting Array using Selection Sort Technique..\n");  
       for(i=0; i<size; i++)  
       {  
           for(j=i+1; j<size; j++)  
           {  
               if(arr[i] > arr[j])  
               {  
                   temp = arr[i];  
                   arr[i] = arr[j];  
                   arr[j] = temp;  
               }  
           }  
       }  
         
       System.out.print("Now the Array after Sorting is :\n");  
       for(i=0; i<size; i++)  
       {  
           System.out.print(arr[i]+ "  ");  
       }  
   }  
}  

Output:

Use image SelectionSort

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Insertion Sort in Java... >>
<< Bubble Sort in Java...