Lab 01 Data Structures
Lab 01 Data Structures
Lab 01 Data Structures
Practice 01
Perform following task on an array,
a) Input size of an array
b) Define and create array with the input size on random value
c) Display the defined array
d) Implement insert, delete and search operation (Binary)
import java.util.Random;
import java.util.Arrays;
import java.util.Scanner;
class MyArray{
// Function to remove the element
public static int[] removeTheElement(int[] arr, int index)
{
return arr;
}
int i=0;
//start form the index till end of array
for (i = index; i< arr.length-1; i++)
arr[i]=arr[i+1];
}
Practice 02
Write a function in Java (or any language) to read a list of 10 integer numbers and arrange
them in ascending/descending order.
Whenever we do hear sorting algorithms come into play such as selection sort, bubble sort,
insertion sort, radix sort, bucket sort, etc. but if we look closer here we are not asked to use any
kind of algorithms. It is as simple sorting with the help of linear and non-linear data structures
present within java. So there is sorting done with the help of brute force in java with the help of
loops and there are two in-built methods to sort in Java.
(See page 104, Chapter 3, M.T. Goodrich)
//Sorting on a subarray
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
// Java program to sort a subarray using Arrays.sort()
int[] arr = { 13, 7, 6, 45, 21, 9, 2, 100 }; // Custom input
array
// Sort subarray from index 1 to 4, i.e., only sort subarray {7, 6,
45, 21} and
//keep other elements as it is.
Arrays.sort(arr, 1, 5);
Practice 03
Write a function in Java (or any language) to generate an array of 10 integer numbers using
random function and sort them in ascending/descending order.
Java has a built-in class, java.util.Random, whose instances are pseudorandom number
generators, that is, objects that compute a sequence of numbers that are statistically random.
These sequences are not actually random, however, in that it is possible to predict the next
number in the sequence given the past list of numbers.
(See page 113, Chapter 3, M.T. Goodrich)
import java.util.Arrays;
import java.util.Random;
public class Main {
public static void main(String[] args) {
int data[ ] = new int[10];
Random rand = new Random(); // a pseudo-random number generator
// use current time as a seed
rand.setSeed(System.currentTimeMillis());
Practice 04
Given an integer n. We have n*n values of a 2-d array, and n values of 1-d array. Task is to
find the sum of the left diagonal values of the 2-d array and the max element of the 1-d array
and print them with space in between.
int n = 3;
int[][] array2D = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int diagonalSum = 0;
diagonalSum += array2D[i][i];
maxElement = array1D[i];
}
}
Exercise
1. Write a function in Java (or any language) to read a list of 10 integer numbers and arrange
them in such a manner that all the even numbers start from the left and all the odd numbers start
from the right.
Input: 1 2 3 5 7 2 2 7 8 9
Output: 1 3 5 7 7 9 2 2 2 8
2. Write a function named noDup() that takes a 2D array of size 4x5 and a 1D array of size 20. It
should then copy all the elements of 2D array into 1D array but should avoid duplication.
3. Create a file named NLArray.java and design following functions or performing NLP.
- String [] wordTokenize (String fileName) Read any text file and return list of words
from that file. (Ignore . , : and all these types operators)
- String[] extractEmail (String fileName) Read any text file and return all emails from
and file
Note: Read about Natural Language Processing (NLP), Word Tokenizing, Stop Words,
Information Extraction/Retrieval for Knowledge.
4. Design following methods in above same class NLArray.java for Image Cropping.
- void extractBoundaries (int arr[][]) This function should extract boundaries and
print from arr (Boundaries include 1st row, 1st col, last row, last col).
- void cropCenterPart (int arr[][]) This function should extract center part and print
from arr, center part includes everything except Boundaries (Boundaries include 1st row,
1st col, last row, last col).