数组元素类型定义为Record以方便性能测试
List类和Key类的实现代码放在文章最后
方法清单
- 插入排序 (Insertion Sort)
- 选择排序 (Selection Sort)
- 希尔排序 (Shell Sort)
- 归并排序 (Merge Sort)
- 快速排序 (Quick Sort)
- 堆排序 (Heap Sort)
1. 插入排序 (Insertion Sort)
template<class Record>
void Sortable_list<Record>::insertion_sort()
{
int first_unsorted;
int position;
Record current;
for(first_unsorted=1;first_unsorted< List<Record>::count;first_unsorted++)
if (List<Record>::entry[first_unsorted] < List<Record>::entry[first_unsorted - 1])
{
position = first_unsorted;
current = List<Record>::entry[first_unsorted];
do
{
List<Record>::entry[position] = List<Record>::entry[position - 1];
position--;
} while (position > 0 && List<Record>::entry[position - 1] > current);
List<Record>::entry[position] = current;
}
}
2. 选择排序 (Selection Sort)
template<class Record>
void Sortable_list<Record>::selection_sort()
{
for (int position = List<Record>::count - 1; position > 0; position--)
{
int max = max_key(0, position);
swap(max, position);
}
}
template<class Record>
int Sortable_list<Record>::max_key(int low, int high)
{
int largest, current;
largest = low;
for (current = low + 1; current <= high; current++)
if (List<Record>::entry[largest] < List<Record>::entry[current])
largest = current;
return largest;
}
template<class Record>
void Sortable_list<Record>::swap(int low, int high)
{
Record temp;
temp = List<Record>::entry[low];
List<Record>::entry[low] = List<Record>::entry[high];
List<Record>::entry[high] = temp;
}
3. 希尔排序 (Shell Sort)
template<class Record>
void Sortable_list<Record>::shell_sort()
{
int increment, start;
increment = List<Record>::count;
do
{
increment = increment / 3 + 1;
for (start = 0; start < increment; start++)
sort_interval(start, increment);
} while (increment > 1);
}
template<class Record>
void Sortable_list<Record>::sort_interval(int first_unsorted, int increment)
{
int position;
Record current;
for (first_unsorted; first_unsorted < List<Record>::count; first_unsorted += increment)
if (List<Record>::entry[first_unsorted] < List<Record>::entry[first_unsorted - 1])
{
position = first_unsorted;
current = List<Record>::entry[first_unsorted];
do
{
List<Record>::entry[position] = List<Record>::entry[position - 1];
position--;
} while (position > 0 && List<Record>::entry[position - 1] > current);
List<Record>::entry[position] = current;
}
}
4. 归并排序 (Merge Sort)
template<class Record>
void Sortable_list<Record>::mergesort()
{
int low = 0, high = List<Record>::count - 1;
run_mergesort(List<Record>::entry, low, high);
}
template<class Record>
void Sortable_list<Record>::run_mergesort(Record entry[], int low, int high)
{
int mid;
if (low < high)
{
mid = (low + high) / 2;
run_mergesort(List<Record>::entry, low, mid);
run_mergesort(List<Record>::entry, mid + 1, high);
merge(List<Record>::entry, low, mid, high);
}
}
template<class Record>
void Sortable_list<Record>::merge(Record entry[], int low, int mid, int high)
{
int i = low, j = mid + 1, k = 0;
int* temp = new int[high - low + 1];
while (i <= mid && j <= high) {
if (List<Record>::entry[i] <= List<Record>::entry[j])
temp[k++] = List<Record>::entry[i++];
else
temp[k++] = List<Record>::entry[j++];
}
while (i <= mid)
temp[k++] = List<Record>::entry[i++];
while (j <= high)
temp[k++] = List<Record>::entry[j++];
for (i = low, k = 0; i <= high; i++, k++)
List<Record>::entry[i] = temp[k];
delete[]temp;
}
5. 快速排序 (Quick Sort)
template<class Record>
void Sortable_list<Record>::quick_sort()
{
recursive_quick_sort(0, List<Record>::count - 1);
}
template <class Record>
void Sortable_list<Record>::recursive_quick_sort(int low, int high)
{
int pivot_position;
if (low < high)
{
pivot_position = partition(low, high);
recursive_quick_sort(low, pivot_position - 1);
recursive_quick_sort(pivot_position + 1, high);
}
}
template<class Record>
int Sortable_list<Record>::partition(int low, int high)
{
Record pivot;
int i, last_small;
swap(low, (low + high) / 2);
pivot = List<Record>::entry[low];
last_small = low;
for (i = low + 1; i <= high; i++)
{
if (List<Record>::entry[i] < pivot)
{
last_small += 1;
swap(last_small, i);
}
}
swap(low, last_small);
return last_small;
}
6. 堆排序 (Heap Sort)
template<class Record>
void Sortable_list<Record>::heap_sort()
{
Record current;
int last_unsorted;
build_heap();
for (last_unsorted = List<Record>::count - 1; last_unsorted > 0; last_unsorted--)
{
current = List<Record>::entry[last_unsorted];
List<Record>::entry[last_unsorted] = List<Record>::entry[0];
insert_heap(current, 0, last_unsorted - 1);
}
}
template<class Record>
void Sortable_list<Record>::insert_heap(const Record& current, int low, int high)
{
int large;
large = 2 * low + 1;
while (large <= high)
{
if (large < high and List<Record>::entry[large] < List<Record>::entry[large + 1])
large++;
if (current >= List<Record>::entry[large])
break;
else
{
List<Record>::entry[low] = List<Record>::entry[large];
low = large;
large = 2 * low + 1;
}
}
List<Record>::entry[low] = current;
}
template<class Record>
void Sortable_list<Record>::build_heap()
{
int low;
for (low = List<Record>::count / 2 - 1; low >= 0; low--)
{
Record current = List<Record>::entry[low];
insert_heap(current, low, List<Record>::count - 1);
}
}
Key类
重载运算符的目的是便于客户进行比较操作
comparisons用于统计比较次数
class Key
{
int key;
public:
static int comparisons;
Key(int x = 0);
int the_key() const;
};
bool operator == (const Key& x, const Key& y);
bool operator > (const Key& x, const Key& y);
bool operator < (const Key& x, const Key& y);
bool operator >= (const Key& x, const Key& y);
bool operator <= (const Key& x, const Key& y);
bool operator != (const Key& x, const Key& y);
Key::Key(int x)
{
key = x;
}
int Key::the_key() const
{
return key;
}
bool operator == (const Key& x, const Key& y)
{
Key::comparisons++;
return x.the_key() == y.the_key();
}
bool operator > (const Key& x, const Key& y)
{
Key::comparisons++;
return x.the_key() > y.the_key();
}
bool operator < (const Key& x, const Key& y)
{
Key::comparisons++;
return x.the_key() < y.the_key();
}
bool operator >= (const Key& x, const Key& y)
{
Key::comparisons++;
return x.the_key() >= y.the_key();
}
bool operator <= (const Key& x, const Key& y)
{
Key::comparisons++;
return x.the_key() <= y.the_key();
}
bool operator != (const Key& x, const Key& y)
{
Key::comparisons++;
return x.the_key() != y.the_key();
}
// Implementation of class Key
int Key::comparisons = 0;
typedef Key Record;