[go: up one dir, main page]

0% found this document useful (0 votes)
6 views40 pages

Notability Notes

Notes

Uploaded by

Mayank Saharan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views40 pages

Notability Notes

Notes

Uploaded by

Mayank Saharan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Mayank CS PRACTICAL

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();
}

public sta c void main(String args[])


{
BubbleSort ob = new BubbleSort();
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
ob.bubbleSort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}
B) Inser on sort

class Inser onSort {


void sort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
sta c void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");

System.out.println();
}
public sta c void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6 };

Inser onSort ob = new Inser onSort();


ob.sort(arr);

printArray(arr);
}
}

C) SELECTION SORT

class Selec onSort {


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

int temp = arr[min_idx];


arr[min_idx] = arr[i];
arr[i] = temp;
}
}
void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public sta c void main(String args[])
{
Selec onSort ob = new Selec onSort();
int arr[] = { 64, 25, 12, 22, 11 };

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++)
{

if (arr[j] <= pivot)


{
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;

return i+1;
}

/* The main func on that implements QuickSort()


arr[] --> Array to be sorted,
low --> Star ng index,
high --> Ending index */
void sort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is par oning index, arr[pi] is
now at right place */
int pi = par on(arr, low, high);
sort(arr, low, pi-1);
sort(arr, pi+1, high);
}
}

/* A u lity func on to print array of size n */


sta c void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
public sta c void main(String args[])
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = arr.length;

QuickSort ob = new QuickSort();


ob.sort(arr, 0, n-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++;
}
}

void sort(int arr[], int l, int r)


{
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
sta c void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}
public sta c void main(String args[])
{
int arr[] = { 12, 11, 13, 5, 6, 7 };

System.out.println("Given Array");
printArray(arr);

MergeSort ob = new MergeSort();


ob.sort(arr, 0, arr.length - 1);

System.out.println("\nSorted array");
printArray(arr);
}
}

QUES 2 To search an element using linear search in an array

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;

int index = search(arr, n, x);


if (index == -1)
System.out.println("Element is not present in the array");
else
System.out.println("Element found at posi on " + index);
}
}

QUES 3 To search an element using BINARY SEARCH in a sorted array


class BinarySearch {
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r) {
int mid = (l + r) / 2;
if (arr[mid] == x) {
return mid;
} else if (arr[mid] > x) {
r = mid - 1;
} else {
l = mid + 1;
}
}
return -1;
}
public sta c void main(String args[])
{
BinarySearch ob = new BinarySearch();

int arr[] = { 2, 3, 4, 10, 40 };


int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, 0, n - 1, x);

if (result == -1)
System.out.println("Element not present");
else
System.out.println("Element found at index "
+ result);
}
}

QUES 4 Write a menu driven program to implement Stack Data


Structure using :
A) ARRAY
import java.util.Scanner;
class Stack
{
int top;
int maxsize = 10;
int[] arr = new int[maxsize];
boolean isEmpty()
{
return (top < 0);
}
Stack()
{
top = -1;
}
boolean push (Scanner sc)
{
if(top == maxsize-1)
{
System.out.println("Overflow !!");
return false;
}
else
{
System.out.println("Enter Value");
int val = sc.nextInt();
top++;
arr[top]=val;
System.out.println("Item pushed");
return true;
}
}
boolean pop ()
{
if (top == -1)
{
System.out.println("Underflow !!");
return false;
}
else
{
top --;
System.out.println("Item popped");
return true;
}
}
void display ()
{
System.out.println("Printing stack elements .....");
for(int i = top; i>=0;i--)
{
System.out.println(arr[i]);
}
}
}
public class Stack_Operations {
public static void main(String[] args) {
int choice=0;
Scanner sc = new Scanner(System.in);
Stack s = new Stack();
System.out.println("*********Stack operations using array********
*\n");
System.out.println("\n-----------------------------------------------
-\n");
while(choice != 4)
{
System.out.println("\nChose one from the below options...\n");

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

import sta c java.lang.System.exit;


class GFG {
public sta c void main(String[] args)
{
StackUsingLinkedlist obj
= new StackUsingLinkedlist();
obj.push(11);
obj.push(22);
obj.push(33);
obj.push(44);

obj.display();
System.out.prin ("\nTop element is %d\n",
obj.peek());
obj.pop();
obj.pop();

obj.display();

System.out.prin ("\nTop element is %d\n",


obj.peek());
}
}
class StackUsingLinkedlist {
private class Node {

int data;
Node link;
}

Node top;

StackUsingLinkedlist() { this.top = null; }


public void push(int x)
{

Node temp = new Node();


if (temp == null) {
System.out.print("\nHeap Overflow");
return;
}

temp.data = x;

temp.link = top;
top = temp;
}

public boolean isEmpty() { return top == null; }


public int peek()
{
if (!isEmpty()) {
return top.data;
}
else {
System.out.println("Stack is empty");
return -1;
}
}

public void pop()


{
if (top == null) {
System.out.print("\nStack Underflow");
return;
}
top = (top).link;
}

public void display()


{
if (top == null) {
System.out.prin ("\nStack Underflow");
exit(1);
}
else {
Node temp = top;
while (temp != null) {
System.out.print(temp.data);
temp = temp.link;
if(temp != null)
System.out.print(" -> ");
}
}
}
}

QUES 5 Write a menu driven program to implement Queue data


structure using
A) ARRAY
class Queue {
sta c private int front, rear, capacity;
sta c private int queue[];

Queue(int c)
{
front = rear = 0;
capacity = c;
queue = new int[capacity];
}

sta c void queueEnqueue(int data)


{
if (capacity == rear) {
System.out.prin ("\nQueue is full\n");
return;
}

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;

public Queue() { this.front = this.rear = null; }


void enqueue(int key)
{
QNode temp = new QNode(key);
if (this.rear == null) {
this.front = this.rear = temp;
return;
}
this.rear.next = temp;
this.rear = temp;
}
void dequeue()
{
if (this.front == null)
return;
QNode temp = this.front;
this.front = this.front.next;
if (this.front == null)
this.rear = null;
}
}
public class Test {
public sta c void main(String[] args)
{
Queue q = new Queue();
q.enqueue(10);
q.enqueue(20);
q.dequeue();
q.dequeue();
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.dequeue();
System.out.println("Queue Front : " + ((q.front != null) ?
(q.front).key : -1));
System.out.println("Queue Rear : " + ((q.rear != null) ? (q.rear).key
: -1));
}
}

QUES 6 Write a menu driven program to implement Circular Queue


Data Structure using :
A) ARRAY
import java.u l.ArrayList;
class CircularQueue{
private int size, front, rear;
private ArrayList<Integer> queue = new ArrayList<Integer>();
CircularQueue(int size)
{
this.size = size;
this.front = this.rear = -1;
}
public void enQueue(int data)
{
if((front == 0 && rear == size - 1) ||
(rear == (front - 1) % (size - 1)))
{
System.out.print("Queue is Full");
}
else if(front == -1)
{
front = 0;
rear = 0;
queue.add(rear, data);
}

else if(rear == size - 1 && front != 0)


{
rear = 0;
queue.set(rear, data);
}

else
{
rear = (rear + 1);

if(front <= rear)


{
queue.add(rear, data);
}

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;
}

else if(front == size - 1)


{
front = 0;
}
else
{
front = front + 1;
}
return temp;
}
public void displayQueue()
{
if(front == -1)
{
System.out.print("Queue is Empty");
return;
}
System.out.print("Elements in the " +
"circular queue are: ");

if(rear >= front)


{

for(int i = front; i <= rear; i++)


{
System.out.print(queue.get(i));
System.out.print(" ");
}
System.out.println();
}
else
{
for(int i = front; i < size; i++)
{
System.out.print(queue.get(i));
System.out.print(" ");
}

for(int i = 0; i <= rear; i++)


{
System.out.print(queue.get(i));
System.out.print(" ");
}
System.out.println();
}
}

public sta c void main(String[] args)


{
CircularQueue q = new CircularQueue(5);

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.*;

public class Solu on {


sta c class Node {
int data;
Node link;
}
sta c class Queue {
Node front, rear;
}

sta c void enQueue(Queue q, int value)


{
Node temp = new Node();
temp.data = value;
if (q.front == null)
q.front = temp;
else
q.rear.link = temp;

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);
}

/* Driver of the program */


public sta c void main(String args[])
{
Queue q = new Queue();
q.front = q.rear = null;

enQueue(q, 14);
enQueue(q, 22);
enQueue(q, 6);
displayQueue(q);

System.out.prin ("\nDeleted value = %d",


deQueue(q));
System.out.prin ("\nDeleted value = %d",
deQueue(q));
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;

return fib(n - 1) + fib(n - 2);


}
public sta c void main(String args[])
{
int N = 10;
for (int i = 0; i < N; i++) {

System.out.print(fib(i) + " ");


}
}
}

You might also like