Question: For the below C# code, what is the BigO notation for each operation? using System; namespace MList { class MyList { private int[] arr; private
For the below C# code, what is the BigO notation for each operation?
using System;
namespace MList
{
class MyList
{
private int[] arr;
private int MaxSize;
private int LastElementList = 0;
public MyList(int size)
{
arr = new int[size];
MaxSize = size;
}
public int GetMaxSize()
{
return arr.Length;
}
public void addBack(int value)
{
if (LastElementList == MaxSize)
{
Console.WriteLine("Array is full. Can not add to the list");
}
else
{
arr[LastElementList] = value;
LastElementList++;
}
}
public void printAll()
{
for (int i=0; i< LastElementList; i++)
{
Console.WriteLine(arr[i] + ",");
}
}
public void addFront(int val)
{
if(isFull()!=true)
{
for(int i = LastElementList; i>0;i--)
{
arr[i]=arr[i-1];
}
arr[0]=val;
++LastElementList;
}
else
Console.WriteLine("List is FULL!!");
}
public bool isEmpty()
{
if(LastElementList< MaxSize)
return false;
return true;
}
public bool isFull()
{
if(LastElementList >=MaxSize)
return true;
return false;
}
public int size()
{
return LastElementList;
}
public void add(int value)
{
//by default it adds at back
addBack(value);
}
public void insert(int index,int value)
{
if(isFull() !=true)
{
//move all elements to right side of array as we are going to place value at index 0
for(int i = LastElementList;i>index;i--)
{
arr[i]=arr[i-1];
}
arr[index]=value;
++LastElementList;
}
}
public void deleteBack()
{
//delete last element
--LastElementList;
}
public void deleteFront()
{
//shift elements of array to left as element at index 0 is removed
for(int i=1; i < MaxSize;i++)
{
arr[i-1]=arr[i];
}
--LastElementList;
}
public void delete(int index)
{
for(int i = index; i { //push elements to left as value at specified indes will be delete arr[i]=arr[i+1]; } --LastElementList; } public bool contains(int value) { for(int i = 0; i < LastElementList; i++) { if(value == arr[i]) return true; } return false; } public void clear() { //make all values 0 and LastElementList=0 for(int i = 0; i < LastElementList;i++) { arr[i]=0; } LastElementList=0; } public void sort() { //sort using bubble sort int tmp; for(int i = 0; i < LastElementList; i++) { for(int j = 0; j { if(arr[j] > arr[j+1]) { tmp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=tmp; } } } } } } ==================================== //main.cs using System; using MList; class MainClass { public static void Main (string[] args) { MyList arrlist = new MyList(50); int max = arrlist.GetMaxSize(); arrlist.addBack(11); arrlist.addBack(2); arrlist.printAll(); Console.WriteLine("My ArrayList has a max of {0} elements", max); //Console.ReadLine(); //test add front arrlist.addFront(5); Console.WriteLine("Content of list after adding 5 at front"); arrlist.printAll(); //test addLast arrlist.addBack(4); Console.WriteLine("Content of list after adding 4 at back"); arrlist.printAll(); //test contains function if(arrlist.contains(4) == true) { Console.WriteLine("List contains element 4"); } //test insert at index arrlist.insert(1,6); Console.WriteLine("Content of list after adding 6 at index 1"); arrlist.printAll(); arrlist.addFront(8); arrlist.addBack(10); Console.WriteLine("Content of list after adding 8 at front and 10 at back"); arrlist.printAll(); //delete at front and back arrlist.deleteBack(); Console.WriteLine("Content of list after deleting at back of list "); arrlist.printAll(); arrlist.deleteFront(); Console.WriteLine("Content of list after deleting at front of list "); arrlist.printAll(); //delete at index arrlist.delete(2); Console.WriteLine("Content of list after deleting at index at 2"); arrlist.printAll(); //test sort function arrlist.sort(); Console.WriteLine("Content of list after sorting "); arrlist.printAll(); //test clear function arrlist.clear(); Console.WriteLine("Content of list after calling clear function "); arrlist.clear(); Console.WriteLine("size of list after clear : "+arrlist.size()); } }
Step by Step Solution
There are 3 Steps involved in it
Lets analyze the Big O notation for each operation in the provided C code StepbyStep Analysis Constructor MyListint size Operation Initializes an arra... View full answer
Get step-by-step solutions from verified subject matter experts
