add insertion_sort and selection_sort to List class, tests completed
This commit is contained in:
@@ -62,10 +62,10 @@ public:
|
|||||||
void insert(int pos, T const &elem);
|
void insert(int pos, T const &elem);
|
||||||
T pop_back(void) { return _elem[--_size]; }
|
T pop_back(void) { return _elem[--_size]; }
|
||||||
T pop(int index);
|
T pop(int index);
|
||||||
T pop(int lo, int hi);//return the last element popped
|
T pop(int lo, int hi); //return the last element popped
|
||||||
int remove(T const &elem);//remove first element matched, return index, -1 if not found
|
int remove(T const &elem); //remove first element matched, return index, -1 if not found
|
||||||
int unique(void); //return the number of elements deleted
|
int unique(void); //return the number of elements deleted
|
||||||
int uniquify(void);//unique method for sorted vector
|
int uniquify(void); //unique method for sorted vector
|
||||||
void map(void(*visit)(T&));
|
void map(void(*visit)(T&));
|
||||||
template <typename VST> void map(VST& visit);
|
template <typename VST> void map(VST& visit);
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ private:
|
|||||||
int size;
|
int size;
|
||||||
|
|
||||||
protected:// internal methods
|
protected:// internal methods
|
||||||
|
ListNodePosi(T) selectMax(ListNodePosi(T) p, int n);
|
||||||
|
void swap(T &one, T &two);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
//constructors
|
//constructors
|
||||||
@@ -28,6 +29,8 @@ public:
|
|||||||
ListNodePosi(T) last() const { return tail->prev; }
|
ListNodePosi(T) last() const { return tail->prev; }
|
||||||
ListNodePosi(T) find(T const &val) const { return find(val, size, tail); } //find val from all elements of the list
|
ListNodePosi(T) find(T const &val) const { return find(val, size, tail); } //find val from all elements of the list
|
||||||
ListNodePosi(T) find(T const &val, int n, ListNodePosi(T) p) const; //find val from a range of n elements before p
|
ListNodePosi(T) find(T const &val, int n, ListNodePosi(T) p) const; //find val from a range of n elements before p
|
||||||
|
ListNodePosi(T) search(T const &val, int n, ListNodePosi(T) p) const; //find val in a sorted List<T>
|
||||||
|
ListNodePosi(T) search(T const &val) const { return search(val, size, tail); } //find val universally ina sorted List<T>
|
||||||
T& operator[](int rank);
|
T& operator[](int rank);
|
||||||
int getSize() const { return size; }
|
int getSize() const { return size; }
|
||||||
bool empty() const { return size == 0; }
|
bool empty() const { return size == 0; }
|
||||||
@@ -44,6 +47,13 @@ public:
|
|||||||
ListNodePosi(T) pop(int rank);
|
ListNodePosi(T) pop(int rank);
|
||||||
ListNodePosi(T) pop(ListNodePosi(T) p);
|
ListNodePosi(T) pop(ListNodePosi(T) p);
|
||||||
|
|
||||||
|
//sort algorithms
|
||||||
|
void selection_sort() { selection_sort(head->succ, size); }
|
||||||
|
void selection_sort(ListNodePosi(T) p, int n);
|
||||||
|
void insertion_sort() { insertion_sort(head->succ, size); }
|
||||||
|
void insertion_sort(ListNodePosi(T) p, int n);
|
||||||
|
|
||||||
|
|
||||||
//deduplicate & uniquify
|
//deduplicate & uniquify
|
||||||
int deduplicate();
|
int deduplicate();
|
||||||
int uniquify();
|
int uniquify();
|
||||||
@@ -51,6 +61,24 @@ public:
|
|||||||
|
|
||||||
/*----------implementations of class List----------*/
|
/*----------implementations of class List----------*/
|
||||||
|
|
||||||
|
// internal methods
|
||||||
|
template <typename T>
|
||||||
|
void List<T>::swap(T &one, T &two){
|
||||||
|
T tmp = one;
|
||||||
|
one = two;
|
||||||
|
two = tmp;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
ListNodePosi(T) List<T>::selectMax(ListNodePosi(T) p, int n){
|
||||||
|
ListNodePosi(T) maxPosi = p;
|
||||||
|
while(--n){
|
||||||
|
p = p->succ;
|
||||||
|
if (maxPosi->val < p->val) maxPosi = p;
|
||||||
|
}
|
||||||
|
return maxPosi;
|
||||||
|
}
|
||||||
|
|
||||||
//constructors
|
//constructors
|
||||||
template <typename T>
|
template <typename T>
|
||||||
List<T>::List(){
|
List<T>::List(){
|
||||||
@@ -116,6 +144,15 @@ ListNodePosi(T) List<T>::find(T const &val, int n, ListNodePosi(T) p) const {
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
ListNodePosi(T) List<T>::search(T const &val, int n, ListNodePosi(T) p) const{
|
||||||
|
for(int ix = 0; ix != n; ++ix){
|
||||||
|
p = p->prev;
|
||||||
|
if (p->val <= val) return p;
|
||||||
|
}
|
||||||
|
return p->prev;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T& List<T>::operator[](int rank){
|
T& List<T>::operator[](int rank){
|
||||||
ListNodePosi(T) p = head->succ;
|
ListNodePosi(T) p = head->succ;
|
||||||
@@ -190,6 +227,31 @@ ListNodePosi(T) List<T>::pop(ListNodePosi(T) p){
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//sort algorithms
|
||||||
|
template <typename T>
|
||||||
|
void List<T>::selection_sort(ListNodePosi(T) p, int n){
|
||||||
|
ListNodePosi(T) currMax;
|
||||||
|
ListNodePosi(T) tail = p;
|
||||||
|
for (int ix = 0; ix != n; ++ix, tail = tail->succ);
|
||||||
|
|
||||||
|
for (; n != 1; --n) {
|
||||||
|
currMax = selectMax(p, n);
|
||||||
|
swap(currMax->val, tail->prev->val);
|
||||||
|
tail = tail->prev;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void List<T>::insertion_sort(ListNodePosi(T) p, int n){
|
||||||
|
ListNodePosi(T) target;
|
||||||
|
for(int ix = 1; ix != n; ix++){
|
||||||
|
p = p->succ;
|
||||||
|
target = search(p->val, ix, p);
|
||||||
|
insert_before(target->succ, p->val);
|
||||||
|
pop(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//deduplicate & uniquify
|
//deduplicate & uniquify
|
||||||
template <typename T>
|
template <typename T>
|
||||||
int List<T>::deduplicate(){
|
int List<T>::deduplicate(){
|
||||||
|
|||||||
@@ -11,6 +11,9 @@ void test_pop();
|
|||||||
void test_find();
|
void test_find();
|
||||||
void test_deduplicate();
|
void test_deduplicate();
|
||||||
void test_uniquify();
|
void test_uniquify();
|
||||||
|
void test_search();
|
||||||
|
void test_selection_sort();
|
||||||
|
void test_insertion_sort();
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
cout << "Running tests......" << endl;
|
cout << "Running tests......" << endl;
|
||||||
@@ -21,6 +24,9 @@ int main(){
|
|||||||
test_find();
|
test_find();
|
||||||
test_deduplicate();
|
test_deduplicate();
|
||||||
test_uniquify();
|
test_uniquify();
|
||||||
|
test_search();
|
||||||
|
test_selection_sort();
|
||||||
|
test_insertion_sort();
|
||||||
|
|
||||||
cout << "All tests passed." << endl;
|
cout << "All tests passed." << endl;
|
||||||
system("pause");
|
system("pause");
|
||||||
@@ -130,13 +136,64 @@ void test_deduplicate(){
|
|||||||
void test_uniquify(){
|
void test_uniquify(){
|
||||||
List<int> l1;
|
List<int> l1;
|
||||||
int i[] = {5,8,12,14,15,19,20,22,29,36};
|
int i[] = {5,8,12,14,15,19,20,22,29,36};
|
||||||
for (int ix = 0; ix != 11; ix++) {
|
for (int ix = 0; ix != 10; ix++) {
|
||||||
l1.push_back(i[ix]);
|
l1.push_back(i[ix]);
|
||||||
l1.push_back(i[ix]);
|
l1.push_back(i[ix]);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(l1.getSize() == 22);
|
assert(l1.getSize() == 20);
|
||||||
assert(l1.uniquify() == 11);
|
assert(l1.uniquify() == 10);
|
||||||
for (int ix = 0; ix != 11; ix++)
|
for (int ix = 0; ix != 10; ix++)
|
||||||
assert(l1[ix] == i[ix]);
|
assert(l1[ix] == i[ix]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_search(){
|
||||||
|
List<int> l1;
|
||||||
|
int i[] = { 5,8,12,14,15,19,20,22,29,36 };
|
||||||
|
for (int ix = 0; ix != 10; ix++) {
|
||||||
|
l1.push_back(i[ix]);
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(l1.search(8)->val == 8);
|
||||||
|
assert(l1.search(14, 6, l1.last())->val == 14);
|
||||||
|
assert(l1.search(30, 5, l1.last())->val == 29);
|
||||||
|
assert(l1.search(1, 5, l1.last())->val == 14);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_selection_sort(){
|
||||||
|
List<int> l1;
|
||||||
|
int a[] = { 2,9,8,4,1,0,7,6,3,5 };
|
||||||
|
for (int ix = 0; ix != 10; ix++) {
|
||||||
|
l1.push_back(a[ix]);
|
||||||
|
}
|
||||||
|
|
||||||
|
l1.selection_sort(l1.first(), 5);
|
||||||
|
assert(l1[0] == 1);
|
||||||
|
assert(l1[1] == 2);
|
||||||
|
assert(l1[2] == 4);
|
||||||
|
assert(l1[3] == 8);
|
||||||
|
assert(l1[4] == 9);
|
||||||
|
|
||||||
|
l1.selection_sort();
|
||||||
|
for (int ix = 0; ix != 10; ++ix)
|
||||||
|
assert(l1[ix] == ix);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_insertion_sort(){
|
||||||
|
List<int> l1;
|
||||||
|
int a[] = { 2,9,8,4,1,0,7,6,3,5 };
|
||||||
|
for (int ix = 0; ix != 10; ix++) {
|
||||||
|
l1.push_back(a[ix]);
|
||||||
|
}
|
||||||
|
|
||||||
|
l1.insertion_sort(l1.first(), 5);
|
||||||
|
assert(l1[0] == 1);
|
||||||
|
assert(l1[1] == 2);
|
||||||
|
assert(l1[2] == 4);
|
||||||
|
assert(l1[3] == 8);
|
||||||
|
assert(l1[4] == 9);
|
||||||
|
|
||||||
|
l1.insertion_sort();
|
||||||
|
for (int ix = 0; ix != 10; ++ix)
|
||||||
|
assert(l1[ix] == ix);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user