Md_Sakib_Hossain

Selection Sort Indexed Array

Mar 9th, 2021 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.62 KB | None | 0 0
  1. import java.util.*;
  2. public class SelectionSortIA{
  3.     public static void main(String[] args) {
  4.         int array[] = {-2,2,4,1,9,0,10};
  5.         int temp=0;//our temp
  6.  
  7.         for (int i=0;i<array.length -1 ;i++) {
  8.             int minIndex=i;
  9.             for (int j=i;j<array.length-1;j++) {//checking by comparing
  10.                 if(array[j]<array[minIndex]){ //here is check
  11.                     minIndex=j; //change index minIdex if it is bigger than our
  12.                     //minIndex
  13.                 }
  14.             }
  15.  
  16.              temp=array[i]; //swapping
  17.              array[i]=array[minIndex];
  18.              array[minIndex]=temp;
  19.         }
  20.  
  21.         for (int X : array) { //just printing
  22.             System.out.print(X+" ");
  23.         }
  24.     }
  25. }
Add Comment
Please, Sign In to add comment