Repository navigation
Expand file tree
/
Copy pathMainClass.java
More file actions
101 lines (85 loc) · 1.68 KB
/
Copy pathMainClass.java
File metadata and controls
101 lines (85 loc) · 1.68 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
package sorting1;
public class MainClass {
static void swap(int a[], int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static void intersectionOfTwoSortedArrays(int a[], int b[]) {
int i = 0;
int j = 0;
// i = 1
// j = 3
// int a[] = {2, 5, 6, 6, 8, 8};
// int b[] = {1, 1, 2, 3, 6, 6, 9};
while(i < a.length && j < b.length) {
if(i > 0 && a[i] == a[i-1]) {
i++;
continue;
}
if(a[i] == b[j]) {
System.out.print(a[i]+" ");
i++;
j++;
} else if(a[i] > b[j]) {
j++;
} else if(a[i] < b[j]) {
i++;
}
}
}
static void selectionSort(int a[]) {
for(int i = 0; i<a.length-1; i++) {
int min = i;
for(int j = i+1; j<a.length; j++) {
if(a[j] < a[min]) {
min = j;
}
}
swap(a, min, i);
}
}
static void insertionSort(int a[]) {
for(int i = 1; i<a.length; i++) {
printArray(a);
System.out.println();
int j = i-1;
int temp = a[i];
for(; j>=0; j--) {
if(temp < a[j]) {
a[j+1] = a[j];
} else {
break;
}
}
a[j+1] = temp;
}
}
static void bubbleSort(int a[]) {
for(int i = 0; i<a.length-1; i++) {
boolean isSwapped = false;
for(int j = 0; j<a.length-i-1; j++) {
if(a[j] > a[j+1]) {
isSwapped = true;
swap(a, j, j+1);
}
}
if(!isSwapped) break;
}
}
static void printArray(int a[]) {
for(int e: a) {
System.out.print(e+" ");
}
}
public static void main(String[] args) {
// int a[] = {9, 5, 2, 6, 3, 1, 3};
// bubbleSort(a);
// insertionSort(a);
// selectionSort(a);
// printArray(a);
int a[] = {2, 2, 5, 6, 6, 8, 8};
int b[] = {1, 1, 2, 2, 3, 6, 6, 9};
intersectionOfTwoSortedArrays(a, b);
}
}