-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathhighArrayApp.java
162 lines (129 loc) · 2.41 KB
/
highArrayApp.java
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
/*
Solved By: Sandeep Ranjan (1641012352)
Solution for Programming Projects
2.1, 2.2, 2.3, 2.6
*/
import java.util.*;
class HighArray {
private int[] a;
private int nEle;
public HighArray(int size) {
a = new int[size];
nEle = 0;
}
public int getNumEle() {
return nEle;
}
public void display() {
for(int i=0; i<nEle; i++)
System.out.print(a[i] + " ");
System.out.println();
}
public void insert(int val) {
a[nEle] = val;
nEle++;
}
public int search(int val) {
int i;
for(i=0; i<nEle; i++)
if(a[i] == val) break;
if(i == nEle) return (-1);
else return i;
}
public int delete(int val) {
int index = search(val);
if(index != -1) {
for(int k=index; k<nEle-1; k++) {
a[k] = a[k+1];
}
nEle--;
return 1;
}else {
return 0;
}
}
public int getMax() {
if(nEle == 0) return -1;
int max = a[0];
for(int i=1; i<nEle; i++) {
if(a[i] > max) max = a[i];
}
return max;
}
public int removeMax() {
int max = getMax();
delete(max);
return max;
}
public void noDups() {
int value = 0;
for(int i=0; i<nEle; i++) {
value = a[i];
for(int j=i+1; j<nEle; j++) {
if(a[j] == value) {
for(int k=j; k<nEle-1; k++) {
a[k] = a[k+1];
}
nEle--;
j--;
}
}
}
}
}
class highArrayApp {
public static void main(String[] args) {
int size = 20;
HighArray ar = new HighArray(size);
Scanner sc = new Scanner(System.in);
ar.insert(34);
ar.insert(56);
ar.insert(23);
ar.insert(12);
ar.insert(90);
ar.insert(89);
ar.insert(21);
ar.insert(77);
ar.insert(99);
ar.insert(44);
ar.insert(55);
ar.insert(22);
ar.insert(88);
ar.insert(11);
ar.insert(00);
ar.insert(66);
ar.insert(33);
ar.display();
// ELEMENT TO BE SEARCHED
/*
System.out.println("Enter the element to be seached");
int sarch = sc.nextInt();
if(ar.search(sarch) != -1) {
System.out.println("Element found");
} else {
System.out.println("Element not found");
}
*/
// ELEMENT TO BE DELETED
/*
System.out.println("Enter the element to be deleted");
int del = sc.nextInt();
ar.delete(del);
*/
// SORT ARRAY IN DESC ORDER
/*
HighArray ar2 = new HighArray(size);
int len = ar.getNumEle();
for(int i=0; i<len; i++) {
int ele = ar.removeMax();
ar2.insert(ele);
}
ar2.display();
*/
// REMOVE DUPLICATES
/*
ar.noDups();
ar.display();
*/
}
}