bar*_*kyo 4 c arrays sorting structure quicksort
我试图找出(使用快速排序算法)如何通过2个标准对结构数组进行排序.比如说我有一个结构:
struct employee{
char gender[12];
char name[12];
int id;
};
Run Code Online (Sandbox Code Playgroud)
说我的输入是:
struct employee arr[3]=
{
{"male","Matt",1234},
{"female","Jessica",2345},
{"male","Josh",1235}
};
Run Code Online (Sandbox Code Playgroud)
我想首先按性别对元素进行排序,然后按升序对ID进行排序.一个例子是,所有的男性首先打印出他们的ID,然后是所有的女性.我试图这样做而不使用qsort函数,但我没有丝毫想知道如何检查.这是我的排序功能:
void quicksort(struct employee *arr, int left, int right)
{
int pivot, l, r, temp;
if(left < right)
{
p = left;
l = left;
r = right;
while(l < r)
{
while(arr[l].id <= arr[p].id && l <= right)
l++;
while(arr[r].id > arr[p].id && r >= left)
r--;
if(l < r)
{
temp = arr[l].id;
arr[l].id = arr[r].id;
arr[r].id = temp;
}
}
temp = arr[r].id;
arr[r].id = arr[p].id;
arr[p].id = temp;
quicksort(arr, left, r-1);
quicksort(arr, r+1, right);
}
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?我以为我可以使用strcmp,但我无法弄清楚在函数中包含它的位置.
你当然可以内联比较函数,以及交换器.下面的代码是非常基本的,依赖于有效的指针,但你会明白这一点.我也冒昧地削减了你的快速排序,修复了一路上的事情(我希望).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// employee record
struct employee
{
char gender[12];
char name[12];
int id;
};
// swap employee records
void swap_employee(struct employee *left, struct employee *right)
{
struct employee tmp = *right;
*right = *left;
*left = tmp;
}
// compare employee records
int compare_employee(const struct employee* left,
const struct employee* right)
{
int gender = strcmp(left->gender, right->gender);
return (gender ? gender : (left->id - right->id));
}
// quicksort for employees
static void quicksort_(struct employee *arr, int left, int right)
{
struct employee p = arr[(left+right)/2]; // as good as any
int l = left, r = right; // movable indicies
while (l <= r)
{
while (compare_employee(arr+l, &p) < 0)
++l;
while (compare_employee(arr+r, &p) > 0)
--r;
if (l <= r)
{
swap_employee(arr+l, arr+r);
++l; --r;
}
}
if (left < r)
quicksort_(arr, left, r);
if (l < right)
quicksort_(arr, l, right);
}
// exposed API
void quicksort(struct employee *arr, int count)
{
if (arr && (count>0))
quicksort_(arr, 0, count-1);
}
/* sample usage */
int main(int argc, char *argv[])
{
struct employee arr[]=
{
{"male","Matt",1234},
{"female","Jessica",2345},
{"male","Josh",1235},
{"female","Betsy",2344},
{"male","Roger",1233}
};
quicksort(arr, sizeof(arr)/sizeof(arr[0]));
for (int i=0;i<sizeof(arr)/sizeof(arr[0]);++i)
printf("%s, %s, %d\n", arr[i].gender,arr[i].name, arr[i].id);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
结果
female, Betsy, 2344
female, Jessica, 2345
male, Roger, 1233
male, Matt, 1234
male, Josh, 1235
Run Code Online (Sandbox Code Playgroud)