C++ 使用模板实现排序类
我昨晚发布了一个关于数组类的帖子,现在我在排序类上遇到了问题。大约一半的函数是由教授或他给我们的算法定义的,但很多定义让我感到困惑。我不知道是什么让他们和上面的有什么不同C++ 使用模板实现排序类,c++,arrays,algorithm,sorting,C++,Arrays,Algorithm,Sorting,我昨晚发布了一个关于数组类的帖子,现在我在排序类上遇到了问题。大约一半的函数是由教授或他给我们的算法定义的,但很多定义让我感到困惑。我不知道是什么让他们和上面的有什么不同 #ifndef SORTS_H #define SORTS_H #include <iostream> #include <string> #include "Array.h" using namespace std; template <class T> void insertionSor
#ifndef SORTS_H
#define SORTS_H
#include <iostream>
#include <string>
#include "Array.h"
using namespace std;
template <class T>
void insertionSort(Array<T> &a);
template <class T>
void selectionSort(Array<T> &a);
template <class T>
void selectionSort(T a[], int n);
template <class T>
void mergesort(T *input, int size);
template <class T>
void mergesort(T *input, int left, int right, T *scratch);
template <class T>
T less(T x, T y);
template <class T>
void mergesort(Array<T> &input, int left, int right, Array<T>&scratch);
template <class T>
void mergesort(Array<T> & input);
Array<int> & random(int n);
template <class T>
void selectionSort(T a[], int n) {
int i, j, tmp;
int min_idx = 0;
for (size_t i = 0; i < n-1; i++) {
min_idx = i;
for (size_t j = i+1; j < n; j++) {
if (a[j] < a[min_idx]) {
min_idx = j;
}
tmp = a[i];
a[i] = a[min_idx];
a[min_idx] = tmp;
}
}
}
template <class T>
void selectionSort(Array<T> &a) {
int tmp;
int min_idx = 0;
for (int i = 0; i < a.getSize() - 1; i++) {
min_idx = i;
for (int j = i + 1; j < a.getSize(); j++) {
if (a[j] < a[min_idx]) {
min_idx = j;
}
tmp = a[i];
a[i] = a[min_idx];
a[min_idx] = tmp;
}
}
}
template <class T>
void insertionSort(Array<T> &a) {
// put your code here
}
template <class T>
bool sorted(Array<T> a) {
for (int i = 1; i < a.getSize(); i++)
if (a[i - 1] > a[i]) return false;
return true;
}
Array<int> & random(int n) {
Array<int> *tmp = new Array<int>(n);
for (int i = 0; i < n; i++)
(*tmp)[i] = rand() % 1000;
return *tmp;
}
template <class T>
T less(T x, T y) {
if (x < y) {
return x;
}
else {
return y;
}
}
template <class T>
void mergesort(T *input, int left, int right, T *scratch) {
if (right == left + 1) {
return;
}
else {
int i = 0;
int length = right - left;
int midpoint_distance = length / 2;
int l = left, r = left + midpoint_distance;
mergesort(input, left, left + midpoint_distance, scratch);
mergesort(input, left + midpoint_distance, right, scratch);
/* merge the arrays together using scratch for temporary storage */
for (i = 0; i < length; i++) {
/* Check to see if any elements remain in the left array; if so,
* we check if there are any elements left in the right array; if
* so, we compare them. Otherwise, we know that the merge must
* use take the element from the left array */
if (l < left + midpoint_distance &&
(r == right || min(input[l], input[r]) == input[l])) {
scratch[i] = input[l];
l++;
}
else {
scratch[i] = input[r];
r++;
}
}
/* Copy the sorted subarray back to the input */
for (i = left; i < right; i++) {
input[i] = scratch[i - left];
}
}
}
template <class T>
void mergesort(T *input, int size) {
int *scratch = new int[size];
mergesort(input, 0, size, scratch);
delete [] scratch;
}
template <class T>
void mergesort(Array<T> &input, int left, int right, Array<T>&scratch) {
if (right == left + 1) {
return;
}
else {
int i = 0;
int length = right - left;
int midpoint_distance = length / 2;
int l = left, r = left + midpoint_distance;
mergesort(input, left, left + midpoint_distance, scratch);
mergesort(input, left + midpoint_distance, right, scratch);
/* merge the arrays together using scratch for temporary storage */
for (i = 0; i < length; i++) {
/* Check to see if any elements remain in the left array; if so,
* we check if there are any elements left in the right array; if
* so, we compare them. Otherwise, we know that the merge must
* use take the element from the left array */
if (l < left + midpoint_distance &&
(r == right || min(input[l], input[r]) == input[l])) {
scratch[i] = input[l];
l++;
}
else {
scratch[i] = input[r];
r++;
}
}
/* Copy the sorted subarray back to the input */
for (i = left; i < right; i++) {
input[i] = scratch[i - left];
}
}
}
template <class T>
void mergesort(Array<T> &input) {
// put your code here
}
#endif
\ifndef排序\u H
#定义排序
#包括
#包括
#包括“Array.h”
使用名称空间std;
模板
void insertionSort(数组&a);
模板
无效选择排序(数组和a);
模板
void selectionSort(T a[],int n);
模板
无效合并排序(T*输入,整数大小);
模板
无效合并排序(T*input,int left,int right,T*scratch);
模板
T小于(tx,ty);
模板
void mergesort(数组和输入、int left、int right、数组和scratch);
模板
无效合并排序(数组和输入);
数组&随机(int-n);
模板
void selectionSort(T a[],int n){
int i,j,tmp;
int min_idx=0;
对于(大小i=0;ia[i])返回false;
返回true;
}
数组和随机数(整数n){
阵列*tmp=新阵列(n);
对于(int i=0;i
我还注意到有一个void insertionSort(Array&a)代码>函数,但给出的算法是:
void insertionSort(int a[], int n){
int tmp;
int i, j;
for (i = 1; i < n; i++) {
tmp = a[i];
for (j = i - 1; j >= 0; j--)
if (a[j] > tmp) a[j + 1] = a[j];
else break;
a[j + 1] = tmp;
}
}
void insertionSort(int a[],int n){
int tmp;
int i,j;
对于(i=1;i=0;j--)
如果(a[j]>tmp)a[j+1]=a[j];
否则就断了;
a[j+1]=tmp;
}
}
我是否应该以同样的方式实现它,只是将inta[]
替换为,比如说<代码>&arr
?我猜,因为这包括array.h
,而array类有T*arr代码>,我应该指向该数组的地址?这是否也适用于在其参数中包含address运算符的每个定义?正如您所说,差异是1,采用典型的int数组a[]
,但您如何知道大小?因此,此版本要求用户将其发送到函数,并将n
作为元素数。在数组
类中,您提供了一个大小,因此不需要它。通常,您为多种情况提供重载
我不知道你说的“替换”是什么意思
template <class T>
Array<T>::Array(const Array &other) : size(other.size), arr(new T[size])
{ // ^^^^^^
for (int i = 0; i < size; ++i)
arr[i] = other.arr[i];
} // ^^^^^
template <class T>
void selectionSort(Array<T> &a) {
// I'm not sure I understand what this is supposed to do that's different from the above selectionSort.
// I know that & is the reference operator, so should I just return whatever &a is?
}