COMP3040-02 Data Structures Assignment 2 Part 1
COMP3040-02 Data Structures Assignment 2 Part 1
Answer:
Search Operation
Delete Operation
Answer:
Because in an unsorted array, the insert operation is faster as compared to sorted array because
we don’t have to care about the position at which the element is to be placed.
3. What is the time complexity of deleting an element from a sorted array? Does it have better
time complexity than deleting a node from an unsorted array? Why?
Answer:
The time complexity is O(n), it’s not better because is O(n) in worst case all elements may have to
be moved.
4. Programming
4.1 Answer:
//Add a testing class that can test all the operations defined in the unsortedArrayAccess class.
import java.util.Scanner;
}
if(option==2)
{
System.out.print("Please type element to find: ");
double Item=Double.parseDouble(new Scanner(System.in).nextLine());
int pos=myArr.search(Item);
if(pos!=-1)
System.out.println("Found in position ",pos);
}
if(option==3)
{
double Item=myArr.remove();
System.out.println("Removed item: ",Item);
}
if(option==4)
{
myArr.display();
}
System.out.print("Enter 1 for insert, 2 for search, 3 for remove, 4 for Display 5 for Exit: ");
option=Integer.parseInt(new Scanner(System.in).nextLine());
}
}
4.2 Answer:
package test4_2;
import java.util.Scanner;
System.out.print("Enter 1 for insert, 2 for Binary search, 3 for Delete, 4 for Display 5 for Exit:
");
int option=Integer.parseInt(new Scanner(System.in).nextLine());
}
if(option==2)
{
System.out.print("Please type element to find: ");
double Item=Double.parseDouble(new Scanner(System.in).nextLine());
int pos=myArr.BinarySearch(Item);
if(pos!=-1)
System.out.println("Found in position "+pos);
}
if(option==3)
{
System.out.print("Please type the element Key to Delete: ");
double Item=Double.parseDouble(new Scanner(System.in).nextLine());
myArr.deletion(Item);
System.out.println("Removed item");
}
if(option==4)
{
myArr.display();
}
System.out.print("Enter 1 for insert, 2 for Binary search, 3 for Delete, 4 for Display 5 for
Exit: ");
option=Integer.parseInt(new Scanner(System.in).nextLine());
}
}
}
4.3 Answer:
package test4_3;
import java.util.*;
double avg=0;
avg=avg/arraySize;
TESTING FILE
package test4_3;
import java.util.Scanner;
System.out.print("Enter 1 for insert, 2 for Binary search, 3 for Delete, 4 for Display 5 for Exit:
");
int option=Integer.parseInt(new Scanner(System.in).nextLine());
}
if(option==2)
{
System.out.print("Please type Employee ID to find: ");
int Item=Integer.parseInt(new Scanner(System.in).nextLine());
int pos=myArr.BinarySearch(Item);
if(pos!=-1)
System.out.println("Found in position "+pos);
}
if(option==3)
{
System.out.print("Please type the Employee ID to Delete: ");
int Item=Integer.parseInt(new Scanner(System.in).nextLine());
myArr.deletion(Item);
System.out.println("Removed Employee");
}
if(option==4)
{
myArr.display();
myArr.highesSalary();
myArr.averageSalary();
}
System.out.print("Enter 1 for insert, 2 for Binary search, 3 for Delete, 4 for Display 5 for
Exit: ");
option=Integer.parseInt(new Scanner(System.in).nextLine());
}