循环则要判别 j - srep < 0的情况 { this->m_num[j] = this->m_num[j - step]; this->m_num[j - step] = tempNum; } }
} } step /= 2; } return this->m_num; } ?
template<class T> T* CDaXiongTemplateSort<T>::MergeSort(T* num, int size, bool ascending = true) {
T* lift, *right; if (size >> 1) { lift = new T[size >> 1]; right = new T[size - (size >> 1)]; for (int i = 0, j = 0; i < size; ++i) { if (i < size >> 1) { lift[i] = num[i]; } else { right[j++] = num[i]; } } ? MergeSort(lift, size >> 1, ascending); MergeSort(right, size - (size >> 1), ascending);
?
? int i, j, k; for (i = 0, j = 0, k = 0; i < size >> 1 && j < size - (size >> 1);) { if (ascending) { if (lift[i] < right[j]) { num[k++] = lift[i++]; } else { num[k++] = right[j++]; } } else { if (lift[i] < right[j]) { num[k++] = right[j++]; } else {
num[k++] = lift[i++]; } }
}
if (i >= size >> 1) { while (j < size - (size >> 1)) { num[k++] = right[j++]; } } else { while (i < size >> 1) { num[k++] = lift[i++]; } } delete[] lift; delete[] right; } ? return num; } ?
template<class T> T* CDaXiongTemplateSort<T>::BucketSort(int n) { T* bucket[11]; int temp; static int count = 0; for (int i = 0;i < 11; ++i) { bucket[i] = new T[this->m_length]; for (int j = 0; j < this->m_length; ++j) { bucket[i][j] = MARK; } }
for (int i = 0, k = 1; i < n; ++i, k *= 10) { for (int j = 0; j < this->m_length; ++j) { if (this->m_num[j] < k * 10 && this->m_num[j] >= k) { temp = this->m_num[j] / k % 10; bucket[temp][j] = this->m_num[j]; } }
for (int m = 0, j = 0; m < 10; ++m) { for (int x = 0; x < this->m_length; ++x) { if (bucket[m][x] != MARK) { bucket[10][count++] = bucket[m][x]; bucket[m][x] = MARK; } } } } ? for (int i = 0; i < this->m_length; ++i) { this->m_num[i] = bucket[10][i]; } ? for (int i = 0; i < 11; ++i) { delete []bucket[i]; bucket[i] = NULL; } return this->m_num; }
|