Arrays - Revised
Arrays - Revised
for(int i=0;i<5;i++)
n[i]=sc.nextInt(); if (flag==1)
lb=0 Sopln(“Found ar”+(mid+1);
Ub=4 else
mid=-1; Sopln(“Not Found”);
search=sc.nextInt();
while(lb<=ub)
{ mid=(lb+ub)/2;
if( search >n[mid])
lb=mid+1;
if( search <n[mid])
ub=mid-1;
if(n[mid]==search)
{ flag=1; break;}}
Write a program to declare an array with the year of graduation from school as an integer value. Read a year to be
searched. Output the message “Record exists” if the value input is located in the array. If not, output the message
“Record does not exist”. Using Binary Search
{1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010}
import java.util.*;
class Binary
{ psvm()
{ Scanner sc=new Scanner(System.in);
int n[]={1982, 1987, 1993, 1996, 1999, 2003, 2006, 2007, 2009, 2010 };
int lb=0 ; int ub=9; int mid=-1; int flag=0;
Sopln(“Enter number to be searched”);
int search=sc.nextInt();
while(lb<=ub)
{ mid=(lb+ub)/2; if (flag==1)
if( search >n[mid]) Sopln(“Record exists”);
lb=mid+1; else
Sopln(“Record does not exists”);
if( search <n[mid])
}}
ub=mid-1;
if(n[mid]==search)
{ flag=1; break;}}
Read 2 array of length 5 and merge them into one array.
Read 2 array of length 5 and merge them into one array.
import java.util.*; for( int i=0 ;i<5;i++)
class Merge {
{ psvm() n2[i]=n[i];
{ Scanner sc=new Scanner(System.in); }
int n[]=new int[5];
int n1[]=new int[5]; for( int i=0 ;i<5;i++)
int n2[]=new int[10]; { n2[i+5]=n1[i];
}
Sopln(“Enter 5 nos in first array”); OR
for( int i=0;i<5;i++) for( int i=0 , j=5; i<5; i++, j++)
{ n[i]=sc.nextInt(); { n2[j]=n1[i];
}
}
Sopln(“Enter 5 nos in sec array”); Sopln(“Merged array is”)
for( int i=0;i<5;i++) for( int i=0 ;i<10;i++)
Sopln(n2[i]);
{ n1[i]=sc.nextInt(); }}
}
Difference between Linear and Binary Search
Linear Binary
1.Array elements need not be in sorted order 1.Array elements need to be in sorted order
2. Slow as it checks each element of array 2.Fast as it skips half the elements
3.Search begins from 0th position of array 3. Array is divided into 2 halves and desired
data is searched in first half or second half
WAP to read 5 number and sort them in ascending order using bubble sort
import java.util.*;
class array
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int n[]=new int[5];
Sopln(“Enter 5 nos”);
for( int i=0;i<5;i++)
{ n[i]=sc.nextInt();
}
for( int i=0;i<4;i++)
{ for( int j=0;j<4-i;j++)
{ if( n[j]> n[j+1]) Sopln(“The sorted array is”);
{ int temp=n[j]; for( int i=0;i<5;i++)
Sopln(n[i]);
n[j]=n[j+1]; }
n[j+1]=temp;}}} }
WAP to read 5 names and
sort them in ascending order import java.util.*;
class array
using bubble sort {
public static void main()
{
Scanner sc=new Scanner(System.in);
String n[]=new String [5];
Sopln(“Enter 5 names”);
for( int i=0;i<5;i++)
{ n[i]=sc.next();
}
for( int i=0;i<4;i++)
{ for( int j=0;j<4-i;j++)
continued
for( int i=0;i<9;i++)
{ for( int j=0;j<9-i;j++)
{ if( S[j].compareTo( S[j+1]) <0)
{ String temp=S[j];
S[j]=S[j+1];
S[j+1]=temp;
String tem= C[j];
C[j]=C[j+1];
C[j+1]=tem;
}}}
Selection Sort
Nos in ascending order n[j]< n[min]
Nos in descending order n[j] >n[mini]
String is ascending n[j].compareTo(n[min])<0
String is descending n[j].compareTo(n[min])>0
WAP to read 5 names and sort them in ascending order using Selection sort
import java.util.*;
class array
{
public static void main()
{
Scanner sc=new Scanner(System.in);
String n[]=new String [5];
Sopln(“Enter 5 names”);
for( int i=0;i<5;i++)
{ n[i]=sc.next();
}
for( int i=0;i<4;i++) Sopln(“The sorted
{ int min=i;
{ for( int j=i+1;j<5j++) array is”);
for( int i=0;i<5;i++)
{ if( n[j].compareTo( n[min]) <0) Sopln(n[i]);
{ min=j; }
}
String temp=n[min]; }
n[min]=n[i];
n[i]=temp
}
read 2D array 3*3 in java and print the elements in matrix form
import java.util.Scanner;
class Matrix {
public static void main() {
int[][] matrix = new int[3][3]; Scanner sc = new Scanner(System.in)
System.out.println("Enter elements for a 3x3 matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = sc.nextInt(); } }
System.out.println("The 3x3 matrix is:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(matrix[i][j] + " ");
} System.out.println();
} }}
read 2D array 3*3 in java and add the elements of each row and column separately
import java.util.Scanner;
class MatrixSumExample {
public static void main()
int[][] matrix = new int[3][3];
Scanner sc = new Scanner(System.in);
System.out.println("Enter elements for a 3x3 matrix:");
class OddEvenSumExample {
public static void main() {
int[][] matrix = new int[3][3];
Scanner scanner = new Scanner(System.in);
System.out.println("Enter elements for a 3x3 matrix:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = scanner.nextInt(); } }
int oddSum = 0; int evenSum = 0;