DSA Part-1
DSA Part-1
=============================
*DAY 1*
-------------
*ARRAYS*
------------------------
1. *INSERTING AN ELEMENT IN AN ARRAY TRYOUT*
---------
class ArrayTest {
class Tester{
}
-------------------
2. *DELETING AN ELEMENT IN AN ARRAY- TRYOUT*
class ArrayTest {
public static void delete(char[] ar, int pos){
//Traversing the array from the position where the element has to be deleted to the
end
for(int i=pos-1;i<ar.length-1;i++){
//Moving each element one position to the left
ar[i]=ar[i+1];
}
//The space that is left at the end is filled with character '0'
ar[ar.length-1]='0';
}
}
class Tester{
public static void main(String args[]){
char arr[]=new char[6];
arr[0]='A';
arr[1]='B';
arr[2]='J';
arr[3]='C';
arr[4]='D';
arr[5]='E';
}
---------------------
*LINKED LIST*
-----------------------
1. *ADDING AN ELEMENT TO A LINKED LIST(AT END)*/CREATE LIST
class Node {
private String data;
private Node next;
class LinkedList {
}
}
class Tester{
public static void main(String args[]){
LinkedList list = new LinkedList();
list.addAtEnd("Milan");
list.addAtEnd("Venice");
list.addAtEnd("Munich");
list.addAtEnd("Vienna");
System.out.println("Adding an element to the linked list");
}
}
-----------------------
2. *ADDING AN ELEMENT AT THE BEGINNING-TRYOUT*
class Node {
private String data;
private Node next;
class LinkedList {
class Tester{
class LinkedList {
class LinkedList {
if (list.find("Munich") != null)
System.out.println("Node found");
else
System.out.println("Node not found");
}
}
-----------------
5. *INSERTING AN ELEMENT IN A LINKED LIST*
class Node {
private String data;
private Node next;
class LinkedList {
}
}
----------------
6. *DELETING AN ELEMENT FROM LINKED LIST*
class Node {
private String data;
private Node next;
class LinkedList {
/*
* if(list.find("Munich")!=null) System.out.println("Node found"); else
* System.out.println("Node not found");
*/
}
}
--------------
*LINKED LIST EXERCISE 1*
class Node {
class LinkedList {
if (this.head == null) {
this.head = this.tail = node;
} else {
this.tail.setNext(node);
this.tail = node;
}
}
if (this.head == null) {
this.head = this.tail = node;
}
else {
node.setNext(this.head);
this.head = node;
}
}
if (this.head == null)
this.head = this.tail = node;
else {
Node nodeBefore = this.find(dataBefore);
if (nodeBefore != null) {
node.setNext(nodeBefore.getNext());
nodeBefore.setNext(node);
if (nodeBefore == this.tail)
this.tail = node;
} else
System.out.println("Node not found");
}
}
if (this.head == null)
System.out.println("List is empty");
else {
Node node = this.find(data);
if (node == null)
System.out.println("Node not found");
if (node == this.head) {
this.head = this.head.getNext();
node.setNext(null);
if (node == this.tail)
tail = null;
}
else {
Node nodeBefore = null;
Node temp = this.head;
while (temp != null) {
if (temp.getNext() == node) {
nodeBefore = temp;
break;
}
temp = temp.getNext();
}
nodeBefore.setNext(node.getNext());
if (node == this.tail)
this.tail = nodeBefore;
node.setNext(null);
}
}
}
}
class Tester {
temp=temp.getNext();
}
return 0;
}
}
--------------
*LINKED LIST ASSIGNMENTS*
-----
1. class Node {
class LinkedList {
if (this.head == null) {
this.head = this.tail = node;
} else {
this.tail.setNext(node);
this.tail = node;
}
}
if (this.head == null) {
this.head = this.tail = node;
}
else {
node.setNext(this.head);
this.head = node;
}
}
if (this.head == null)
this.head = this.tail = node;
else {
Node nodeBefore = this.find(dataBefore);
if (nodeBefore != null) {
node.setNext(nodeBefore.getNext());
nodeBefore.setNext(node);
if (nodeBefore == this.tail)
this.tail = node;
}
else
System.out.println("Node not found");
}
}
if (this.head == null)
System.out.println("List is empty");
else {
Node node = this.find(data);
if (node == null)
System.out.println("Node not found");
if (node == this.head) {
this.head = this.head.getNext();
node.setNext(null);
if (node == this.tail)
tail = null;
}
else {
Node nodeBefore = null;
Node temp = this.head;
while (temp != null) {
if (temp.getNext() == node) {
nodeBefore = temp;
break;
}
temp = temp.getNext();
}
nodeBefore.setNext(node.getNext());
if (node == this.tail)
this.tail = nodeBefore;
node.setNext(null);
}
}
}
}
class Tester {
System.out.println("Initial List");
linkedList1.display();
System.out.println("\nInitial List");
linkedList2.display();
}
}
------------------------
2. class Node {
class LinkedList {
if (this.head == null) {
this.head = this.tail = node;
} else {
this.tail.setNext(node);
this.tail = node;
}
}
if (this.head == null) {
this.head = this.tail = node;
}
else {
node.setNext(this.head);
this.head = node;
}
}
if (this.head == null)
this.head = this.tail = node;
else {
Node nodeBefore = this.find(dataBefore);
if (nodeBefore != null) {
node.setNext(nodeBefore.getNext());
nodeBefore.setNext(node);
if (nodeBefore == this.tail)
this.tail = node;
}
else
System.out.println("Node not found");
}
}
if (this.head == null)
System.out.println("List is empty");
else {
Node node = this.find(data);
if (node == null)
System.out.println("Node not found");
if (node == this.head) {
this.head = this.head.getNext();
node.setNext(null);
if (node == this.tail)
tail = null;
}
else {
Node nodeBefore = null;
Node temp = this.head;
while (temp != null) {
if (temp.getNext() == node) {
nodeBefore = temp;
break;
}
temp = temp.getNext();
}
nodeBefore.setNext(node.getNext());
if (node == this.tail)
this.tail = nodeBefore;
node.setNext(null);
}
}
}
}
class Tester {
linkedList.addAtEnd("Data");
linkedList.addAtEnd("Structures");
linkedList.addAtEnd("and");
linkedList.addAtEnd("Algorithms");
System.out.println("Initial List");
linkedList.display();
System.out.println();
reverseList(linkedList.getHead(), reversedLinkedList);
System.out.println("Reversed List");
reversedLinkedList.display();
}
------------------------
=====================
*DAY 2*
----------------
*PUSH OPERATION- TRYOUT*
class Stack {
private int top; // represents the index position of the top most element in the stack
private int maxSize; // represents the maximum number of elements that can be stored in
the stack
private int[] arr;
Stack(int maxSize) {
this.top = -1; // top is -1 when the stack is created
this.maxSize = maxSize;
arr = new int[maxSize];
}
class Tester {
public static void main(String args[]) {
Stack stack = new Stack(5);
System.out.println("Stack created.\n");
if (stack.push(1))
System.out.println("The element is pushed to the stack!\n");
else
System.out.println("Stack is full!\n");
if (stack.push(2))
System.out.println("The element is pushed to the stack!\n");
else
System.out.println("Stack is full!\n");
if (stack.push(3))
System.out.println("The element is pushed to the stack!\n");
else
System.out.println("Stack is full!\n");
if (stack.push(4))
System.out.println("The element is pushed to the stack!\n");
else
System.out.println("Stack is full!\n");
if (stack.push(5))
System.out.println("The element is pushed to the stack!\n");
else
System.out.println("Stack is full!\n");
stack.display();
if (stack.push(6))
System.out.println("The element is pushed to the stack!\n");
else
System.out.println("Stack is full!\n");
System.out.println("The top element is : " + stack.peek());
}
}
--------------------------------
*STACK COMPLETE TRYOUT*
class Stack {
private int top; // represents the index position of the top most element in the stack
private int maxSize; // represents the maximum number of elements that can be stored in
the stack
private int[] arr;
Stack(int maxSize) {
this.top = -1; // top is -1 when the stack is created
this.maxSize = maxSize;
arr = new int[maxSize];
}
class Tester {
public static void main(String args[]) {
Stack stack = new Stack(5);
System.out.println("Stack created.\n");
if (stack.push(1))
System.out.println("The element is pushed to the stack!\n");
else
System.out.println("Stack is full!\n");
if (stack.push(2))
System.out.println("The element is pushed to the stack!\n");
else
System.out.println("Stack is full!\n");
if (stack.push(3))
System.out.println("The element is pushed to the stack!\n");
else
System.out.println("Stack is full!\n");
if (stack.push(4))
System.out.println("The element is pushed to the stack!\n");
else
System.out.println("Stack is full!\n");
if (stack.push(5))
System.out.println("The element is pushed to the stack!\n");
else
System.out.println("Stack is full!\n");
stack.display();
if (stack.push(6))
System.out.println("The element is pushed to the stack!\n");
else
System.out.println("Stack is full!\n");
poppedElement = stack.pop();
if (poppedElement == Integer.MIN_VALUE)
System.out.println("Stack is empty\n");
else
System.out.println("The element popped out is : " + poppedElement +
"\n");
poppedElement = stack.pop();
if (poppedElement == Integer.MIN_VALUE)
System.out.println("Stack is empty\n");
else
System.out.println("The element popped out is : " + poppedElement +
"\n");
poppedElement = stack.pop();
if (poppedElement == Integer.MIN_VALUE)
System.out.println("Stack is empty\n");
else
System.out.println("The element popped out is : " + poppedElement +
"\n");
poppedElement = stack.pop();
if (poppedElement == Integer.MIN_VALUE)
System.out.println("Stack is empty\n");
else
System.out.println("The element popped out is : " + poppedElement +
"\n");
poppedElement = stack.pop();
if (poppedElement == Integer.MIN_VALUE)
System.out.println("Stack is empty\n");
else
System.out.println("The element popped out is : " + poppedElement +
"\n");
}
}
-------------------------
*ENQUEUE OPERATION-TRYOUT*
class Queue {
private int front; // front represents the index position of the first element in the queue
private int rear; // rear represents the index position of the last element in the queue
private int maxSize; // maxSize represent the maximum number of elements that can be
stored in the queue
private String arr[];
Queue(int maxSize) {
this.front = 0; // front is 0 when the queue is created
this.rear = -1; // rear is -1 when the queue is created
this.maxSize = maxSize;
this.arr = new String[maxSize];
}
class Tester {
if (queue.enqueue("Joe"))
System.out.println("The element is enqueued to the queue!\n");
else
System.out.println("Queue is full!\n");
if (queue.enqueue("Jack"))
System.out.println("The element is enqueued to the queue!\n");
else
System.out.println("Queue is full!\n");
if (queue.enqueue("Eva"))
System.out.println("The element is enqueued to the queue!\n");
else
System.out.println("Queue is full!\n");
if (queue.enqueue("Mia"))
System.out.println("The element is enqueued to the queue!\n");
else
System.out.println("Queue is full!\n");
if (queue.enqueue("Luke"))
System.out.println("The element is enqueued to the queue!\n");
else
System.out.println("Queue is full!\n");
queue.display();
if (queue.enqueue("Emma"))
System.out.println("The element is enqueued to the queue!\n");
else
System.out.println("Queue is full!\n");
}
}
-------------------------
*QUEUE COMPLETE- TRYOUT*
class Queue {
private int front; // front represents the index position of the first element in the queue
private int rear; // rear represents the index position of the last element in the queue
private int maxSize; // maxSize represent the maximum number of elements that can be
stored in the queue
private String arr[];
Queue(int maxSize) {
this.front = 0; // front is 0 when the queue is created
this.rear = -1; // rear is -1 when the queue is created
this.maxSize = maxSize;
this.arr = new String[maxSize];
}
return data;
}
}
}
class Tester {
if (queue.enqueue("Joe"))
System.out.println("The element is enqueued to the queue!\n");
else
System.out.println("Queue is full!\n");
if (queue.enqueue("Jack"))
System.out.println("The element is enqueued to the queue!\n");
else
System.out.println("Queue is full!\n");
if (queue.enqueue("Eva"))
System.out.println("The element is enqueued to the queue!\n");
else
System.out.println("Queue is full!\n");
if (queue.enqueue("Mia"))
System.out.println("The element is enqueued to the queue!\n");
else
System.out.println("Queue is full!\n");
if (queue.enqueue("Luke"))
System.out.println("The element is enqueued to the queue!\n");
else
System.out.println("Queue is full!\n");
queue.display();
if (queue.enqueue("Emma"))
System.out.println("The element is enqueued to the queue!\n");
else
System.out.println("Queue is full!\n");
dequeuedElement = queue.dequeue();
if (dequeuedElement == "empty")
System.out.println("Queue is empty\n");
else
System.out.println("The element dequeued is : " + dequeuedElement +
"\n");
dequeuedElement = queue.dequeue();
if (dequeuedElement == "empty")
System.out.println("Queue is empty\n");
else
System.out.println("The element dequeued is : " + dequeuedElement +
"\n");
dequeuedElement = queue.dequeue();
if (dequeuedElement == "empty")
System.out.println("Queue is empty\n");
else
System.out.println("The element dequeued is : " + dequeuedElement +
"\n");
dequeuedElement = queue.dequeue();
if (dequeuedElement == "empty")
System.out.println("Queue is empty\n");
else
System.out.println("The element dequeued is : " + dequeuedElement +
"\n");
dequeuedElement = queue.dequeue();
if (dequeuedElement == "empty")
System.out.println("Queue is empty\n");
else
System.out.println("The element dequeued is : " + dequeuedElement +
"\n");
}
}
-------------------------
*STACK-EXERCISE 1*
class Stack {
Stack(int maxSize) {
this.top = -1;
this.maxSize = maxSize;
arr = new int[maxSize];
}
class Tester {
stack.display();
if (checkTop(stack)) {
System.out.println("The top most element of the stack is an even number");
} else {
System.out.println("The top most element of the stack is an odd number");
}
}
Queue(int maxSize) {
this.front = 0;
this.rear = -1;
this.maxSize = maxSize;
this.arr = new int[this.maxSize];
}
class Tester {
class Stack {
Stack(int maxSize) {
this.top = -1;
this.maxSize = maxSize;
arr = new int[maxSize];
}
class Tester {
calculateSum(stack);
System.out.println("Updated stack");
stack.display();
}
Queue(int maxSize) {
this.front = 0;
this.rear = -1;
this.maxSize = maxSize;
this.arr = new int[this.maxSize];
}
class Tester {
private T t;
public T get() {
return t;
}
}
class Tester {
}
---------------------------------
*GENERIC METHOD-TRYOUT*
class GenericDemo{
//Generic Method
public static <E> void display(E[] arr) {
for (E element : arr) {
System.out.println(element);
}
}
System.out.println();
Integer[] numbers = { 1, 2, 3, 4, 5 };
display(numbers);
}
---------------------------
*ARRAYLIST TRYOUT-1*
class Tester {
public static void main(String[] args) {
List<String> food = new ArrayList<String>(); // Creating a list of String elements
food.add("Pizza"); // Adding elements
food.add("Burger");
food.add("Pasta");
food.add("Sandwich");
food.add(1, "momos");
System.out.println("Food items: " + food);
}
}
class Tester {
public static void main(String[] args) {
List<String> food = new ArrayList<String>(); // Creating a list of String elements
food.add("Pizza"); // Adding elements
food.add("Burger");
food.add("Pasta");
food.add("Sandwich");
for (String element : food) {
System.out.println("Food items: " + element);
}
}
}
-------------------------
*ARRATLIST-TRYOUT 2*
import java.util.ArrayList;
import java.util.List;
class Tester {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<Integer>(); // Creating an ArrayList
object
// Adding the elements to the list
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);
numbers.add(6);
System.out.println("numbers list: " + numbers);
}
}
--------------------
*ARRAYLIST TRYOUT-3*
import java.util.ArrayList;
import java.util.List;
class Tester {
}
}
------------------------
*ITERATING THROUGH ARRAYLIST USING FOR EACH- TRYOUT*
import java.util.ArrayList;
import java.util.List;
class Student {
private int studentId;
private String studentName;
private boolean courseRegistered;
public Student(int studentId, String studentName, boolean courseRegistered) {
this.studentId = studentId;
this.studentName = studentName;
this.courseRegistered = courseRegistered;
}
class Tester {
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
students.add(new Student(1001, "Steve", true));
students.add(new Student(1002, "Rachel", false));
students.add(new Student(1003, "Monica", true));
students.add(new Student(1004, "David", true));
}
}
-------------------------
*ITERATOR METHOD TRYOUT*
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
class Student {
private int studentId;
private String studentName;
private boolean courseRegistered;
class Tester {
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
students.add(new Student(1001, "Steve", true));
students.add(new Student(1002, "Rachel", false));
students.add(new Student(1003, "Monica", true));
students.add(new Student(1004, "David", true));
System.out.println();
}
}
---------------------------
*METHODS IN LINKED LIST-TRYOUT*
import java.util.List;
import java.util.LinkedList;
class EuropeTrip {
// Displaying elements
System.out.println(cities);
// Inserting elements
cities.add(3, "Prague");
System.out.println(cities);
// Removing elements
cities.remove("Munich");
System.out.println(cities);
// Replacing element
cities.set(2, "Berlin");
System.out.println(cities);
// Displaying size
System.out.println(cities.size());
@Override
public String toString() {
return "Order Id: "+getOrderId()+", Item names: "+getItemNames()+", Cash on
delivery: "+isCashOnDelivery();
}
}
class Tester {
}
-------------------------
*ARRAYLIST ASSIGNMENT-1*
import java.util.ArrayList;
import java.util.List;
class Participant {
private String participantName;
private String participantTalent;
private double participantScore;
@Override
public String toString() {
return "Participant Name: "+getParticipantName()+", Participant Talent:
"+getParticipantTalent()+", Participant Score: "+getParticipantScore();
}
class Tester {
if(p.getParticipantTalent().equals(talent)){
list.add(p);
}
}
return list;
}
System.out.println("Finalists");
for (Participant finalist : finalistsList)
System.out.println(finalist);
String talent = "Singing";
System.out.println("Finalists in " + talent + " category");
}
--------------------------
*LINKEDLIST EXERCISE-1*
import java.util.List;
import java.util.LinkedList;
import java.util.Iterator;
class Tester {
class Tester {
public static void main(String[] args) {
// Creating HashSet
Set<String> food = new HashSet<String>();
// Traversing elements
Iterator<String> item = food.iterator();
while (item.hasNext())
System.out.println(item.next());
class User {
private int userId;
private String userName;
private String emailId;
@Override
public boolean equals(Object user) {
User otherUser = (User) user;
if (this.emailId.equals(otherUser.emailId))
return true;
return false;
}
@Override
public int hashCode() {
return emailId.hashCode();
}
@Override
public String toString() {
return "User Name: "+userName + ", Email Id: " + emailId;
}
}
class Tester {
class Tester {
System.out.println(books);
}
-------------------------------
*QUEUE USING ARRAY DEQUE- TRYOUT*
import java.util.Deque;
import java.util.ArrayDeque;
class Tester{
queue.remove();
queue.remove();
queue.remove();
System.out.println();
System.out.println("Head of the queue using element() - "+queue.element());
System.out.println("Head of the queue using peek() - "+queue.peek());
queue.remove();
queue.remove();
System.out.println();
System.out.println("Removing the head of the queue using poll when queue is
empty - "+queue.poll()); // returns null since queue is empty
/* Uncomment the lines of code given below one at a time and observe the output */
//System.out.println("Head of the queue using element() when queue is empty -
"+queue.element()); // throws NoSuchElementException since queue is empty
}
}
------------------------
*STACK USING ARRAYDEQUE-TRYOUT*
import java.util.Deque;
import java.util.ArrayDeque;
class Tester {
stack.pop();
stack.pop();
stack.pop();
System.out.println();
System.out.println("Top of the stack using peek() - "+stack.peek());
stack.pop();
stack.pop();
System.out.println(stack);
//Uncomment the below code and observe the output
//System.out.println("Trying to remove the element from the top of the stack using pop()
when stack is empty - "+stack.pop(){{//or use stack.poll() if you don't want exception//}}); //
throws NoSuchElementException since stack is empty
}
}
-----------------------
*SET INTERFACE EXERCISE 1*
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
class Student {
@Override
public boolean equals(Object student) {
Student otherStudent = (Student) student;
if (this.studentId == otherStudent.studentId)
return true;
return false;
}
@Override
public int hashCode() {
return studentId;
}
@Override
public String toString() {
return "Student Id: "+studentId + ", Student Name: " + studentName;
}
class Tester {
class Student {
}
return studentMarks;
}
class Tester {
}
}
-------------------
*LINKED LIST ASSIGNMENT 1*
import java.util.LinkedList;
import java.util.List;
class Tester {
}
-------------------------
*LINKED LIST ASSIGNMENT 2*
import java.util.LinkedList;
import java.util.List;
class Tester {
System.out.println(findCommonElements(listOne, listTwo));
}
}
-------------------------
*LINKED LIST ASSIGNMENT 3*
import java.util.LinkedList;
import java.util.List;
class Tester {
class Tester {
queue.enqueue("Emily");
queue.enqueue("Lily");
queue.enqueue("Rachel");
queue.enqueue("Rose");
queue.dequeue();
queue.dequeue();
System.out.println(queue.getQueue());
}
}
------------------
*SET INTERFACE ASSIGNMENT 1*
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
class Student {
private int studentId;
private String studentName;
private String emailId;
private String event;
@Override
public boolean equals(Object student) {
Student otherStudent = (Student) student;
if (this.emailId.equals(otherStudent.emailId))
return true;
return false;
}
@Override
public int hashCode() {
return emailId.hashCode();
}
@Override
public String toString() {
return "Student Id: " + studentId + ", Student Name: " + studentName + ", Email
Id: " + emailId;
}
}
class Tester {
class Tester {
}
-------------------
*HASHMAP ASSIGNMENT 2*
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
class Tester {
System.out.println("Merged Map\n===========");
for (Entry<String, Integer> entry : mergedMap.entrySet()) {
System.out.println(entry.getKey()+" -- "+entry.getValue());
}
}
}
------------------------
*HASHMAP ASSIGNMENT 3*
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
class Tester {
return charCountMap;
}
System.out.println("Occurrences of characters\n=======================");
for (Entry<Character, Integer> entry : occurrenceMap.entrySet()) {
System.out.println(entry.getKey()+" -- "+entry.getValue());
}
}
----------------------
*QUEUE INTERFACE EXERCISE 1*
import java.util.Deque;
import java.util.ArrayDeque;
class Tester {
}
}
-------------------
*QUEUE INTERFACE EXERCISE 2*
import java.util.Deque;
import java.util.ArrayDeque;
class Tester {
return ans;
}
public static int smallest(Deque<Integer> st){
if(st.size()==0)
return Integer.MAX_VALUE;
st.addFirst(crr);
return Math.min(crr,rv);
class Patient {
@Override
public String toString() {
return "Name: "+this.name+", Gender: "+this.gender+", Age: "+this.age;
}
}
class Tester {
int counter=0;
for (Deque<Patient> queue : queuesList) {
if(counter==0)
System.out.println("Patients in the senior
queue\n============================");
else
System.out.println("Patients in the normal
queue\n============================");
}
-------------------
*QUEUE INTERFACE ASSIGNMENT 2*
import java.util.ArrayDeque;
import java.util.Deque;
class Tester {
if (numberOfIterations == -1)
System.out.println("Element not found!");
else
System.out.println("Element found! Number of iterations required to find
the element : " + numberOfIterations);
}
}
---------------------------
*BINARY SEARCH TRYOUT 1*
class Tester {
//-1 is being returned when the element is not present in the array
return -1;
}
if (arrayOfElements[mid] == elementToBeSearched)
return mid;
if (elements[mid] == elementToBeSearched)
return mid;
if (indexPosition == -1)
System.out.println("Element not found!");
else
System.out.println("Element found at index position " + indexPosition+"!");
}
-------------------------
*LINEAR SEARCH ASSIGNMENT 1*
class Tester {
if (numberOfIterations == -1)
System.out.println("Employee Id " + employeeIdToBeSearched + " is not
found!");
else
System.out.println("Employee Id " + employeeIdToBeSearched + " is
found! Number of iterations : " + numberOfIterations);
}
}
---------------------------
*BINARY SEARCH ASSIGNMENT 1*
class Tester {
//-1 is being returned when the element is not present in the array
return -1;
}
if (index == -1)
System.out.println("Customer Id " + customerIdToBeSearched + " is not
found!");
else
System.out.println("Customer Id " + customerIdToBeSearched + " is
found at index position " + index+"!");
}
}
-----------------------------
=============================
DAY 6
=============================
*BUBBLE SORT*
> NO OF PASSESS DOESN'T DEPEND ON INPUT SIZE
> NO OF SWAPS AND PASSES ARE NOT EQUAL
> 1 EXTRA PASS IS REQUIRED EVEN AFTER SORTING
-----------------------
*BUBBLE SORT TRYOUT*
class Tester {
static int noOfSwaps = 0;
static int noOfPasses = 0;
bubbleSort(numbers);
System.out.println("Sorted array:");
for (int number : numbers) {
System.out.println(number);
}
System.out.println("Given array:");
displayArray(elements);
System.out.println("==========================");
System.out.println("Total number of passes needed to sort the array: " +
noOfPasses);
System.out.println("==========================");
}
}
----------------------
*MERGE SORT TRYOUT*
class Tester {
public static void main(String[] args) {
int[] arr = { 19, 8, 16, 26, 45, 76 };
mergeSort(arr, arr.length);
for (int number : arr)
System.out.println(number);
}
public static void merge(int[] arr, int[] left, int[] right, int leftMerge, int rightMerge) {
public static void merge(int[] elements, int[] left, int[] right, int leftMerge, int rightMerge) {
//Implement your logic here
int firstIndex = 0; //initial index of first sub-array
int secondIndex = 0; //initial index of second sub-array
int thirdIndex = 0; //initial index of merged sub-array
while (firstIndex < leftMerge && secondIndex < rightMerge) {
if (left[firstIndex] <= right[secondIndex])
elements[thirdIndex++] = left[firstIndex++];
else
elements[thirdIndex++] = right[secondIndex++];
}
System.out.println("Given Array:");
displayArray(elements);
mergeSort(elements, elements.length);
System.out.println("Sorted Array:");
displayArray(elements);
}
----------------------------
*BUBBLE SORT ASSIGNMENT 1*
class Tester {
static int noOfSwaps = 0;
static int noOfPasses = 0;
}
----------------------
*MERGE SORT ASSIGNMENT 1*
class Tester {
if (num > 0) {
f[1] = 1;
for (index = 2; index <= num; index++) {
//Add the previous 2 numbers in the series and store the sum
f[index] = f[index - 1] + f[index - 2];
}
}
return f[num];
}
return val[n];
}
int[][] arr3=multiply(arr1,arr2);
for(int index1=0;index1<arr3.length;index1++){
for(int index2=0;index2<arr3.length;index2++){
System.out.print(arr3[index1][index2]+" ");
}
System.out.println();
}
}
}
-------------------------------
*DIVIDE AND CONQUER ASSIGNMENT 1*
class Tester {
public static int findMaxCrossingSubarraySum(int arr[], int low, int mid, int high) {
// Implement your logic here and change the return statement accordingly
// Include elements on left of mid.
int sum = 0;
int left_sum = Integer.MIN_VALUE;
for (int i = mid; i >= low; i--)
{
sum = sum + arr[i];
if (sum > left_sum)
left_sum = sum;
}
// To store result
int sum = 0;
char[] S = inputString.toCharArray();
// We have encountered an
// unbalanced part of string
if (count < 0)
{
// Reset count to 1
count = 1;
}
}
return sum;
}
}
---------------------------
*DYNAMIC PROGRAMMING ASSIGNMENT 1*
import java.util.ArrayList;
import java.util.List;
class Tester {
if (inputString.length() == 0)
{
count++;
}
if (wordsList.contains(prefix)) {
findWordSegments(wordsList, inputString.substring(i));
}
}
}