-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedQueue.java
More file actions
181 lines (176 loc) · 3.75 KB
/
SortedQueue.java
File metadata and controls
181 lines (176 loc) · 3.75 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
class Queue<Q>{
class Node{
Q o;
Node next;
Node(Q obj){
o = obj;
next = null;
}
}
Node front, back;
void enqueue(Q obj){
Node n = new Node(obj);
if (front == null){
front = n;
back = n;
}else{
back.next = n;
back = n;
}
}
Q dequeue(){
if (front == null){
System.out.println("The Queue is empty!!");
return null;
}
Node obj = front;
front = front.next;
return obj.o;
}
void printContent(){
Node n = front;
System.out.print(" Contents of Queue :\t");
while(n!= null){
System.out.print(n.o+"\t");
n = n.next;
}
System.out.println();
}
}
class SortedQueue{
static <T> void test(T[] arr){
Queue<T> q = new Queue<T>();
for(int i=0; i<arr.length; i++){
q.enqueue(arr[i]);
}
q. printContent();
}
//For sorting the array
class Sorting
{
//Array for double value
public Double[] test_arr1;
//Array for key
public Object[] arr;
Sorting(Integer i[])
{
test_arr1=new Double[i.length];
arr=new Integer[i.length];
for(int j=0;j<i.length;j++)
{
arr[j]=i[j];
test_arr1[j]=(double)i[j];
}
}
Sorting(Double i[])
{
test_arr1=new Double[i.length];
arr=new Double[i.length];
for(int j=0;j<i.length;j++)
{
arr[j]=i[j];
test_arr1[j]=i[j];
}
}
Sorting(Character i[])
{
test_arr1=new Double[i.length];
arr=new Character[i.length];
for(int j=0;j<i.length;j++)
{
arr[j]=i[j];
int x=(int)i[j];
test_arr1[j]=(double)i[j];
}
}
Sorting(String i[])
{
test_arr1=new Double[i.length];
arr=new String[i.length];
for(int j=0;j<i.length;j++)
{
arr[j]=i[j];
String s=i[j];
Double d=0.0;
for(int k=0;k<s.length();k++)
{
d=d+Math.pow(255,-(k+1))*s.charAt(k);
}
test_arr1[j]=(double)d;
}
}
//Performing sort operations on the double value array
public void Quicksort()
{
if (arr ==null || test_arr1.length==0){
return;
}
quicksort(0, arr.length - 1);
}
public void quicksort(int low, int high)
{
int i = low, j = high;
double pivot = test_arr1[low + (high-low)/2];
while (i <= j) {
while (test_arr1[i] < pivot) {
i++;
}
while (test_arr1[j] > pivot) {
j--;
}
if (i <= j) {
swap(i, j);
i++;
j--;
}
}
if (low < j)
quicksort(low, j);
if (i < high)
quicksort(i, high);
}
public void swap(int i, int j)
{
Double temp = test_arr1[i];
test_arr1[i] = test_arr1[j];
test_arr1[j] = temp;
Object tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
public void print()
{
//{
System.out.println("Elements of Sorted Queue : ");
for(int j=0;j<test_arr1.length;j++)
{
System.out.print(test_arr1[j]+" ");
}
System.out.println();
//}
}
}
public static void main(String [] args){
Integer[] intArray = { 100 , 101 , 102 , 50 , 5 };
Double[] doubleArray = { 4.9 , 0.8 , 64.8 , 1.9 , 2.5 };
Character[] charArray = { 'A' , 'M' , 'A', 'N' };
String[] stringArray = {"aman" , "kumar" , "wats"};
test(intArray);
test(doubleArray);
test(charArray);
test(stringArray);
sortedQueue sq=new sortedQueue();
sortedQueue.Sorting s1=sq.new Sorting(intArray);
sortedQueue.Sorting s2=sq.new Sorting(doubleArray);
sortedQueue.Sorting s3=sq.new Sorting(charArray);
sortedQueue.Sorting s4=sq.new Sorting(stringArray);
s1.Quicksort();
s2.Quicksort();
s3.Quicksort();
s4.Quicksort();
s1.print();
s2.print();
s3.print();
s4.print();
}
}