#include <sorter.h>
Inheritance diagram for ABA_SORTER< ItemType, KeyType >:
Public Member Functions | |
ABA_SORTER (ABA_GLOBAL *glob) | |
void | quickSort (int n, ABA_ARRAY< ItemType > &items, ABA_ARRAY< KeyType > &keys) |
Sorts the elements of an array of n items according to their keys. | |
void | quickSort (ABA_ARRAY< ItemType > &items, ABA_ARRAY< KeyType > &keys, int left, int right) |
void | heapSort (int n, ABA_ARRAY< ItemType > &items, ABA_ARRAY< KeyType > &keys) |
Private Member Functions | |
int | partition (ABA_ARRAY< ItemType > &items, ABA_ARRAY< KeyType > &keys, int left, int right) |
Returns a number q ({{left <= q <= right)} and guarantees that all elements i with {key[i] <= key[q]}} are stored in the left part of the array, i.e., in items[left], , items[q], and all elements j with key[j] > key[q] are stored in the right part of the array, i.e., in items[q+1], ![]() | |
void | buildHeap (int n, ABA_ARRAY< ItemType > &items, ABA_ARRAY< KeyType > &keys) |
Resorts the elements if items and keys such that the heap property holds, i.e., keys[i] >= keys[2*i+1] and keys[i] >= keys[2*i+2]. | |
void | heapify (int n, ABA_ARRAY< ItemType > &items, ABA_ARRAY< KeyType > &keys, int root) |
Assumes that the heap property holds for the subtrees rooted at the sons of root and restores the heap property for the subtree rooted at root. | |
void | check (int n, ABA_ARRAY< ItemType > &items, ABA_ARRAY< KeyType > &keys) |
Is a debugging function and terminates the program with an error message if the elements of items are not sorted by increasing keys. | |
Private Attributes | |
ABA_GLOBAL * | glob_ |
ItemType | itemSwap_ |
KeyType | keySwap_ |
Definition at line 46 of file sorter.h.
ABA_SORTER< ItemType, KeyType >::ABA_SORTER | ( | ABA_GLOBAL * | glob | ) |
The constructor.
glob | A pointer to the corresponding global object. |
void ABA_SORTER< ItemType, KeyType >::quickSort | ( | int | n, | |
ABA_ARRAY< ItemType > & | items, | |||
ABA_ARRAY< KeyType > & | keys | |||
) |
Sorts the elements of an array of n items according to their keys.
This function is very efficient for many practical applications. Yet, has a worst case running time of .
n | The number of elements being sorted. | |
items | The items being sorted. | |
keys | The keys of the sorted items. |
void ABA_SORTER< ItemType, KeyType >::quickSort | ( | ABA_ARRAY< ItemType > & | items, | |
ABA_ARRAY< KeyType > & | keys, | |||
int | left, | |||
int | right | |||
) |
Sorts an partial array.
The function quickSort() uses the divide-and-conquer technique. First the function partition() puts the small elements to the left part and all big elements to the right part of the array being sorted. More precisely, it holds then, keys[i] <= keys[q] for all i in left, q and keys[q] < keys[i] for all i in q+1, right. Hence, it is sufficient to sort these two subarrays recursively.
items | The items being sorted. | |
keys | The keys of the items. | |
left | The first item in the partial array being sorted. | |
right | The last item in the partial array being sorted. |
void ABA_SORTER< ItemType, KeyType >::heapSort | ( | int | n, | |
ABA_ARRAY< ItemType > & | items, | |||
ABA_ARRAY< KeyType > & | keys | |||
) |
Sorts an array of n items according to their keys.
In many practical applications this function is inferior to quickSort(), although it has the optimal worst case running time of .
n | The number of items being sorted. | |
items | The items being sorted. | |
keys | The keys of the items. |
int ABA_SORTER< ItemType, KeyType >::partition | ( | ABA_ARRAY< ItemType > & | items, | |
ABA_ARRAY< KeyType > & | keys, | |||
int | left, | |||
int | right | |||
) | [private] |
Returns a number q ({{left <= q <= right)} and guarantees that all elements i with {key[i] <= key[q]}} are stored in the left part of the array, i.e., in items[left], , items[q], and all elements j with key[j] > key[q] are stored in the right part of the array, i.e., in items[q+1], , items[right].
items | The items being sorted. | |
keys | The keys for sorting the items. | |
left | The left border of the partial array being considered. | |
right | The right border ot the partial array being considered. |
void ABA_SORTER< ItemType, KeyType >::buildHeap | ( | int | n, | |
ABA_ARRAY< ItemType > & | items, | |||
ABA_ARRAY< KeyType > & | keys | |||
) | [private] |
Resorts the elements if items and keys such that the heap property holds, i.e., keys[i] >= keys[2*i+1] and keys[i] >= keys[2*i+2].
n | The number of elements of the following arrays. | |
items | The items being sorted. | |
keys | The keys for sorting the items. |
void ABA_SORTER< ItemType, KeyType >::heapify | ( | int | n, | |
ABA_ARRAY< ItemType > & | items, | |||
ABA_ARRAY< KeyType > & | keys, | |||
int | root | |||
) | [private] |
Assumes that the heap property holds for the subtrees rooted at the sons of root and restores the heap property for the subtree rooted at root.
n | The number of elements of the following arrays. | |
items | The items being sorted. | |
keys | The keys for sorting the items. | |
root | The index where the heaps property has to be restored. |
void ABA_SORTER< ItemType, KeyType >::check | ( | int | n, | |
ABA_ARRAY< ItemType > & | items, | |||
ABA_ARRAY< KeyType > & | keys | |||
) | [private] |
Is a debugging function and terminates the program with an error message if the elements of items are not sorted by increasing keys.
n | The number of elements of the following arrays. | |
items | The items being sorted. | |
keys | The keys for sorting the items. |
ABA_GLOBAL* ABA_SORTER< ItemType, KeyType >::glob_ [private] |
ItemType ABA_SORTER< ItemType, KeyType >::itemSwap_ [private] |
KeyType ABA_SORTER< ItemType, KeyType >::keySwap_ [private] |