欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

结构体排序

程序员文章站 2024-03-21 19:20:28
...

postgres中,需要修改一个sort方法,把源码中的快速排序改成能够加速的版本,把结构体中的key值搞出来,并且记录下标,把原来的sorttuple数组拷贝,然后按照排序过后的下标进行重新拷贝。

// qsort 的魔派、.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
using namespace std;
#define Datum unsigned __int64

typedef struct SortTuple 
{
	Datum datum1;
	char val;
}SortTuple;



static void quicksort(Datum *tuple_keys, int *indexs, size_t low, size_t high)//快速排序  kpu核函数
{
	if (low < high) 
	{
		size_t i = low, j = high;
		Datum privos = tuple_keys[low];//基准值
		int index = indexs[low];//基准值
		while (i < j) 
		{
			while (i<j&&tuple_keys[j]>=privos) j--;
			if (i < j) 
			{
				tuple_keys[i] = tuple_keys[j];
				indexs[i] = indexs[j];
			}
			while (i<j&&tuple_keys[i]<privos) i++;
			if (i < j) 
			{
				tuple_keys[j] = tuple_keys[i];
				indexs[j] = indexs[i];
			}
			
		}
		tuple_keys[i] = privos;
		indexs[i] = index;
		quicksort(tuple_keys, indexs,low,i);
		quicksort(tuple_keys, indexs, i+1, high);
		
	}
}

static void Dblib_qsort(SortTuple *a, size_t n)
{
	Datum *tuple_keys = (Datum *)malloc(n * sizeof(Datum));//保存sorttuple索引的数组
	int *indexs = (int*)malloc(n * sizeof(int));
	SortTuple *pm = a;
	for (size_t i = 0; i < n; i++)
	{
		tuple_keys[i] = pm->datum1;
		indexs[i] = i;
		pm++;
	}
	quicksort(tuple_keys, indexs, 0, n - 1);
	/*
	for (size_t i = 0; i < n; i++) 
	{
		cout<< tuple_keys [i]<<" ";
	}cout << endl;
	*/
	
	SortTuple *temp= (SortTuple*)malloc(n*sizeof(SortTuple));//拷贝暂存
	memcpy(temp,a, n * sizeof(SortTuple));//拷贝之后重新赋值
	for (size_t i = 0; i < n; i++) 
	{
		memcpy(&a[i],&temp[indexs[i]],sizeof(SortTuple));
	}
	
	
	free(temp);
	temp = NULL;
}


void init(SortTuple **a, size_t *n)
{
	cout<<"please input num of sorttuple"<<endl;
	int t;
	cin >> t;
	*n = t;
	*a = (SortTuple*)malloc((*n)*sizeof(SortTuple));
	//cout<<*n<<endl;
	for (size_t i = 0; i < (*n); i++)
	{
		Datum d; char c;
		cout << "please input datum1 of sorttuple:";
		cin >> d;
		cout << "please input value of sorttuple:";
		cin >> c;
		(*a)[i].datum1 = d; (*a)[i].val = c;//*a是指针
		cout << (*a)[i].val <<endl;
	}
}
void print(SortTuple *a, size_t n)
{
	cout<<"print value of sorttuple"<<endl;
	for (size_t i=0;i<n;i++)
	{
		cout<<a[i].datum1<<" "<<a[i].val<<endl;
	}cout << endl;
}
int main()
{
	SortTuple *a;
	size_t n=0;
	init(&a,&n);
	
	Dblib_qsort(a, n);

	print(a, n);
    std::cout << "Hello World!\n"; 
	//return 0;
}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门提示: 
//   1. 使用解决方案资源管理器窗口添加/管理文件
//   2. 使用团队资源管理器窗口连接到源代码管理
//   3. 使用输出窗口查看生成输出和其他消息
//   4. 使用错误列表窗口查看错误
//   5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
//   6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
please input num of sorttuple
5
please input datum1 of sorttuple:7
please input value of sorttuple:c
c
please input datum1 of sorttuple:3
please input value of sorttuple:s
s
please input datum1 of sorttuple:3
please input value of sorttuple:n
n
please input datum1 of sorttuple:7
please input value of sorttuple:a
a
please input datum1 of sorttuple:0
please input value of sorttuple:m
m
print value of sorttuple
0 m
3 s
3 n
7 c
7 a

Hello World!