Using selection sort algorithm sorts arr in ascending order

Question

Student writes this method
Sorts arr in ascending order using the selection sort algorithm. Adds a counter to count the number of iterations.
Public int selectionSort()
// Note: To count the number of iterations use a counter.
// The variable counter has been declared and initialized for you.

Summary

In this question, we have to apply a selection sorting algorithm. Where we enter the different numbers And when the program ends we will see the number in the sorting order. To perform such an operation we need an iteration. To perform such an operation we will apply a for loop here.

Explanation

Here in the Selection Sort algorithm, We take a class name Sort. After that, we take a static method as selection Sort where we pass a parameter as an array with the name a. In that method we enter a and declare the counter to 0. After that, we add the for loop for sorting in ascending order. So After that, there is another for loop with j to have a length. In the second for loop, there is a counter++ to increment the counter. Once the for loop is complete there is a Swapping of numbers.

This will need to arrange the number. After there is a statement that ‘return counter’, this will return the counter. After that, there is a main method of java. So Next that there is a declaration of an array with the random number. After that, we print the output statement. Once all this is done there is output with the sorted number as output.

Code

class Sort
{  
    /* to perform the selection sort */
    public static int selectionSort(int[] a)
    {  
        /*  to have the number that we will have after the comparision */
        int counter=0;
        /* each iteration to get the ith smallest number */
        for (int i=0;i<a.length;i++)  
        {  
            int ind=i;  
            /* among the remaining numbers after index i,
            getting the smallest number and replacing with the 
            ith element */
            for (int j=i+1; j<a.length; j++)
            {  
              counter++;
                if (a[j]<a[ind]) 
                    ind=j;
            }  
            /* swapping the numbers */
            int small=a[ind];   
            a[ind]=a[i];  
            a[i]=small;  
        }  
        return counter;
    }  
     
    public static void main(String args[])
    {  
        int[] arr={22,17,98,75,11,3,2,95,34};  
        System.out.println("Before Sorting: ");  
        for(int i=0;i<arr.length;i++)  
            System.out.print(arr[i]+" ");  
        System.out.println();  
          
        int count=selectionSort(arr); 
         
        System.out.println("After performing selection sort: ");  
        for(int i=0;i<arr.length;i++)
            System.out.print(arr[i]+" ");  
        System.out.println("\nNumber of iterations: "+count);
    }  
}  

Output

selection sort algorithm

 

Also read, Dijkstra’s algorithm uses a greedy approach to solve the single-source shortest path (SSSP) problem in a weighted directed graph.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *