#include<iostream>
template<typename Type>
size_t sequenceSearch(const Type data[], const size_t size, const Type& key){
for (size_t counter(0); counter != size; ++counter){
if (data[counter] == key){
return counter;
}
}
return size;
}
template<typename Type>
size_t binarySearch(const Type data[], const size_t size, const Type& key){
size_t begin(0), end(size - 1), mid;
while (begin < end + 1){
mid = (begin + end) / 2;
if (data[mid] < key){
begin = mid + 1;
}
else if (data[mid] > key){
end = mid - 1;
}
else{
return mid;
}
}
return size;
}
template<typename Type>
void bubbleSort(Type data[], const size_t size){
size_t i, j;
for (j = 0; j != size; ++j){
for (i = 0; i != size - 1 - j; ++i){
if (data[i] > data[i + 1]){
std::swap(data[i], data[i + 1]);
}
}
}
}
template<typename Type>
void selectSort(Type data[], const size_t size){
size_t i, j, pos;
for (i = 0; i != size - 1; i++){
pos = i;
for (j = i + 1; j != size; ++j){
if (data[pos] > data[j]){
pos = j;
}
}
if (pos != i){
std::swap(data[pos], data[i]);
}
}
}
template<typename Type>
void insertSort(Type data[], const size_t size){
size_t i, j;
Type temp;
for (i = 1; i != size; ++i){
if (data[i - 1] > data[i]){
temp = data[i];
data[i] = data[i - 1];
for (j = i - 1; temp < data[j] && j < size; --j){
data[j + 1] = data[j];
}
data[j + 1] = temp;
}
}
}
template<typename Type>
void binaryInsertSort(Type data[], const size_t size){
size_t i, j;
size_t begin, end, mid;
Type temp;
for (i = 1; i != size; ++i){
if (data[i - 1] > data[i]){
temp = data[i];
begin = 0;
end = i - 1;
while (begin < end + 1){
mid = (begin + end) / 2;
if (data[mid] < temp){
begin = mid + 1;
}
else{
end = mid - 1;
}
}
for (j = i; j != begin; --j){
data[j] = data[j - 1];
}
data[begin] = temp;
}
}
}
template<typename Type>
void shellSortUnit(Type data[], const size_t size, const size_t step = 1){
size_t i, j;
Type temp;
for (i = step; i < size; ++i){
if (data[i - step] > data[i]){
temp = data[i];
for (j = i - step; temp < data[j] && j < size; j -= step){
data[j + step] = data[j];
}
data[j + step] = temp;
}
}
}
template<typename Type>
void shellSort(Type data[], const size_t size){
for (size_t step(size / 2); step != 0; step /= 2){
shellSortUnit(data, size, step);
}
}
template<typename Type>
void quickSort(Type data[], const size_t begin, const size_t end){
if (begin < end){
int left(begin), right(end);
Type temp(data[begin]);
while (left < right){
while (left < right && data[right] >= temp){
--right;
}
data[left++] = data[right];
while (left < right && data[left] <= temp){
++left;
}
data[right--] = data[left];
}
data[left] = temp;
quickSort(data, begin, left - 1);
quickSort(data, left + 1, end);
}
}
08-27
08-27
08-27