I am trying to sort elements(Integers) present in queue, using another queue. Queue does not have negative numbers or duplicate values Queue must sorted in ascending order.(head -> tail) sample input:4 [7 3 9 5]
output: [3 5 7 9]
import java.util.*;
public class Source {
public static void main(String args[]) {
Queue<Integer> queue = new LinkedList<Integer>();
Scanner s = new Scanner(System.in);
int n = s.nextInt();
while (n-- > 0)
queue.add(s.nextInt());
sort(queue);
}
// Method to sort the queue
static void sort(Queue<Integer> queue) {
Queue<Integer> queue2 = new LinkedList<Integer>();
queue2.add(queue.remove());
while(queue.size()!=0){
int temp = queue.remove();
queue2.add(temp);
while(temp<queue2.peek()){
queue2.add(queue2.remove());
}
}
System.out.println(queue2);
}
}