-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathass2_q3.java
More file actions
179 lines (177 loc) · 3.73 KB
/
ass2_q3.java
File metadata and controls
179 lines (177 loc) · 3.73 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
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(" The contents of the queue are :\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();
//T o = q.dequeue();
//while (o!= null){
//System.out.println(o);
//o = q.dequeue();
//}
}
class Sorting
{
public Double[] test_arr1;
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;
}
}
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) {
exchange(i, j);
i++;
j--;
}
}
if (low < j)
quicksort(low, j);
if (i < high)
quicksort(i, high);
}
public void exchange(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("The 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 = { 3, 4, 2, 1, 5 };
Double[] doubleArray = { 5.1, 2.2, 3.2, 1.4 };
Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };
String[] stringArray = {"qw", "we", "er", "rt", "ty"};
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();
}
}