forked from Team-Vaayushastra/GUI-tasks
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathall.java
More file actions
255 lines (222 loc) · 6.2 KB
/
all.java
File metadata and controls
255 lines (222 loc) · 6.2 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
class all{
// sorting
static void display(int arr[]){
for(int i:arr){
System.out.print(i+" ");
}
System.out.println();
}
static void selection_sort(int arr[]){
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length;j++){
if(arr[i]<arr[j]){
int t=arr[j];
arr[j]=arr[i];
arr[i]=t;
}
}
}
display(arr);
}
static void bubble_sort(int arr[]){
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length-1;j++){
if(arr[j]>arr[j+1]){
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
display(arr);
}
static void divide(int arr[]){
if(arr.length<2)return;
int mid=arr.length/2;
int left[]=new int[mid];
int right[]=new int[arr.length-mid];
for(int i=0;i<mid;i++){
left[i]=arr[i];
}
for(int i=mid;i<arr.length;i++){
right[i-mid]=arr[i];
}
divide(left);
divide(right);
conquer(arr,left,right);
}
static void conquer(int arr[],int left[],int right[]){
int i=0,j=0,k=0;
while(i<left.length && j<right.length){
if(left[i]<right[j]){
arr[k]=left[i];
i++;
}
else{
arr[k]=right[j];
j++;
}
k++;
}
while(i<left.length){
arr[k]=left[i];
i++;k++;
}
while(j<right.length){
arr[k]=right[j];
j++;k++;
}
}
static void insertion_sort(int arr[]){
for(int i=1;i<arr.length;i++){
int curr=arr[i];
int j=i-1;
while(j>=0 && curr<arr[j]){
arr[j+1]=arr[j];
j--;
}
arr[j+1]=curr;
}
display(arr);
}
static int binary_search(int arr[],int start,int end,int k){
if(end>=start){
int mid=(start+end)/2;
if(arr[mid] == k){
return mid;
}
if(k<arr[mid]){
return binary_search(arr,start,mid-1,k);
}
// if(k>arr[mid]){
else{
return binary_search(arr,mid+1,arr.length,k);
}
// if(arr[mid] != k){
// return -1;
// }
}
// return binary_search(arr,mid+1,arr.length,k);
return -1;
}
static int jump_search(int arr[],int x){
int step=(int)Math.floor(Math.sqrt(arr.length));
if(arr[step]==x){return step;}
for(int i=0;i<arr.length-1;i=i+step){
if(arr[i]>x && arr[i-step]>=0){
for(int j=i;j>=0;j--){
if(x == arr[j]){return j;}
}
}
// else{}
}
return -1;
}
// ll
static nodee head1;
static class nodee{
int data;
nodee next;
nodee(int data){
this.data=data;
}
}
static void insert(int data){
nodee n=new nodee(data);
if(head1 == null){
head1=n;
// n.next=null;
return;
}
n.next=head1;
head1 = n;
}
static void show(){
nodee n=head1;
while(n != null){
System.out.print(n.data+" ");
n=n.next;
}
}
// cll
static node head,tail;
static class node{
node next;
int data;
node(int data){
this.data=data;
}
}
static void add(int data){
node n=new node(data);
if(head == null){
head=n;
tail=n;
tail.next=head;
// n.next
}else{
tail.next=n;
tail=n;
tail.next=head;
}
}
static void cc_d(){
node n=head;
do{
System.out.print(n.data+" ");
n=n.next;
}
while(n != head);
}
// tree
static class node1{
node1 left,right;
int data;
node1(int data){
this.data=data;
this.left=null;
this.right=null;
}
}
static class tree{
static int i=-1;
static node1 build_tree(int nodes[]){
i++;
if(nodes[i] == -1)return null;
node1 n=new node1(nodes[i]);
n.left=build_tree(nodes);
n.right=build_tree(nodes);
return n;
}
}
static void preorder(node1 root){
if(root == null) return;
System.out.print(root.data+" ");
preorder(root.left);
preorder(root.right);
}
public static void main(String args[]){
int arr[]={2,4,6,3,8,99,43,2,65,0,4};
int a[]={0 ,2 ,2 ,3 ,4 ,4, 6 ,8,43,65,99};
display(arr);
// selection_sort(arr);
// bubble_sort(arr);
// divide(arr);
// display(arr);
insertion_sort(arr);
// System.out.println(binary_search(a,0,a.length,999));
System.out.println(jump_search(arr,909));
// LL
// insert(20);insert(11);insert(77);
// show();
// CLL
add(22);add(722);add(422);add(123);
cc_d();
// TREE
int nodes[]={9,2,4,-1,-1,5,-1,-1,3,-1,6,-1,-1};
tree t=new tree();
node1 n =t.build_tree(nodes);
System.out.println();
preorder(n);
}
}