Professional Documents
Culture Documents
Assignment-1 ADS1 2021-22 - 1936
Assignment-1 ADS1 2021-22 - 1936
Assignment-1 ADS1 2021-22 - 1936
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
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;
0 1 2 3 4 5 6 7 8
98 83 76 61 50 37 42 22 10
Answer:
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;
}
}
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)
newNode.next = head;
head = newNode;
return head;
if(head==null)
{ head = newNode;
return; }
while(temp.next!=null)
temp = temp.next;
temp.next = newNode;}
while(node!=null)
node = node.next;
System.out.println();}
singly_linked.insertBeginning('A');
singly_linked.insertEnd('B');
singly_linked.insertEnd('C');
singly_linked.insertEnd('D');
singly_linked.display();
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;}}
newNode.next = head;
head = newNode;
return head;
if(head==null) {
head = newNode;
return head;
while(temp.next!=null)
temp = temp.next;
temp.next = newNode;
return head;}
node = node.next;}
System.out.println();}
head = insertBeginning(head,'A');
head = insertEnd(head,'B');
head = insertEnd(head,'C');
head = insertEnd(head,'D');
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).