You are on page 1of 7

LAB ASSIGNMENT 03

Name : Vaibhav Rajendra Bhagwat


Branch : IT
SEM : 03
DSY

1. Title of Experiment: Write a test application for your own long integer array class with the following new
operations :
initArray(): void -- initialize all elements with random value [java.util.Random]
bubbleSort()
selectionSort()
insertionSort()

2. Description of different Java constructs you used for the implementation.


Every class has a constructor that is used to initialize its objects. A custom parameterized constructor
‘public MyLongArray(int size)’ is Used for creating an instance of the MyLongArray class.
This constructor takes a single parameter, ‘size’ which indicates the desired size for the ‘data’ array. The ‘data’
array is then initialized with that size. Parameterized constructors are used to pass arguments while creating an
instance of the class, thereby initializing the object with some specific state. In this case, you can create a
MyLongArray object of any desired size using this constructor.

3. Class Diagram :

Fig. Class Diagram


4. Program Code :
import java.util.Random;

/**

* @author Vaibhav Bhagwat

* @version 1.0.1

* @since 27-09-2023

*/

/**

* Represents a long integer array with sorting capabilities.

*/

public class MyLongArray {

/**

* The internal long integer array.

*/

private long[] data;

/**

* A class-level constant used to generate random numbers.

*/

private static final Random RANDOM = new Random();

/**

* Constructor that initializes the internal array with the given size.

* @param size the size of the internal array

*/

public MyLongArray(int size) {

data = new long[size];

}
// Initialize the array with random values

public void initArray() {

for (int i = 0; i < data.length; i++) {

data[i] = RANDOM.nextLong();

/**

* Sorts the internal array using the Bubble Sort algorithm.

*/

public void bubbleSort() {

for (int i = 0; i < data.length - 1; i++) {

for (int j = 0; j < data.length - i - 1; j++) {

if (data[j] > data[j + 1]) {

// swap data[j] and data[j+1]

long temp = data[j];

data[j] = data[j + 1];

data[j + 1] = temp;

/**

* Sorts the internal array using the Selection Sort algorithm.

*/

public void selectionSort() {

for (int i = 0; i < data.length - 1; i++) {

int min_idx = i;

for (int j = i + 1; j < data.length; j++) {

if (data[j] < data[min_idx]) {

min_idx = j;

// Swap the found minimum with the first element


long temp = data[min_idx];

data[min_idx] = data[i];

data[i] = temp;

/**

* Sorts the internal array using the Insertion Sort algorithm.

*/

public void insertionSort() {

for (int i = 1; i < data.length; i++) {

long key = data[i];

int j = i - 1;

// Move elements greater than key one position ahead

while (j >= 0 && data[j] > key) {

data[j + 1] = data[j];

j = j - 1;

data[j + 1] = key;

// Method to print the contents of the array

public void printArray() {

for (long val : data) {

System.out.print(val + " ");

System.out.println();

/**

* Main method to demonstrate the usage of the LongArray class.

* @param args command-line arguments (unused)

*/
public static void main(String[] args) {

MyLongArray array = new MyLongArray(10);

array.initArray();

System.out.println("---------------------------------------------------------------
---------------------------------");

System.out.println("Original Array:");

array.printArray();

System.out.println("---------------------------------------------------------------
---------------------------------");

array.bubbleSort();

System.out.println("Bubble Sorted Array:");

array.printArray();

System.out.println("---------------------------------------------------------------
---------------------------------");

array.initArray();

System.out.println("Re-initialized Array:");

array.printArray();

System.out.println("---------------------------------------------------------------
---------------------------------");

array.selectionSort();

System.out.println("Selection Sorted Array:");

array.printArray();

System.out.println("---------------------------------------------------------------
---------------------------------");

array.initArray();

System.out.println("Re-initialized Array:");

array.printArray();

System.out.println("---------------------------------------------------------------
---------------------------------");

array.insertionSort();

System.out.println("Insertion Sorted Array:");

array.printArray();

System.out.println("---------------------------------------------------------------
---------------------------------");
}

 Output :

5. Type of errors you encountered and the way you fixed it.

1. Array Size Limitations :


Error Scenario: The JVM imposes limits on the maximum size of arrays, which is typically
‘Integer.MAX_VALUE – 5’. If you try to initialize the ‘MyLongArray’ class with a size
value approaching this upper limit (or even larger), you might encounter an
‘OutOfMemoryError’.
Fix: Either have size constraints or handle the potential error gracefully.

6. Learning lessons from the experiment :


1. Importance of Documentation:
The program begins with clear comments and JavaDoc-style documentation. This emphasizes
the significance of well-documented code, making it more maintainable and understandable
for other developers.
2. Random Initialization:
The program uses the ‘java.util.Random’ class to initialize the ‘data’ array with random long
values.
A constant ‘RANDOM’ of type ‘Random’ is declared as ‘static final’, which means it
remains the same for all instances of the class and can't be modified.
3. Sorting:
1. Bubble Sort:
 This is one of the simplest sorting algorithms.
 It repeatedly steps through the list, compares adjacent elements, and swaps
them if they are in the wrong order.
 The process is repeated for every element.
 Over successive passes, larger elements "bubble up" to their correct position.
2. Selection Sort:
 The algorithm divides the list into a sorted and an unsorted section.
 It repeatedly selects the smallest (or largest) element from the unsorted section
and moves it to the end of the sorted section.
 This process continues until the entire list is sorted.
3. Insertion Sort:
 This algorithm builds the final sorted array one item at a time.
 It keeps a "current" element and repeatedly compares it with the previous
elements.
 If the current element is smaller than the previous one, it moves the previous
one to the right, until the correct position for the current element is found.

7. Conclusion:
In summary, this experiment provides hands-on knowledge of the basic sorting algorithms in Java,
emphasizes the importance of encapsulation in object-oriented programming, and demonstrates data
initialization and manipulation techniques.

You might also like