forked from Denisolt/CSCI-160
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayIns.java
More file actions
130 lines (121 loc) · 3.95 KB
/
ArrayIns.java
File metadata and controls
130 lines (121 loc) · 3.95 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
/**
* Muhammad Amar Waqas
* Student ID: 1022832
*/
// demonstrates insertion sort
// to run this program: C>java InsertSortApp
//--------------------------------------------------------------
class ArrayIns
{
int count = 0;
private long[] a; // ref to array a
private int nElems; // number of data items
//--------------------------------------------------------------
public ArrayIns(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
}
//--------------------------------------------------------------
public void insert(long value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
//--------------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j<nElems-count; j++) // for each element,
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//--------------------------------------------------------------
public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++) // out is dividing line
{
long temp = a[out]; // remove marked item
in = out; // start shifts at out
while(in>0 && a[in-1] >= temp) // until one is smaller,
{
a[in] = a[in-1]; // shift item to right
--in; // go left one position
}
a[in] = temp; // insert marked item
} // end for
} // end insertionSort()
public long median()
{
int in, out;
for(out=1; out<nElems; out++) // out is dividing line
{
long temp = a[out]; // remove marked item
in = out; // start shifts at out
while(in>0 && a[in-1] >= temp) // until one is smaller,
{
a[in] = a[in-1]; // shift item to right
--in; // go left one position
}
a[in] = temp; // insert marked item
}
int j = nElems;
int y = j/2;
int x = j%2;
if(x == 1)
return a[y];
else
{
long z = (a[y] + a[y-1]);
long g = z/2;
return g;
}
}
public void noDups()
{
int dups = 0;
int start = 0;
int end = 0;
for(int d=nElems; d>=1; d--)
{
if(a[d]==a[d-1])
count++;
}
for(int i = 0; i < nElems; i++)
{
for(int j = 1; j < nElems; j++)
{
if(a[i] == a[j])
{
start = j;
for (int k = j; k < nElems; k++)
{
//dups++;
if (a[i] != a[k])
{
end = k - 1;
break;
}
}
}
if( a[i] == a[j])
{
dups++;
break;
}
}
if( dups > 0)
{
for( int s =nElems; s > 0; s--)
{
a[start] = a[end];
start++;
end++;
}
}
}
}
}
//--------------------------------------------------------------
// end class ArrayIns
////////////////////////////////////////////////////////////////