Facebook
From Sukruth Surendra NS , 1 Year ago, written in Java.
Embed
Download Paste or View Raw
Hits: 178
  1. import java.util.*;
  2.  
  3. public class Main
  4. {
  5.         public static void main(String[] args) {
  6.             Scanner sc = new Scanner(System.in);
  7.                 int n = sc.nextInt();
  8.                 int arr[] = new int[n];
  9.                 for (int i = 0 ; i < n ;i++ )
  10.                 {
  11.                     arr[i] = sc.nextInt();
  12.                 }
  13.                 //Selection sort
  14.                 for(int i = 0; i < n-1 ; i++)
  15.                 {
  16.                     int minidx = i;
  17.                    
  18.             for(int j = i+1 ; j < n ; j++)
  19.             {
  20.                 if (arr[j] < arr[minidx])
  21.                 {
  22.                     minidx = j;
  23.                 }
  24.                // swap
  25.                 int temp = arr[minidx];
  26.                 arr[minidx] = arr[i];
  27.                 arr[i] = temp;
  28.                
  29.             }
  30.                
  31.                 }
  32.                 System.out.print(Arrays.toString(arr));
  33.         }
  34. }

Replies to Selection Sort rss

Title Name Language When
Re: Selection Sort Bistre Cat java 1 Year ago.