Notability Notes
Notability Notes
2021psc1082
QUES 1 To sort the elements of an array using :
A) BUBBLE SORT
class BubbleSort {
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;
}
}
void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
System.out.println();
}
public sta c void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6 };
printArray(arr);
}
}
C) SELECTION SORT
ob.sort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}
D) QUICK SORT
class QuickSort
{
int par on(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1);
for (int j=low; j<high; j++)
{
return i+1;
}
System.out.println("sorted array");
printArray(arr);
}
}
E) MERGE SORT
class MergeSort {
void merge(int arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int[n1];
int R[] = new int[n2];
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
}
else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
System.out.println("Given Array");
printArray(arr);
System.out.println("\nSorted array");
printArray(arr);
}
}
class LinearSearch {
sta c int search(int arr[], int n, int x)
{
for (int i = 0; i < n; i++) {
if (arr[i] == x)
return i;
}
return -1;
}
public sta c void main(String[] args)
{
int[] arr = { 3, 4, 1, 7, 5 };
int n = arr.length;
int x = 4;
if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index "
+ result);
}
}
System.out.println("\n1.Push\n2.Pop\n3.Show\n4.Exit");
System.out.println("\n Enter your choice \n");
choice = sc.nextInt();
switch(choice)
{
case 1:
{
s.push(sc);
break;
}
case 2:
{
s.pop();
break;
}
case 3:
{
s.display();
break;
}
case 4:
{
System.out.println("Exiting....");
System.exit(0);
break;
}
default:
{
System.out.println("Please Enter valid choice ");
}
};
}
}
}
B) LINKED LIST
obj.display();
System.out.prin ("\nTop element is %d\n",
obj.peek());
obj.pop();
obj.pop();
obj.display();
int data;
Node link;
}
Node top;
temp.data = x;
temp.link = top;
top = temp;
}
Queue(int c)
{
front = rear = 0;
capacity = c;
queue = new int[capacity];
}
else {
queue[rear] = data;
rear++;
}
return;
}
sta c void queueDequeue()
{
if (front == rear) {
System.out.prin ("\nQueue is empty\n");
return;
}
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
if (rear < capacity)
queue[rear] = 0;
rear--;
}
return;
}
sta c void queueDisplay()
{
int i;
if (front == rear) {
System.out.prin ("\nQueue is Empty\n");
return;
}
for (i = front; i < rear; i++) {
System.out.prin (" %d <-- ", queue[i]);
}
return;
}
sta c void queueFront()
{
if (front == rear) {
System.out.prin ("\nQueue is Empty\n");
return;
}
System.out.prin ("\nFront Element is: %d",
queue[front]);
return;
}
}
public class Sta cQueueinjava {
public sta c void main(String[] args)
{
Queue q = new Queue(4);
q.queueDisplay();
q.queueEnqueue(20);
q.queueEnqueue(30);
q.queueEnqueue(40);
q.queueEnqueue(50);
q.queueDisplay();
q.queueEnqueue(60);
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
System.out.prin (
"\n\na er two node dele on\n\n");
q.queueDisplay();
q.queueFront();
}
}
B) LINKED LIST
class QNode {
int key;
QNode next;
public QNode(int key)
{
this.key = key;
this.next = null;
}
}
class Queue {
QNode front, rear;
else
{
rear = (rear + 1);
else
{
queue.set(rear, data);
}
}
}
public int deQueue()
{
int temp;
if(front == -1)
{
System.out.print("Queue is Empty");
return -1;
}
temp = queue.get(front);
if(front == rear)
{
front = -1;
rear = -1;
}
q.enQueue(14);
q.enQueue(22);
q.enQueue(13);
q.enQueue(-6);
q.displayQueue();
int x = q.deQueue();
if(x != -1)
{
System.out.print("Deleted value = ");
System.out.println(x);
}
x = q.deQueue();
if(x != -1)
{
System.out.print("Deleted value = ");
System.out.println(x);
}
q.displayQueue();
q.enQueue(9);
q.enQueue(20);
q.enQueue(5);
q.displayQueue();
q.enQueue(20);
}
}
B) LINKED LIST
import java.io.*;
import java.u l.*;
q.rear = temp;
q.rear.link = q.front;
}
sta c int deQueue(Queue q)
{
if (q.front == null) {
System.out.prin ("Queue is empty");
return Integer.MIN_VALUE;
}
int value;
if (q.front == q.rear) {
value = q.front.data;
q.front = null;
q.rear = null;
}
else
{
Node temp = q.front;
value = temp.data;
q.front = q.front.link;
q.rear.link = q.front;
}
return value;
}
sta c void displayQueue(Queue q)
{
Node temp = q.front;
System.out.prin (
"\nElements in Circular Queue are: ");
while (temp.link != q.front) {
System.out.prin ("%d ", temp.data);
temp = temp.link;
}
System.out.prin ("%d", temp.data);
}
enQueue(q, 14);
enQueue(q, 22);
enQueue(q, 6);
displayQueue(q);
enQueue(q, 9);
enQueue(q, 20);
displayQueue(q);
}
}
QUES 7 Implement solu ons to the following problems using
recursion
1) Factorial
import java.io.*;
import java.u l.*;
class GFG {
sta c int factorial(int n)
{
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
public sta c void main(String[] args)
{
int ans = factorial(5);
System.out.println("Factorial of 5 is :" + ans);
}
}
2) Fibonacci
class GFG {
sta c int fib(int n)
{
if (n <= 1)
return n;