Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void sort(Queue<Integer> q) {
- int max = getMax(q);
- Queue<Integer> tmp = new Queue<Integer>();
- Queue<Integer> sort = new Queue<Integer>();
- while(!q.isEmpty()) {
- while(!q.isEmpty()) {
- if(q.head() == max) {
- sort.insert(q.remove());
- }else {
- tmp.insert(q.remove());
- }
- }
- while(!tmp.isEmpty()) {
- q.insert(tmp.remove());;
- }
- if(!q.isEmpty()) {
- max = getMax(q);
- }
- }
- while(!sort.isEmpty()) {
- q.insert(sort.remove());
- }
- }
- public static int getMax(Queue<Integer> q) {
- Queue<Integer> tmp = new Queue<Integer>();
- int max = q.head();
- while(!q.isEmpty()) {
- if(q.head() > max) { // change to < for minimum and then the queue will go upwards
- max = q.head();
- }
- tmp.insert(q.remove());
- }
- while(!tmp.isEmpty()) {
- q.insert(tmp.remove());
- }
- return max;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement