Skip to main content

Posts

Showing posts with the label Day 20: Sorting Hackerrank Solution in Java

Day 20: Sorting Hackerrank Solution Java

 Day 20: Sorting Hackerrank Solution Java For Explanation Watch Video: Sample Input 0 3 1 2 3 Sample Output 0 Array is sorted in 0 swaps. First Element: 1 Last Element: 3 Code: import  java.io.*; import  java.util.*; import  java.text.*; import  java.math.*; import  java.util.regex.*; public   class  Solution {      public   static   void  main(String[] args) {         Scanner in =  new  Scanner(System.in);          int  n = in.nextInt();          int [] a =  new   int [n];          for ( int  a_i= 0 ; a_i < n; a_i++){             a[a_i] = in.nextInt();         }          int  numberOfSwaps =  0 ;          for  ( int  i =  0 ; i < n; i++) {                   for  ( int  j =  0 ; j < n -  1 ; j++) {                  // Swap adjacent elements if they are in decreasing order                  if  (a[j] > a[j +  1 ]) {                      int  temp = a[j];                     a[j] = a[j+ 1 ];                     a[j+ 1 ] = temp;                     numberOfSwaps++;