Project 1 Java, C++
Project 1 Java, C++
import java.util.Random;
/**
*
* @author sehar
* @param <E>
*/
public class Array<E>{
private final Object[] obj_array;
public final int length;
//class constructer
public Array(int length){
obj_array=new Object [length]; //instantiates a new object array of specified length
this.length = length;
}
//generic class methods
public boolean isEmpty (){
return length == 0; //if length is zero it returns true otherwise false
}
Runtime
O(1)
public int LengthofArray(E arr[]){
int flag=0,i=0;
while(arr[i]!=null)
{
flag++;
i++;
}
return flag;
}
Runtime
O(1)
Runtime
O(n)
newarr[i]=arr[i-1];
}
return newarr;
}
Runtime
O(n+1)
}
else
c=1;
}
return c==0;
Runtime
O(n)
return arr;
}
// if the index is
// the removal element index
if (i == index) {
continue;
}
Runtime
O(n)
Runtime
O(n²)
Runtime
O(n)
public void selectionSort(E[] arr) {
int n = arr.length;
for (int i = 0; i < n-1; i++){
for (int j = 0; j < n-i-1; j++){
if (arr[j] > arr[j+1])
{
E temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
Runtime
O(n²)
Runtime
O(n)
Runtime
O(n)
// Function to get slice of a primitive array in Java
public E[] getSliceOfArray(E[] arr, int start, int end)
{
Runtime
O(n)
(C++)
#include <vector>
#include <iostream>
#include <any>
using namespace std;
template<E>
class Array
{
public:
const std::vector<std::any> obj_array;
public:
const int length;
//class constructer
Array(int length) : obj_array(std::vector<std::any>(length)) / *
instantiates a new object array of specified length */
{
length(length);
}
//generic class methods
virtual bool isEmpty()
{
return length == 0; //if length is zero it returns true otherwise false
}
virtual int LengthofArray(std::vector<E> &arr)
{
int flag = 0, i = 0;
while (arr[i] != nullptr)
{
flag++;
i++;
}
return flag;
}
virtual bool isEqual(std::vector<E> &arr, std::vector<E> &arr2)
{
int c = 0;
if (arr.size() == arr2.size()) //if both array are equals than we check
the elements of array other
{
for (int i = 0; i < arr.size(); i++)
{
if (arr[i] == arr2[i]) //Checking each element
{
c = 0;
}
else
{
c = 1;
break; //terminates the loop
}
}
return c == 0;
}
else
{
return false;
}
}
virtual std::vector<E> insertAt(std::vector<E> &arr, int pos, int n, E x)
{
std::vector<E> newarr =
static_cast<std::vector<E>>(std::vector<std::any>(n + 1)); //creating new array
with size n+1
for (int i = 0; i < n + 1; i++)
{
if (i < pos - 1) //if the iterator is before the required position
it copies the previous array to the new one
{
newarr[i] = arr[i];
}
else if (i == pos - 1)
{
newarr[i] = x;
}
else
{
}
else
{
c = 1;
}
}
return c == 0;
}
virtual std::vector<E> removeTheElement(std::vector<E> &arr, int index)
{
return arr;
}
// if the index is
// the removal element index
if (i == index)
{
continue;
}
delete random;
}
(Java)
import java.util.*;
/**
* @param args the command line arguments
*/
int n;
int k;
System.out.print("\n");
n= sc.nextInt();
System.out.print("\n");
arr[i]= sc.nextInt();
System.out.print("enter k ");
k = sc.nextInt();
if (m1.containsKey(k))
{ //returns yes if the element with key K is present in the map container.
System.out.print("Yes");
System.out.print("\n");
else
{
System.out.print("No");
System.out.print("\n");
Runtime
O(n)
//to find maximum number in the array and result will be 2*max+1 because maximum
achievable sum by adding two numbers in an array can be 2*max
//if max appear more than once. But at any cost, we can't make 2*max+1 using sum of two
numbers in an array
}
return 2* maxNum + 1; //result will be 2 times max + 1
int main(){
cout<<endl;
cout<< "Number which can't be formed as sum of two numbers in array: "<<result <<endl;
(JAVA)
class Main{
//to find maximum number in the array and result will be 2*max+1 because maximum achievable
sum by adding two numbers in an array can be 2*max
//if max appear more than once. But at any cost, we can't make 2*max+1 using sum of two
numbers in an array
}
public static void main(String[] args) {
System.out.print("Array: ");
System.out.print(arr[i]+ "");
System.out.println("");
int result;
System.out.print("Number which can't be formed as sum of two numbers in array: " + result);
Runtime
O(n²)
import java.util.*;
public class JavaApplication1 {
int k;
System.out.print("Type k value:");
k = sc.nextInt();
int n = arr.length;
bubbleSort(arr, n);
printArray(arr, n);
int i;
System.out.println();
int i, j, temp;
boolean swapped;
swapped = false;
for (j = 0; j < n - i - 1; j++)
temp = arr[j];
arr[j + 1] = temp;
swapped = true;
if (swapped == false)
break;
int i;
newarr[i] = arr[i];
newarr[n] = x;
return newarr;
(C++)
#include "snippet.h"
delete sc;
}
if (swapped == false)
{
break;
}
}
}
newarr[n] = x;
return newarr;
}
Runtime
O(n²)
String Functions
(Java)
public class NewClass {
//length of string
{
int i=0;
try{
for(i=0;;i++)
catch(Exception e)
return i;
Runtime
O(1)
char str[]=st.toCharArray();
for(int i=0;i<str.length;i++)
str[i]=(char)((int)str[i]+32);
}
}
Runtime
O(n)
char str[]=st.toCharArray();
for(int i=0;i<str.length;i++)
str[i]=(char)((int)str[i]-32);
Runtime
O(n)
String str;
str = s1 + s2;
return str;
Runtime
O(1)
//copy source string to destination
int n=ch.length;
ch2=new char[n];
int i = 0;
for(int i=0;i<n;i++){
ch2[i]=ch[i];
ch2[i]='\0';
Runtime
O(n)
if(a.length() != b.length()) {
return false;
for(int i=0;i<a.length();i++) {
if(a.charAt(i) != b.charAt(i)) {
return false;
return true;
}
Runtime
O(n)
(C++)
#include "snippet.h"
//Checking length of String
int NewClass::getLengthOfString(const std::wstring &str)
{
int i = 0;
try
{
for (i = 0;;i++)
{
wchar_t charAt = str[i];
}
}
catch (const std::runtime_error &e)
{
}
return i;
}
//From Upper to Lower
#include "snippet.h"
}
ch2[i] = L'\0';