This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TheArray.java
142 lines (79 loc) · 2.01 KB
/
TheArray.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
public class TheArray {
// some algorithms and operation of array except for the ones in BasicArray
private int[] array;
private int size;
private int itemsInArray = 0;
public TheArray(int size) {
this.size = size;
array = new int[size];
}
public void displayArray() {
for(int i=0;i<size;i++) {
System.out.println(i+" | "+array[i]);
System.out.println("__|__");
}
}
public void addElement(int element) {
array[itemsInArray++] = element;
size++;
System.out.println();
displayArray();
}
public void selectionSort() {
for (int c = 0; c < (size - 1); c++)
{
int position = c;
for (int d = c + 1; d < size; d++)
{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
int swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
}
public void insertionSort() {
for(int i=0;i<size;i++) {
int j=i;
int toInsert = array[i];
while(j>0 && array[j-1]>toInsert) {
array[j]=array[j-1];
j--;
}
array[j] = toInsert;
}
}
public void generateRandomArray() {
for(int i=0;i<size;i++)
array[i] = (int) (Math.random()*1000) + 10;
itemsInArray = size - 1;
}
public void deleteElementAtIndex(int index) {
if(index>size)
return;
else {
System.out.println("Element found at "+index+" was "+array[index]+" which is now deleted");
System.out.println();
for(int i=index;i<(size-1);i++) {
array[i]=array[i+1];
}
size--;
displayArray();
}
}
public static void main(String[] args) {
TheArray theArray = new TheArray(10);
theArray.generateRandomArray();
theArray.displayArray();
System.out.println();
// theArray.deleteElementAtIndex(3);
// theArray.addElement(256);
theArray.insertionSort();
System.out.println();
theArray.displayArray();
}
}