Posts

Showing posts from March, 2024

Finding the shortest path

  public class shortestpath {     public static void getpath ( String path ) {         int x = 0 , y = 0 ;         for ( int i = 0 ; i < path . length (); i ++ ) {             if ( path . charAt ( i ) == 'E' ) {                 x += 1 ;             } else if ( path . charAt ( i ) == 'W' ) {                 x -= 1 ;             } else if ( path . charAt ( i ) == 'N' ) {                 y += 1 ;             } else if ( path . charAt ( i ) == 'S' ) {                 y -= 1 ;             }         }         int x2 = x * x ;       ...

Finding if a string is a palindrome or not

  /**  * palindrome  */ public class palindrome {     public static boolean palindrome ( String str ) {         int n = str . length ();         for ( int i = 0 ; i < n / 2 ; i ++ ) {             if ( str . charAt ( i ) != str . charAt ( n - 1 - i )) {                 return false ;             }         }         return true ;     }     public static void main ( String [] args ) {         String str = "racer" ;         if ( palindrome ( str )) {             System . out . println ( "string is a palindrome" );         } else {             System . out . println ( "string is not a palindrome" );     ...

Transpose of a matrix

  public class transpose {     public static void transpose ( int n [][], int row , int col ) {         int transpose [][] = new int [ col ][ row ];         for ( int i = 0 ; i < row ; i ++ ) {             for ( int j = 0 ; j < col ; j ++ ) {                 transpose [ j ][ i ] = n [ i ][ j ];             }         }         print ( transpose );     }     public static void print ( int n [][]) {         for ( int i = 0 ; i < n . length ; i ++ ) {             for ( int j = 0 ; j < n [ 0 ]. length ; j ++ ) {                 System . out . print ( n [ i ][ j ] + " " );             }       ...

Searching in sorted matrix (STAIRCASE SEARCH)

  public class staircasesearch {     public static boolean staircasesearchsortedmatrix ( int n [][], int key ) {         int row = 0 ;         int col = n [ 0 ]. length - 1 ;         while ( row < n . length && col >= 0 ) {             if ( n [ row ][ col ] == key ) {                 System . out . println ( "key found at" + "(" + row + "," + col + ")" );                 return true ;             } else if ( key < n [ row ][ col ]) {                 col -- ;             } else {                 row ++ ;             }         }         System ...

Diagonal sum(optimised)

  public class diagonalsum2 {     public static void diagonalsum ( int n [][]) {         int sum = 0 ;         for ( int i = 0 ; i < n . length ; i ++ ) {             sum += n [ i ][ i ];             if ( i != n . length - 1 - i ) {                 sum += n [ i ][ n . length - 1 - i ];             }         }         System . out . println ( sum );     }     public static void main ( String [] args ) {         int n [][] = { { 1 , 2 , 3 }, { 5 , 6 , 7 }, { 8 , 9 , 10 } };         diagonalsum ( n );     } }

Diagonal sum

  public class diagonalsum {     public static void diagonal ( int n [][]) {         int sum = 0 ;         for ( int i = 0 ; i < n . length ; i ++ ) {             for ( int j = 0 ; j < n . length ; j ++ ) {                 if ( i == j ) {                     sum += n [ i ][ j ];                     ;                 } else if ( i + j == n . length - 1 ) {                     sum += n [ i ][ j ];                 }             }         }         System . out . println ( sum );     }     public static void ...

Spiral matrix

  public class spiralmatrix {     public static void spiral ( int matrix [][]) {         int startrow = 0 ;         int endrow = matrix . length - 1 ;         int startcol = 0 ;         int endcol = matrix [ 0 ]. length - 1 ;         while ( startrow <= endrow && startcol <= endcol ) {             // TOP             for ( int i = startcol ; i <= endcol ; i ++ ) {                 System . out . print ( matrix [ startrow ][ i ] + " " );             }             // RIGHT             for ( int i = startrow + 1 ; i <= endrow ; i ++ ) {                 System . out . print ( matrix [ i ][ ...

2D Arrays

  /**  * declaration  */ import java . util .* ; public class declaration {     public static boolean search ( int matrix [][], int key ) {         for ( int i = 0 ; i < matrix . length ; i ++ ) {             for ( int j = 0 ; j < matrix [ 0 ]. length ; j ++ ) {                 if ( matrix [ i ][ j ] == key ) {                     System . out . println ( "key found at " + "(" + i + "," + j + ")" );                     return true ;                 }             }         }         System . out . println ( "key not found" );         return false ;     }     public static void main ( St...

PRACTICE Descending order

  public class practice {     public static void bubblesort ( int n []) {         for ( int i = 0 ; i < n . length - 1 ; i ++ ) {             for ( int j = 0 ; j < n . length - 1 - i; j ++ ) {                 if (n[j] < n[j + 1 ]) {                     int temp = n[j];                     n[j] = n[j + 1 ];                     n[j + 1 ] = temp;                 }             }         }     }     public static void selectionsort ( int n []) {         for ( int i = 0 ; i < n . length - 1 ; i ++ ) {             int min = i; ...

Count sort

  public class countsort {     public static void count ( int n []) {         int max = Integer . MIN_VALUE ;         for ( int i = 0 ; i < n . length ; i ++ ) {             max = Math . max ( max , n [ i ]);         }         int count [] = new int [ max + 1 ];         for ( int i = 0 ; i < n . length ; i ++ ) {             count [ n [ i ]] ++ ;         }         int j = 0 ;         for ( int i = 0 ; i < count . length ; i ++ ) {             while ( count [ i ] > 0 ) {                 n [ j ] = i ;                 j ++ ;                 count [ i ] -- ; ...

Inbuilt sort

  import java . util .* ; public class inbuiltsort {     public static void print ( Integer n []) {         for ( int i = 0 ; i < n . length ; i ++ ) {             System . out . print ( n [ i ] + " " );         }     }     public static void main ( String [] args ) {         Integer n [] = { 333 , 17 , 18 , 7 , 3 , 45 };         // Arrays.sort(n);         // Arrays.sort(n,2,4);         // Arrays.sort(n,Collections.reverseOrder());         Arrays . sort ( n , 2 , 4 , Collections . reverseOrder ());         print ( n );     } }

Insertion sort

  public class insertionsort {     public static void insertion ( int n []) {         for ( int i = 1 ; i < n . length ; i ++ ) {             int curr = n [ i ];             int prev = i - 1 ;             while ( prev >= 0 && n [ prev ] > curr ) {                 n [ prev + 1 ] = n [ prev ];                 prev -- ;             }             n [ prev + 1 ] = curr ;         }         for ( int i = 0 ; i < n . length ; i ++ ) {             System . out . print ( n [ i ] + " " );         }     }     public static void main ( String [] args ) {     ...

Selection sort

  public class selectionsort {     public static void selection ( int n []) {         for ( int i = 0 ; i < n . length - 1 ; i ++ ) {             int min = i ;             for ( int j = i + 1 ; j < n . length ; j ++ ) {                 if ( n [ min ] > n [ j ]) {                     min = j ;                 }             }             int temp = n [ min ];             n [ min ] = n [ i ];             n [ i ] = temp ;         }         for ( int i = 0 ; i < n . length ; i ++ ) {             System . out . print ( n [ i ] + " "...

Bubble sort

  public class bubblesort {     public static void bubble ( int n []) {         for ( int i = 0 ; i < n . length - 1 ; i ++ ) {             for ( int j = 0 ; j < n . length - 1 - i ; j ++ ) {                 if ( n [ j ] > n [ j + 1 ]) {                     int temp = n [ j ];                     n [ j ] = n [ j + 1 ];                     n [ j + 1 ] = temp ;                 }             }         }         for ( int j = 0 ; j < n . length ; j ++ ) {             System . out . print ( n [ j ] + " " );         }   ...