-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedQueue_without_extra_space.java
More file actions
82 lines (66 loc) · 1.28 KB
/
SortedQueue_without_extra_space.java
File metadata and controls
82 lines (66 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.util.LinkedList;
import java.util.Queue;
class sortQueue
{
public static int minIndex(Queue<Integer> list,
int sortIndex)
{
int min_index = -1;
int min_value = Integer.MAX_VALUE;
int s = list.size();
for (int i = 0; i < s; i++)
{
int current = list.peek();
list.poll();
if (current <= min_value && i <= sortIndex)
{
min_index = i;
min_value = current;
}
list.add(current);
}
return min_index;
}
public static void mintorear(Queue<Integer> list,
int min_index)
{
int min_value = 0;
int s = list.size();
for (int i = 0; i < s; i++)
{
int current = list.peek();
list.poll();
if (i != min_index)
list.add(current);
else
min_value = current;
}
list.add(min_value);
}
public static void sortQueue(Queue<Integer> list)
{
for(int i = 1; i <= list.size(); i++)
{
int min_index = minIndex(list,list.size() - i);
mintorear(list, min_index);
}
}
public static void main (String[] args)
{
Queue<Integer> list = new LinkedList<Integer>();
list.add(20);
list.add(4);
list.add(5);
list.add(16);
list.add(3);
list.add(1);
sortQueue(list);
while(list.isEmpty()== false)
{
System.out.print(list.peek() + " ");
list.poll();
}
}
}
/**
* @author Pradumn Patel */