Lab 04 DsA
Lab 04 DsA
LAB # 04
Linear Array Implementation
Object:
Implementing linear array and associated methods.
LAB TASKS
TASK#1:
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;
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
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:
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:
}
}
OUTPUT:
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 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);
}
}
OUTPUT:
OUTPUT: