in JAVA by
How to perform Selection Sort in Java?

1 Answer

0 votes
by
Consider the following program to perform selection sort in Java.

public class SelectionSort {  

public static void main(String[] args) {  

  int[] a = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};  

  int i,j,k,pos,temp;  

  for(i=0;i<10;i++)  

  {  

    pos = smallest(a,10,i);  

    temp = a[i];  

    a[i]=a[pos];  

    a[pos] = temp;  

  }  

  System.out.println("\nprinting sorted elements...\n");  

  for(i=0;i<10;i++)  

  {  

    System.out.println(a[i]);  

  }  

}  

public static int smallest(int a[], int n, int i)  

{  

  int small,pos,j;  

  small = a[i];  

  pos = i;  

  for(j=i+1;j<10;j++)  

  {  

    if(a[j]<small)  

    {  

      small = a[j];  

      pos=j;  

    }  

  }  

  return pos;  

}  

}  

Output:

printing sorted elements...

7

9

10

12

23

23

34

44

78

101

Related questions

+2 votes
asked May 13, 2021 in JAVA by rajeshsharma
0 votes
asked Feb 2, 2021 in JAVA by SakshiSharma
0 votes
asked May 4, 2021 in JAVA by SakshiSharma
...