Assignment-1 ADS1 2021-22 - 1936

You might also like

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

KINGDOM OF SAUDI ARABIA | JAZAN UNIVERSITY

COLLEGE OF COMPUTER SCIENCE & INFORMATION TECHNOLOGY


ASSIGNMENT I - 2022-2023
Academic Year 2022-23 Semester Second
Course with code Algorithms & Data Structure – I (COMP321) Section 1936
Type of Assignment Individual Marks 10
Date of Announcement 25-DEC-2022 Deadline 1-JAN-2023
ASSIGNMENT PROBLEM STATEMENT

1. Explain various operations that can be performed on Circular Linked Lists. (CLO 1.1)

Answer:
In a circular linked list, we perform the following operations...

1. Insertion
2. Deletion
3. Display

Insertion on a Circular Linked List

We can insert elements at 3 different positions of a circular linked list:

1. Insertion at the beginning


2. Insertion in-between nodes
3. Insertion at the end

Deletion on a Circular Linked List


In a circular linked list, the deletion operation can be performed in three ways those are as
follows...

1. Deleting from Beginning of the list


2. Deleting from End of the list
3. Deleting a Specific Node

2. Write an algorithm to display prime numbers from 1 to 150. What is the order of growth
of the algorithm? (CLO 2.1)
Answer:
public class Main {
static void prime_N(int N)
{
int x, y, flg;
System.out.println(" the Prime numbers From 1 to " +N+ " are:");
for (x = 1; x <= N; x++) {
flg = 1;

for (y = 2; y * y <= x; y++) {


if (x % y == 0) {
flg = 0;
break;
}}
if (flg == 1)
System.out.print(x + " ");}}
public static void main(String[] args)
{
int N = 150;
prime_N(N);
}
}
3. Write the steps taken by the Selection Sort algorithm to sort the data given below in
ascending order: (CLO 2.2)

0 1 2 3 4 5 6 7 8

98 83 76 61 50 37 42 22 10

Answer:

1. Make the initial component the bare minimum.


2. Evaluate the minimum against the second element. If the second
element is smaller than the minimum, it should be designated as
the minimum.
3. Compare the minimal value to the third component. Once more, if
the third element is smaller, set a minimum value for it; otherwise,
do nothing. The procedure continues till the final component.
4. The ninth item on the unsorted list is where minimum is positioned
after each iteration.
5. Indexing begins with the first unsorted element for each iteration.
Up until all the components are in their proper places, steps 1
through 3 are repeated.

import java.util.Arrays;
public class Main {
static void selection_Sort(int array_Sort[]) {
int size = array_Sort.length;
for (int step = 0; step < size - 1; step++) {
int min_idx = step;
for (int i = step + 1; i < size; i++) {
if (array_Sort[i] < array_Sort[min_idx]) {
min_idx = i;
}
}
int temp = array_Sort[step];
array_Sort[step] = array_Sort[min_idx];
array_Sort[min_idx] = temp;
}
}

public static void main(String args[]) {


int[] data_Sort = { 0, 1, 2, 3, 4 , 5, 6, 7, 8, 98, 83, 76, 61, 50, 37, 42, 22, 10};
selection_Sort(data_Sort);
System.out.println("Sorted Array in Ascending Order: ");
System.out.println(Arrays.toString(data_Sort));
}
}

4. Write steps to perform the following operations on the singly linked list given below:
i. Insert G at the beginning of the list.
ii. Insert S at the end of the list. (CLO 2.2)

Answer 1:
import java.lang.*;

class LinkedList {

Node head;

class Node{

char data;

Node next;

Node(char x)

data = x;

next = null;}}
public Node insertBeginning(char data)

Node newNode = new Node(data);

newNode.next = head;

head = newNode;

return head;

public void insertEnd(char data)

Node newNode = new Node(data);

if(head==null)

{ head = newNode;

return; }

Node temp = head;

while(temp.next!=null)

temp = temp.next;

temp.next = newNode;}

public void display()

Node node = head;

while(node!=null)

System.out.print(node.data + " ");

node = node.next;

System.out.println();}

public class Main{


public static void main(String args[])

LinkedList singly_linked = new LinkedList();

System.out.println("singly_linked befor insert G,S");

singly_linked.insertBeginning('A');

singly_linked.insertEnd('B');

singly_linked.insertEnd('C');

singly_linked.insertEnd('D');

singly_linked.display();

System.out.println("singly_linked After insert G,S");

singly_linked.insertBeginning('G');

singly_linked.insertEnd('S');

singly_linked.display();}}

Answer 2:
import java.lang.*;

class Node {

char data;

Node next;

Node(char x)

data = x;

next = null;}}

public class Main

static Node insertBeginning(Node head, char data)

Node newNode = new Node(data);

newNode.next = head;
head = newNode;

return head;

static Node insertEnd(Node head, char data) {

Node newNode = new Node(data);

if(head==null) {

head = newNode;

return head;

Node temp = head;

while(temp.next!=null)

temp = temp.next;

temp.next = newNode;

return head;}

static void display(Node node) {

while (node != null) {

System.out.print(node.data + " ");

node = node.next;}

System.out.println();}

public static void main(String args[])

Node head = null;

head = insertBeginning(head,'A');

head = insertEnd(head,'B');

head = insertEnd(head,'C');

head = insertEnd(head,'D');

System.out.println("singly_linked befor insert G,S");

display(head);
System.out.println("singly_linked After insert G,S");

head = insertBeginning(head,'G');

head = insertEnd(head,'S');

display(head);}}

5. Compute the total computing time T (n) of the following block of statements. (CLO 2.1)

• Initialize i to 1
• Initialize n to 20
• while(i<=n)
• print (2 * i)
• Increment i by 1
• End While

Answer:
i=1
n=20
while (i<=n)
{
display (2*i)
i++
}
Here the T(n) = O(n) as the while loop is using the i variable and it is going from i = 1 to i = n

Other statements will take O (1) whether it is initializing the variable or printing the statements.
So, overall time complexity is O(n)
Hence the time complexity is O(n).

Name of the Course teacher Ms. Roma Fayaz Signature

You might also like