Lab Assignment 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

19ELC211 DATA STRUCTURES AND ALGORITHMS

LABSHEET 1
MALAVIKA SREEJITH
AM.EN.U4ELC22053
1. import java.util.Scanner;

public class MyREverse {


private int[] array;

public MyREverse(int[] array)


{
this.array = array;
}

public void reverse(int[] array)


{
int start_index = 0;
int end_index = array.length-1;

while(start_index<end_index)
{
int temp = array[start_index];
array[start_index] = array[end_index];
array[end_index] = temp;

start_index ++;
end_index --;

}
}
public void printArray()
{
for(int num : array)
{
System.out.print(num + " ");
}
System.out.println();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter size of array : ");
int size = scanner.nextInt();
int [] arr = new int [size];
for(int i = 0; i<size;i++)
{
System.out.println("Enter element to be entered at element "+i+" :
");
arr[i] = scanner.nextInt();
}
System.out.println("Original Array :");
MyREverse MyObj = new MyREverse(arr);
MyObj.printArray();
MyObj.reverse(arr);
System.out.println("Reversed array :");
MyObj.printArray();
scanner.close();

}
}

Output:

2. public class BubbleSort {

public static void bubbleSort(int[] arr) {


int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {

int temp = arr[j];


arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

public static void main(String[] args) {


int[] arr = {64, 34, 25, 12, 22, 11, 90};

System.out.println("Array before sorting: ");


for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();

bubbleSort(arr);

System.out.println("Array after sorting: ");


for (int num : arr) {
System.out.print(num + " ");
}
}
}

Output:

3.
public class Book {
private String title;
private String author;
private String publisher;
private int bookId;
private int edition;
private String ISBN;

public Book(String title, String author, String publisher, int bookId, int
edition, String ISBN) {
this.title = title;
this.author = author;
this.publisher = publisher;
this.bookId = bookId;
this.edition = edition;
this.ISBN = ISBN;
}

public String getTitle() {


return title;
}

public void setTitle(String title) {


this.title = title;
}

public String getAuthor() {


return author;
}

public void setAuthor(String author) {


this.author = author;
}

public String getPublisher() {


return publisher;
}

public void setPublisher(String publisher) {


this.publisher = publisher;
}

public int getBookId() {


return bookId;
}

public void setBookId(int bookId) {


this.bookId = bookId;
}

public int getEdition() {


return edition;
}

public void setEdition(int edition) {


this.edition = edition;
}

public String getISBN() {


return ISBN;
}
public void setISBN(String ISBN) {
this.ISBN = ISBN;
}

public void displayBookDetails() {


System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Publisher: " + publisher);
System.out.println("Book ID: " + bookId);
System.out.println("Edition: " + edition);
System.out.println("ISBN: " + ISBN);
}

public static void main(String[] args) {

Book book = new Book("A Feast for the crows", "George R R Martin",
"Fort Publications", 12345, 1, "978-3-16-148410-7");

book.displayBookDetails();

book.setEdition(2);
book.setPublisher("Swen Publications");

System.out.println("\nUpdated Book Details:");


book.displayBookDetails();
}
}

Output:
4.
import java.util.Scanner;
import java.util.Arrays;

public class ZigZag {


public static void rearrangeZigZag(int[] arr) {
int n = arr.length;
boolean flag = true;

for (int i = 0; i < n - 1; i++) {


if (flag) {
if (arr[i] > arr[i + 1]) {
swap(arr, i, i + 1);
}
} else {
if (arr[i] < arr[i + 1]) {
swap(arr, i, i + 1);
}
}
flag = !flag;
}
}

private static void swap(int[] arr, int i, int j) {


int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int size = scanner.nextInt();

int[] arr = new int[size];

System.out.println("Enter the elements of the array:");


for (int i = 0; i < size; i++) {
System.out.print("Enter element at index " + i + ": ");
arr[i] = scanner.nextInt();
}

rearrangeZigZag(arr);

System.out.println("Array in Zig-Zag Pattern: " +


Arrays.toString(arr));

scanner.close();
}
}

Output:

5.
import java.util.Scanner;
import java.util.Arrays;

public class Shift {


public static int[] leftRotate(int[] arr, int d) {
int n = arr.length;
int[] rotatedArray = new int[n];

int rotations = d % n;

for (int i = 0; i < n; i++) {


int newIndex = (i + n - rotations) % n;
rotatedArray[newIndex] = arr[i];
}

return rotatedArray;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the size of the array: ");


int size = scanner.nextInt();

int[] arr = new int[size];

System.out.println("Enter the elements of the array:");


for (int i = 0; i < size; i++) {
System.out.print("Enter element at index " + i + ": ");
arr[i] = scanner.nextInt();
}

System.out.print("Enter the number of left rotations: ");


int d = scanner.nextInt();

System.out.println("Original Array: " + Arrays.toString(arr));


System.out.println("Number of left rotations: " + d);

int[] rotatedArray = leftRotate(arr, d);

System.out.println("Array after left rotation: " +


Arrays.toString(rotatedArray));

scanner.close();
}
}

Output:

You might also like