You are on page 1of 9

1. What are the fundamental operations of an unsorted array?

Answer:

 Search Operation
 Delete Operation

2. Why is the insertion not supported for unsorted array?

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;

public class Test4_1


{

public static void main(String[] args)


{

unsortedArrayAccess myArr=new unsortedArrayAccess(10);


System.out.print("Enter 1 for insert, 2 for search, 3 for remove, 4 for Display 5 for Exit: ");
int option=Integer.parseInt(new Scanner(System.in).nextLine());

while(option==1 || option==2 || option==3 || option==4)


{
if(option==1)
{
System.out.print("Please type the new element: ");
double NewItem=Double.parseDouble(new Scanner(System.in).nextLine());
myArr.append(NewItem);

}
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;

public class Test4_2 {

public static void main(String[] args)


{
sortedArrayAccess myArr=new sortedArrayAccess(10);

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());

while(option==1 || option==2 || option==3 || option==4)


{
if(option==1)
{
System.out.print("Please type the new element: ");
double NewItem=Double.parseDouble(new Scanner(System.in).nextLine());
myArr.insertion(NewItem,NewItem);

}
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.*;

public class sortedArrayAccessEmp


{
public static Employee[] arr;
private int arraySize;

public sortedArrayAccessEmp(int scale)


{
arr = new Employee[scale];
arraySize = 0;
}

public final Employee get(int i)


{
return arr[i];
}

public final int BinarySearch(int Key)


{
int k = 0;
int lower = 0;
int upper = arraySize - 1;
while (lower < upper)
{
k = (lower + upper + 1) / 2;
if (Key == arr[k].id)
{
break;
}
if (Key < arr[k].id)
{
upper = k - 1;
}
else
{
lower = k + 1;
}
}
if (lower == upper)
{
k = lower;
}
if (Key == arr[k].id)
{
return k;
}
else
{
System.out.println("The Employee cannot be found!");
return -1;
}
}

public final void insertion(int Key, Employee Item)


{
if (arraySize == 0)
{
arr[0] = Item;
}
/* find the position for interting the given item */
int position = 0;
while (Key > arr[position].id && position < arraySize)
{
position++;
}
for (int i = arraySize; i > position; i--)
{
arr[i] = arr[i - 1];
}
arr[position] = Item;
arraySize = arraySize + 1;

public final void deletion(int Key)


{
/* find the given item */
int position = BinarySearch(Key);
if (position != -1)
{
for (int i = position; i < arraySize - 1; i++)
{
arr[i] = arr[i + 1];
}
arraySize = arraySize - 1;
};
}

public final void display()


{
if (arraySize != 0)
{
for (int i = 0; i < arraySize; i++)
{
arr[i].Output();
}
};

System.out.println("The number of Employees is " + arraySize);


}

/* find the employee who has the highest salary.*/


public void highesSalary()
{
int index=0;
double MaxS=arr[0].salary;

for(int i=0; i<arraySize;i++)


if(arr[i].salary>=MaxS)
{
MaxS=arr[i].salary;
index=i;
}

System.out.println("The employee with the highest salary is: ");


arr[index].Output();
}

/*find the average salary of all employees*/


public void averageSalary()
{

double avg=0;

for(int i=0; i<arraySize;i++)


{
avg=avg+arr[i].salary;
}

avg=avg/arraySize;

System.out.println("The verage salary of all employees is "+avg);


}

TESTING FILE

package test4_3;

import java.util.Scanner;

public class Test4_3 {

public static void main(String[] args)


{

sortedArrayAccessEmp myArr=new sortedArrayAccessEmp(10);


Employee NewItem;

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());

while(option==1 || option==2 || option==3 || option==4)


{
if(option==1)
{
NewItem=new Employee();
NewItem.Input();
myArr.insertion(NewItem.id,NewItem);

}
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());
}

You might also like