You are on page 1of 7

import java.util.

Scanner;
import java.util.ArrayList;

public class mainShoes2


{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
ArrayList shoeList = new ArrayList(10);

System.out.print("----------ENTER DATA----------");

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


{
System.out.print("\nEnter brand : ");
String brand = in.nextLine();

System.out.print("\nEnter size (EUR) : ");


int size = in.nextInt();

in.nextLine();

Shoes sh = new Shoes(brand, size);


shoeList.add(sh);
}

System.out.print("\n----- UNSORTED list -----");

for (int i=0; i<shoeList.size(); i++)


{
Object ob = shoeList.get(i);
Shoes sh = (Shoes) ob;

System.out.print("\n"+sh.getBrand());
}

String array[] = new String[5];


in.nextLine();

//Insertion sort
insertionSort(shoeList);

System.out.print("\n----- SORTED list -----");

for (int i=0; i<shoeList.size(); i++)


{
Object ob = shoeList.get(i);
Shoes sh = (Shoes) ob;

array[i] = sh.getBrand();
System.out.print("\n"+sh.toString());
}

//Binary search
System.out.print("\n\nEnter brand to search : ");
String x = in.nextLine();

int result = binarySearch(array, x);

if (result == -1)
System.out.println("\nElement not present");
else
{

System.out.println("\nElement found at "


+ "index " + result);
shoeList.remove(result);
}

System.out.print("\n----- NEW list -----");

for (int i=0; i<shoeList.size(); i++)


{
Object ob = shoeList.get(i);
Shoes sh = (Shoes) ob;

System.out.print("\n"+sh.toString());
}

System.out.print("\n\nEnter new data at index 4");


System.out.print("\nEnter brand : ");
String brand = in.nextLine();

System.out.print("Enter size :");


int size = in.nextInt();

//Add a new item at index 4


Shoes sh1 = new Shoes(brand, size);
shoeList.add(4, sh1);

System.out.print("\n----- UPDATED list -----");

for (int i=0; i<shoeList.size(); i++)


{
Object ob = shoeList.get(i);
Shoes sh = (Shoes) ob;

System.out.print("\nindex : "+i+"\t"+sh.toString());
}

System.exit(0);
}

public static void insertionSort(ArrayList<Shoes> shoe)


{
int i = 1 , j;
Shoes key = new Shoes( );
ArrayList<Shoes> inputArray = shoe;
for (j=1; j<inputArray.size(); j++)
{
key = inputArray.get(j);
i = j-1;

while (i>=0)
{
if (key.getBrand().compareTo(inputArray.get(i).getBrand()) > 0)
{
break;
}

Shoes element = inputArray.get(i+1);


inputArray.set(i+1, inputArray.get(i));
inputArray.set(i ,element);
i-- ;
}
}
}

static int binarySearch(String[] arr, String x)


{
int l = 0, r = arr.length - 1;

while (l <= r)
{
int m = l + (r - l) / 2;
int res = x.compareTo(arr[m]);

if (res == 0)
return m;

if (res > 0)
l = m + 1;

else
r = m - 1;
}

return -1;
}
}
public class Shoes
{
public String brand;
public int size;

public Shoes()
{
brand = null;
size = 0;
}

public Shoes(String b, int s)


{
brand = b;
size = s;
}

public String getBrand() { return brand; }


public int getSize() { return size; }

public void setBrand(String b) { brand = b; }


public void setSize(int s) { size = s; }

public String toString()


{
return("Brand : "+brand+
"\t\tSize : "+size);
}
}
SAMPLE OUTPUT

----------ENTER DATA----------
Enter brand : puma
Enter size (EUR) : 40

Enter brand : vans


Enter size (EUR) : 37

Enter brand : nike


Enter size (EUR) : 38

Enter brand : adidas


Enter size (EUR) : 41

Enter brand : reebok


Enter size (EUR) : 38

Enter brand : mizuno


Enter size (EUR) : 38

Enter brand : asics


Enter size (EUR) : 36

Enter brand : vincci


Enter size (EUR) : 36

Enter brand : skechers


Enter size (EUR) : 37

Enter brand : kelme


Enter size (EUR) : 39

----- UNSORTED list -----


puma
vans
nike
adidas
reebok
mizuno
asics
vincci
skechers
kelme

----- SORTED list -----


Brand : adidas Size : 41
Brand : asics Size : 36
Brand : kelme Size : 39
Brand : mizuno Size : 38
Brand : nike Size : 38
Brand : puma Size : 40
Brand : reebok Size : 38
Brand : skechers Size : 37
Brand : vans Size : 37
Brand : vincci Size : 36

Enter brand to search : skechers

Element found at index 7

----- NEW list -----


Brand : adidas Size : 41
Brand : asics Size : 36
Brand : kelme Size : 39
Brand : mizuno Size : 38
Brand : nike Size : 38
Brand : puma Size : 40
Brand : reebok Size : 38
Brand : vans Size : 37
Brand : vincci Size : 36

Enter new data at index 4


Enter brand : guess
Enter size :40

----- UPDATED list -----


index : 0 Brand : adidas Size : 41
index : 1 Brand : asics Size : 36
index : 2 Brand : kelme Size : 39
index : 3 Brand : mizuno Size : 38
index : 4 Brand : guess Size : 40
index : 5 Brand : nike Size : 38
index : 6 Brand : puma Size : 40
index : 7 Brand : reebok Size : 38
index : 8 Brand : vans Size : 37
index : 9 Brand : vincci Size : 36
CSC 248 – FUNDAMENTALS OF DATA STRUCTURE

ASSIGNMENT 1

GROUP MEMBERS : WAN AQILAH ZAHIRAH BT WAN AHMAD LUTFI [2017233068]

NUR NABIQHAH BT KHAIROL ANOUAR [2017254818]

GROUP : A4CS1104D

LECTURER : PN NORMAH AHMAD

You might also like