Traversing a Sorted Linear Array
Here A is an array with N elements with subscript Lower bound (LB) = 0 and upper
bound (UP) = N– 1 where N is the size of the array:
K = LB and UB = N – 1
Repeat steps 3 to 4 while K <= UB
Display element A[K]
K = K + 1
Inserting Item into Sorted Linear Array
insertItem()
Get value of ITEM to be inserted.
Call findLocationInsert()
findLocationInsert()
Search for position which ITEM can be inserted and Return position
Deleting item into Sorted Linear Array
deleteItem()
Get value of ITEM to be deleted
Call findLocationDelete()
findLocationDelete()
Search for the position in which ITEM will be deleted and return position
main() - Display option similar to this:
1. Insert value 2. Delete value 3. Traverse array 4. Exit
Provide switch statement which cases for the shown options are written.
array must be checked if it's already full or not.
Sample output:
Enter your choice: 1
Enter element to inserted: 5
Array Basic Operations
1. Insert value
2. Delete value
3. Traverse the array
4. Exit
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Input an option\n1. Insert value\n2. Delete value\n3. Traverse the array\n4. Exit");
int option=input.nextInt();
if(option==1){
System.out.println("Enter the Value to be inserted");
int v=input.nextInt();
System.out.println("Value "+v+" inserted successfully");
}
else if(option==2){
System.out.println("Input value to be deleted");
int n=input.nextInt();
System.out.println("Value "+n+" deleted successfully");
}
else if(option==3){
System.out.println("Elements of the array");
int[] LA = new int[]{5,7,1,2,8,2,12,11};
int LB =0;
int UB =LA.length;
int K=LB;
while(K<UB)
{
System.out.println(LA[K]);
K++;
}
}
else if(option==4){
System.out.println("");
}
}
}
Comments
Leave a comment