const int MAX_ITEMS = 100;
enum RelationType {LESS, EQUAL, GREATER};
class ItemType {
public:
RelationType CompareTo(ItemType other) const;
void print() const;
void Initialize(int number);
private:
int value;
};
#include "ItemType.h"
#include <iostream>
RelationType ItemType::CompareTo(ItemType other) const {
if (value < other.value)
return LESS;
else if (value == other.value)
return EQUAL;
else
return GREATER;
}
void ItemType::print() const {
std::cout << value << std::endl;
}
void ItemType::Initialize(int number) {
value = number;
}
#include "ItemType.h"
#define MAX_ITEMS 100
class UnsortedType {
public:
UnsortedType();
void MakeEmpty();
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
bool IsFull();
int LengthIs();
void RetrieveItem(ItemType& item, bool& found);
void ResetList();
void GetNextItem(ItemType &item);
private:
int length;
ItemType info[MAX_ITEMS];
int currentPos;
};
#include "unsorted.h"
UnsortedType::UnsortedType() {
length = 0;
}
void UnsortedType::MakeEmpty() {
length = 0;
}
void UnsortedType::InsertItem(ItemType item) {
info[length] = item;
length++;
}
void UnsortedType::DeleteItem(ItemType item) {
int location = 0;
while (item.ComparedTo(info[location]) != EQUAL) //item과 info[location]이 다르다면
location++; //location 업데이트
info[location] = info[length - 1]; //뒤에 있는 애 아무나 끌어다 쓰기
length--;
}
bool UnsortedType::IsFull() {
return (length == MAX_ITEMS);
}
int UnsortedType::LengthIs() {
return length;
}
void UnsortedType::RetrieveItem(ItemType& item, bool& found) {
found = false;
int location = 0;
bool moreToSearch = (location < length);
while (moreToSearch && !found) {
switch (item.ComparedTo(info[location])) {
case LESS:
case GREATER:
location++;
moreToSearch = (location < length);
case EQUAL:
found = true;
item = info[location];
}
}
}
void UnsortedType::ResetList() {
currentPos = 0;
}
void UnsortedType::GetNextItem(ItemType &item) {
item = info[currentPos];
currentPos++;
}
#include "ItemType.h"
#define MAX_ITEMS 100
class SortedType {
public:
SortedType();
void MakeEmpth();
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
bool IsFull();
int LengthIs();
void RetrieveItem(ItemType& item, bool& found);
void ResetList();
void GetNextItem(ItemType& item);
private:
int length;
ItemType info[MAX_ITEMS];
int currentPos;
};
#include "Sorted.h"
SortedType::SortedType() {
length = 0;
}
void SortedType::MakeEmpth() {
length = 0;
}
void SortedType::InsertItem(ItemType item) {
int location = 0;
bool moreToSearch = (location < length);
while (moreToSearch) {
switch (item.ComparedTo(info[location])) {
case LESS:
moreToSearch = false;
case EQUAL:
location++;
moreToSearch = (location < length);
}
}
for (int index = length; index > location; index--) {
info[index] = info[index - 1];
}
info[location] = item;
length++;
}
void SortedType::DeleteItem(ItemType item) {
int location = 0;
while (item.ComparedTo(info[location]) != EQUAL)
location++;
for (int index = location + 1; index < length; index++) {
info[index - 1] = info[index];
length--;
}
}
bool SortedType::IsFull() {
return(length == MAX_ITEMS);
}
int SortedType::LengthIs() {
return length;
}
void SortedType::RetrieveItem(ItemType& item, bool& found) {
int first = 0;
int last = length - 1;
int mid;
bool moreToSearch = (first < last);
found = false;
while (moreToSearch && !found) {
mid = (first + last) / 2;
switch (item.ComparedTo(info[mid])) {
case LESS:
last = mid - 1;
moreToSearch = (first < last);
case EQUAL:
found = true;
case GREATER:
first = mid + 1;
moreToSearch = (first < last);
}
}
}
void SortedType::ResetList() {
currentPos = 0;
}
void SortedType::GetNextItem(ItemType& item) {
item = info[currentPos];
currentPos++;
}
데이터가 n개 있을 때 Operation이 수행되는 차수
최악의 경우로 따짐
가장 빠르게 증가하는 Size로 표현
Names of Orders of Magnitude
Big-O Comparison of List Operation
Operation | UnsortedType | SortedType |
---|---|---|
Constructor | O(1) | O(1) |
MakeEmpty | O(1) | O(1) |
InsertItem | O(1) | O(N) |
DeleteItem | O(N) | O(N) |
IsFull | O(1) | O(1) |
LengthIs | O(1) | O(1) |
RetrieveItem | O(N) | O(log_2(N)) |
ResetList | O(1) | O(1) |
GetNextItem | O(1) | O(1) |