Merge Queues
Code for merging queues
import java.util.LinkedList;
import java.util.Queue;
public class MergeQueues<T> {
private Queue<T> queue1;
private Queue<T> queue2;
public MergeQueues(Queue<T> queue1, Queue<T> queue2) {
this.queue1 = queue1;
this.queue2 = queue2;
}
public Queue<T> merge() {
Queue<T> result = new LinkedList<>();
while (!queue1.isEmpty() && !queue2.isEmpty()) {
if (queue1.peek().hashCode() < queue2.peek().hashCode()) {
result.add(queue1.poll());
} else {
result.add(queue2.poll());
}
}
while (!queue1.isEmpty()) {
result.add(queue1.poll());
}
while (!queue2.isEmpty()) {
result.add(queue2.poll());
}
return result;
}
public static void main(String[] args) {
Queue<Integer> queue1 = new LinkedList<>();
Queue<Integer> queue2 = new LinkedList<>();
queue1.add(1);
queue1.add(3);
queue1.add(5);
queue2.add(2);
queue2.add(4);
queue2.add(6);
System.out.println("Queue 1: " + queue1);
System.out.println("Queue 2: " + queue2);
MergeQueues<Integer> merger = new MergeQueues<>(queue1, queue2);
Queue<Integer> mergedQueue = merger.merge();
System.out.println("Merged Queue: " + mergedQueue);
}
}
MergeQueues.main(null);
public class SortingAlgos {
public static void bubbleSort(int[] arr) {
for (int i =0; i < arr.length; i++) {
// Loop through the array
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Check to see if the next element is greater than the current element
int temp = arr[j];
// Swap the elements
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void insertionSort(int[] array) {
for (int i = 1; i < array.length; i++) {
// Loop through the array
int key = array[i];
// Set the key to the current element
int j = i - 1;
// Set j to the previous element
while (j >= 0 && array[j] > key) {
// Loop through the array until the previous element is less than the key
array[j + 1] = array[j];
// Set the next element to the current element
j = j - 1;
}
// Set the next element to the key
array[j + 1] = key;
}
}
public static void main(String[] args) {
int[] arr = { 5, 4, 3, 2, 1 };
bubbleSort(arr);
System.out.println(Arrays.toString(arr));
int[] arr2 = { 5, 4, 3, 2, 1 };
insertionSort(arr2);
System.out.println(Arrays.toString(arr2));
}
}
SortingAlgos.main(null);
import java.util.*;
public class ShuffleQueue {
public static void main(String[] args) {
Queue<Integer> orderedQueue = new LinkedList<>();
orderedQueue.add(1);
orderedQueue.add(2);
orderedQueue.add(3);
orderedQueue.add(4);
orderedQueue.add(5);
orderedQueue.add(6);
orderedQueue.add(7);
orderedQueue.add(8);
orderedQueue.add(9);
orderedQueue.add(10);
Queue<Integer> shuffledQueue = shuffle(orderedQueue);
System.out.println("Original queue: " + orderedQueue);
System.out.println("Shuffled queue: " + shuffledQueue);
}
public static <T> Queue<T> shuffle(Queue<T> queue) {
List<T> list = new ArrayList<>(queue);
Collections.shuffle(list);
Queue<T> shuffledQueue = new LinkedList<>(list);
return shuffledQueue;
}
}
ShuffleQueue.main(null);
import java.util.Stack;
public class StackMerger {
public static Stack<Integer> merge(Stack<Integer> stack1, Stack<Integer> stack2) {
Stack<Integer> result = new Stack<>();
while (!stack1.isEmpty() && !stack2.isEmpty()) {
// Loop through the stacks
if (stack1.peek() < stack2.peek()) {
// Check to see if the top of stack1 is less than the top of stack2
result.push(stack1.pop());
} else {
// Otherwise, push the top of stack2 to the result stack
result.push(stack2.pop());
}
}
while (!stack1.isEmpty()) {
// Push the remaining elements of stack1 to the result stack
result.push(stack1.pop());
}
while (!stack2.isEmpty()) {
// Push the remaining elements of stack2 to the result stack
result.push(stack2.pop());
}
return result;
}
public static void main(String[] args) {
// Create two stacks
Stack<Integer> stack1 = new Stack<>();
stack1.push(5);
stack1.push(3);
stack1.push(1);
Stack<Integer> stack2 = new Stack<>();
stack2.push(6);
stack2.push(4);
stack2.push(2);
Stack<Integer> result = merge(stack1, stack2);
// Print the result stack
System.out.println("Merged stack:");
while (!result.isEmpty()) {
System.out.println(result.pop());
}
}
}
StackMerger.main(null);