0% found this document useful (0 votes)
53 views8 pages

Lab 04 DsA

This document describes a lab assignment on implementing linear arrays in Java. It includes 3 tasks: 1) Write a program to implement basic linear array operations like insertion, deletion and traversal. 2) Add a method to merge two arrays into one new array. 3) Add a method to split an existing array into two arrays at a given index. The document provides sample code solutions for each task and expected output. It also assigns a homework task to write a program to generate all permutations of a given array.

Uploaded by

Esha Fayyaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views8 pages

Lab 04 DsA

This document describes a lab assignment on implementing linear arrays in Java. It includes 3 tasks: 1) Write a program to implement basic linear array operations like insertion, deletion and traversal. 2) Add a method to merge two arrays into one new array. 3) Add a method to split an existing array into two arrays at a given index. The document provides sample code solutions for each task and expected output. It also assigns a homework task to write a program to generate all permutations of a given array.

Uploaded by

Esha Fayyaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SWE-203L: Data Structures and Algorithms SSUET/QR/114

LAB # 04
Linear Array Implementation
Object:
Implementing linear array and associated methods.

LAB TASKS
TASK#1:

1. Write a program to implement all basic operations in linear array (specified in


linear array class). Write a demo class to show the working of array.

SOURCE CODE:
/**
*
*/
public class Lab4 {
public static void main(String[] args) {

int n = 10;
int array[] = new int[n];
array[0] = 12;
array[1] = 27;
array[2] = 49;
array[3] = 54;
array[4] = 67;

int new_item =4;


int k = 2;
int j = n - 1;
System.out.println("Numbers After Inserting 4");

Name: Esha Muhammad Fayyaz


Roll No: 2020F-BSE-121 -- Section: C Page 1 | 8
SWE-203L: Data Structures and Algorithms SSUET/QR/114

while (j >= k) {
array[j] = array[j - 1];
j = j - 1;
}
array[k] = new_item;
for (int i = 0; i < n; i++) {
System.out.println("Index" + "(" + i + "):" + array[i]);
}

System.out.println();
System.out.println("Numbers after Traversing");
for (int i = 0; i < array.length; i++) {
System.out.println("Index" + "(" + i + "):" + array[i]);
}

System.out.println();
int del_item;
System.out.println("Numbers After Deleting 4");
del_item = array[k];
for (int i = k; i < n - 1; i++) {
array[i] = array[i + 1];
}
n = n - 1;
for (int i = 0; i < n; i++) {
System.out.println("Index" + "(" + i + "):" + array[i]);
}
}
}

OUTPUT:
Name: Esha Muhammad Fayyaz
Roll No: 2020F-BSE-121 -- Section: C Page 2 | 8
SWE-203L: Data Structures and Algorithms SSUET/QR/114

Name: Esha Muhammad Fayyaz


Roll No: 2020F-BSE-121 -- Section: C Page 3 | 8
SWE-203L: Data Structures and Algorithms SSUET/QR/114

TASK#2:

2. Add a method in the class that takes array and merge it with the existing one.

SOURCE CODE:
public static int[] merge(int[] arr,int[] arr2){
int[] arr3 = new int[arr.length + arr2.length];
for(int i = 0; i < arr.length; i++) {
arr3[i] = arr[i];
count++;
}
for(int j = 0; j < arr2.length;j++) {
arr3[count++] = arr2[j];
}
return arr3;
}
public static void main(String[] args) {
int[] arr = {2,4,6,8,10};
int[] arr2 = {1,3,5,7,9};
int[] result = merge(arr,arr2);
System.out.println(Arrays.toString(result));

OUTPUT:

Name: Esha Muhammad Fayyaz


Roll No: 2020F-BSE-121 -- Section: C Page 4 | 8
SWE-203L: Data Structures and Algorithms SSUET/QR/114

TASK#3:

3. Add a method in the same class that splits the existing array into two. The method should
search a key in array and if found splits the array from that index of the key

SOURCE CODE:

public class Lab4 {

public static int[][] split(int[] arr, int index) {


int[] arr1 = new int[index + 1];
int[] arr2 = new int[arr.length -index -1];
for (int i = 0; i < arr.length; i++) {
if(i < arr1.length) { arr1[i] = arr[i];}
else { arr2[i-arr1.length] = arr[i];}
}
int intArr[][] = new int[2][];
intArr[0] = arr1;
intArr[1] = arr2;
return intArr;
}

public static void main(String[] args) {


int[] arr =
{112,112,153,184,145,2454,8004,7834,159,764,223,890,980,425,784,9034
,196,170,118,1234,9029};
int index = 10 ;
int[][] dArr = split(arr, index);
System.out.println("ARRAY BEFORE SPLITTING : : " +
Arrays.toString(arr));
System.out.println("AFTER SPLITTING AT INDEX : " + index);
System.out.println(Arrays.toString(dArr[0]));
System.out.println(Arrays.toString(dArr[1]));

}
}

Name: Esha Muhammad Fayyaz


Roll No: 2020F-BSE-121 -- Section: C Page 5 | 8
SWE-203L: Data Structures and Algorithms SSUET/QR/114

OUTPUT:

Name: Esha Muhammad Fayyaz


Roll No: 2020F-BSE-121 -- Section: C Page 6 | 8
SWE-203L: Data Structures and Algorithms SSUET/QR/114

HOME TASKS
TASK#1:
1. Write a Java program to create all possible permutations of a given array of distinct
integers.

SOURCE CODE:
**
*
*/
public class Lab4 {

public static void permute(int[] arr,int k){


for(int i = k; i < arr.length; i++){
int temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
permute(arr, k+1);
temp = arr[k];
arr[k] = arr[i];
arr[i] = temp;
}
if (k == arr.length -1){
System.out.println(Arrays.toString(arr));
}

}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] arr1 = new int[3];
System.out.println("\tArray 1\n");
for (int i=0; i<arr1.length; i++){
System.out.print("Enter Number:");
arr1[i] = input.nextInt();
}
int[] arr2 = new int[4];
System.out.println("\n\tArray 2\n");
for (int i=0; i<arr2.length; i++){
System.out.print("Enter Number:");
arr2[i] = input.nextInt();
}
System.out.println("\nPermutation of Array 1:");
permute(arr1,0);
System.out.println("\nPermutation of Array 2:");
permute(arr2,0);
}

Name: Esha Muhammad Fayyaz


Roll No: 2020F-BSE-121 -- Section: C Page 7 | 8
SWE-203L: Data Structures and Algorithms SSUET/QR/114

}
OUTPUT:

OUTPUT:

Name: Esha Muhammad Fayyaz


Roll No: 2020F-BSE-121 -- Section: C Page 8 | 8

You might also like