2021-10-08 09:17:16 +00:00
|
|
|
|
#include "vec.h"
|
|
|
|
|
|
|
|
|
|
Vec::Vec(int len)
|
|
|
|
|
{
|
|
|
|
|
_used = len - 1;
|
|
|
|
|
_len = len * 2;
|
|
|
|
|
_v = new int[_len];
|
|
|
|
|
|
|
|
|
|
// for (int i = 0; i <= _used; i++)
|
|
|
|
|
// _v[i] = i + 1;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i <= _used; i++)
|
|
|
|
|
_v[i] = rand() % (_used + 1);
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i <= _used; i++)
|
|
|
|
|
swap(i, rand() % (_used + 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Vec::get(int a)
|
|
|
|
|
{
|
|
|
|
|
return _v[a];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Vec::put(int a, int value)
|
|
|
|
|
{
|
|
|
|
|
_v[a] = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Vec::swap(int a, int b)
|
|
|
|
|
{
|
|
|
|
|
int temp = _v[a];
|
|
|
|
|
_v[a] = _v[b];
|
|
|
|
|
_v[b] = temp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Vec::expand()
|
|
|
|
|
{
|
|
|
|
|
_len = _len * 2;
|
|
|
|
|
int *p = new int[_len];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i <= _used; i++)
|
|
|
|
|
p[i] = _v[i];
|
|
|
|
|
|
|
|
|
|
delete[] _v;
|
|
|
|
|
_v = p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Vec::shrink()
|
|
|
|
|
{
|
|
|
|
|
if (((double)_used / (double)_len) >= 0.25)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_len = _len >> 1;
|
|
|
|
|
int *p = new int[_len];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i <= _used; i++)
|
|
|
|
|
p[i] = _v[i];
|
|
|
|
|
|
|
|
|
|
delete[] _v;
|
|
|
|
|
_v = p;
|
|
|
|
|
}
|
|
|
|
|
//λ<><CEBB> <20><>ֵ
|
|
|
|
|
int Vec::insert(int locate, int value)
|
|
|
|
|
{
|
2021-10-15 07:20:32 +00:00
|
|
|
|
if (locate == _used + 1)
|
|
|
|
|
{
|
|
|
|
|
_used++;
|
|
|
|
|
_v[_used] = value;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-10-08 09:17:16 +00:00
|
|
|
|
if (locate < 0 || locate > _used)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
_used++;
|
|
|
|
|
|
|
|
|
|
if (_used >= _len)
|
|
|
|
|
expand();
|
|
|
|
|
|
|
|
|
|
for (int i = _used; i > locate; i--)
|
|
|
|
|
_v[i] = _v[i - 1];
|
|
|
|
|
|
|
|
|
|
_v[locate] = value;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//λ<><CEBB> ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
int Vec::remove(int locate, int value)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (locate < 0 || ((_used - value) < 0) || ((locate + value - 1) > _used))
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
_used = _used - value;
|
|
|
|
|
|
|
|
|
|
for (int i = locate; i <= _used; i++)
|
|
|
|
|
_v[i] = _v[i + value];
|
|
|
|
|
|
|
|
|
|
shrink();
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//ɾ<><C9BE>ijһԪ<D2BB><D4AA>
|
|
|
|
|
int Vec::remove_sorted(int value)
|
|
|
|
|
{
|
|
|
|
|
int j, i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i <= _used && _v[i] != value; i++)
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
for (j = 1; _v[i + j] == value; j++)
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
return remove(i, j);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Vec::count(int value)
|
|
|
|
|
{
|
|
|
|
|
int j, i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i <= _used && _v[i] != value; i++)
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
for (j = 0; _v[i + j] == value && i + j <= _used; j++)
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
if (j == 0)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
return j;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Vec::find(int value)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (i; i <= _used; i++)
|
|
|
|
|
{
|
|
|
|
|
if (_v[i] == value)
|
|
|
|
|
{
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Vec::getlen()
|
|
|
|
|
{
|
|
|
|
|
return _len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int Vec::getused()
|
|
|
|
|
{
|
|
|
|
|
return _used;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Vec::printall()
|
|
|
|
|
{
|
2021-10-15 07:20:32 +00:00
|
|
|
|
for (int i = 0; i <= _used; i++)
|
2021-10-08 09:17:16 +00:00
|
|
|
|
printf("%d\n", _v[i]);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-15 07:20:32 +00:00
|
|
|
|
void Vec::bubbleSort()
|
2021-10-08 09:17:16 +00:00
|
|
|
|
{
|
|
|
|
|
int n = _used;
|
|
|
|
|
bool sorted = false;
|
|
|
|
|
|
|
|
|
|
while (!sorted)
|
|
|
|
|
{
|
|
|
|
|
sorted = true;
|
|
|
|
|
|
2021-10-15 07:20:32 +00:00
|
|
|
|
for (int i = 1; i <= n; i++)
|
2021-10-08 09:17:16 +00:00
|
|
|
|
{
|
|
|
|
|
if (_v[i - 1] > _v[i])
|
|
|
|
|
{
|
|
|
|
|
swap(i - 1, i);
|
|
|
|
|
|
|
|
|
|
sorted = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
n--;
|
|
|
|
|
}
|
|
|
|
|
_sorted = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-15 07:20:32 +00:00
|
|
|
|
void Vec::mergeSort(int lo, int hi)
|
|
|
|
|
{
|
|
|
|
|
if (lo >= hi)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int mi = lo + (hi - lo) / 2;
|
|
|
|
|
mergeSort(lo, mi);
|
|
|
|
|
mergeSort(mi + 1, hi);
|
|
|
|
|
merge(lo, mi, hi);
|
|
|
|
|
}
|
2021-10-08 09:17:16 +00:00
|
|
|
|
int Vec::search(int e)
|
|
|
|
|
{
|
|
|
|
|
if (_sorted == false)
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
for (i; i <= _used && _v[i] <= e; i++)
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
return i - 1;
|
2021-10-15 07:20:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Vec::merge(int lo, int mi, int hi)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
int i = lo, j = mi + 1, k = 0;
|
|
|
|
|
int *temp = new int[hi - lo + 1];
|
|
|
|
|
while (i <= mi && j <= hi)
|
|
|
|
|
{
|
|
|
|
|
if (_v[i] <= _v[j])
|
|
|
|
|
temp[k++] = _v[i++];
|
|
|
|
|
else
|
|
|
|
|
temp[k++] = _v[j++];
|
|
|
|
|
}
|
|
|
|
|
while (i <= mi)
|
|
|
|
|
temp[k++] = _v[i++];
|
|
|
|
|
while (j <= hi)
|
|
|
|
|
temp[k++] = _v[j++];
|
|
|
|
|
for (i = lo, k = 0; i <= hi; i++, k++)
|
|
|
|
|
_v[i] = temp[k];
|
|
|
|
|
delete[] temp;
|
|
|
|
|
}
|