这个排序本质上是一个链表排序,没有算法,就是直接排序。但是由于类的特性,在排序时可以保留相关信息。并且可以按要求的比较方式排序。
换句话说,就是将一组任意类型的信息按照指定的方式进行排序。
例子:将一个整型数组排序,排序按照到10的绝对值大小。
主函数:
void main() {
int data[10] = { 9,1,3,5,4,8,6,7,8,9 };
for (int i = 0; i < 10; i++) {
cout << data[i] << " ";
}
cout << endl;
reorder<int, int>(data, 10);
for (int i = 0; i < 10; i++) {
cout << data[i] << " ";
}
cout << endl;
system("pause");
return;
}
其中,reorder<class tocompare, class data>,tocompare是要用来进行比较从而排序的数据的类型,data是信息的类型。
这里默认排序的数据由信息得出。
如果不是的话可以自行修改排序函数的参数,通过参数传入。并且修改node类的数据成员。然后将reorder函数中调用whatever_tocompare部分改为要比较的表达式。(
请注意表达式的值与最后排序结果的关系(比如:在比较中用<进行比较,最后的排序顺序则是<规则的升序排列)。不清楚可以看一下样例程序及其输出。
排序依据的数值:
template<class data,class tocompare >
tocompare whatever_tocompare(data input) {
return abs(input-10);
//here write your function which get the value used to compare
}
排序方式的函数:
template<class tocompare>
bool compare(const tocompare &left, const tocompare &right) {
return left<right;
//here write your compare function
//the new order of array will be ascending sort
//for example, if what you write here is:
//return left<right;
//and your tocompare is int type
//then the final array will be in a ascending order
}
运行结果:
全部代码如下:
template<class data, class tocompare >
tocompare whatever_tocompare(data input) {
//here write your function which get the value used to compare
//for example
//return input;
}
template<class data, class tocompare>
class node {
public:
tocompare C;
data mdata;
node *next;
node() = default;
node(data d) :mdata(d), C(whatever_tocompare<data, tocompare>(d)) { next = nullptr; }
node& operator=(const node &ro) {
mdata = ro.mdata; next = ro.next;
C = ro.C;
//here can add other type data
}
node(const node &ro) {
mdata = ro.mdata; C = ro.C; next = nullptr;
//here can add other type data
}
};
typedef node<class data, class tocompare>* nodep;
template<class tocompare>
bool compare(const tocompare &left, const tocompare &right) {
//here write your compare function
//the new order of array will be ascending sort
//for example, if what you write here is:
//return left<right;
//and your tocompare is int type
//then the final array will be in a ascending order
}
template<class tocompare, class data>
void reorder(data deposit[], int sizeofarray) {
int i;
node< data, tocompare> *head = new node< data, tocompare>(deposit[0]);//create the head node
node< data, tocompare> *p1, *b1; b1 = head;
for (i = 1; i < sizeofarray; i++) {//create the list
p1 = new node< data, tocompare>(deposit[i]);
//if you have some conditions in which the node should't be add into list, write it here
b1->next = p1;
b1 = p1;
}
//the reorder operation
int num = sizeofarray;
node< data, tocompare> **arr = new node< data, tocompare> *[num];
node< data, tocompare> *b, *p, **t, **todelete;
t = new node< data, tocompare> *;//这里只是为了堵住编译器的嘴
todelete = t;
b = p = head;
for (i = 0; i < num; i++) {
arr[i] = p;
p = p->next;
}
for (int j = 0; j < num; j++) {
tocompare temp;
for (int i = j; i < num; i++) {
if (i == j) {
temp = arr[i]->C;
t = &arr[i];
}
if (compare(arr[i]->C, temp)) {
temp = arr[i]->C;
t = &arr[i];
}
}
node< data, tocompare> *lin = arr[j];
arr[j] = *t;
*t = lin;
}
head = arr[0];
b = head;
for (i = 1; i < num; i++) {
b->next = arr[i];
b = arr[i];
}
b->next = nullptr;
//refill the array
p1 = head;
for (i = 0; i < sizeofarray; i++) {
deposit[i] = p1->mdata;
p1 = p1->next;
}
//clean up
delete todelete; node< data, tocompare> *helper;
for (i = 0; i < num; i++) {
helper = head->next;
delete head;
head = helper;
}
}