C 在链表上快速排序,如何使此代码使用第一个元素作为轴心,而不是最后一个元素
为了使这段代码能够使用第一个元素作为轴心而不是最后一个元素,我必须实现哪些改变 我所采取的任何方法都不起作用,任何帮助都将不胜感激,谢谢C 在链表上快速排序,如何使此代码使用第一个元素作为轴心,而不是最后一个元素,c,linked-list,quicksort,C,Linked List,Quicksort,为了使这段代码能够使用第一个元素作为轴心而不是最后一个元素,我必须实现哪些改变 我所采取的任何方法都不起作用,任何帮助都将不胜感激,谢谢 struct Node* partition(struct Node *l, struct Node *h) { // set pivot as h element int x = h->data; // similar to i = l-1 for array implementation struct No
struct Node* partition(struct Node *l, struct Node *h)
{
// set pivot as h element
int x = h->data;
// similar to i = l-1 for array implementation
struct Node *i = l->prev;
// Similar to "for (int j = l; j <= h- 1; j++)"
for (struct Node *j = l; j != h; j = j->next)
{
if (j->data <= x)
{
// Similar to i++ for array
i = (i == NULL) ? l : i->next;
swap(&(i->data), &(j->data));
}
}
i = (i == NULL) ? l : i->next; // Similar to i++
swap(&(i->data), &(h->data));
return i;
}
/* A recursive implementation of quicksort for linked list */
void _quickSort(struct Node* l, struct Node *h)
{
if (h != NULL && l != h && l != h->next)
{
struct Node *p = partition(l, h);
_quickSort(l, p->prev);
_quickSort(p->next, h);
}
}
// The main function to sort a linked list.
// It mainly calls _quickSort()
void quickSort(struct Node *head)
{
// Find last node
struct Node *h = lastNode(head);
// Call the recursive QuickSort
_quickSort(head, h);
}
// set pivot as h element
int x = h->data;
struct Node*分区(struct Node*l,struct Node*h)
{
//将轴设置为h元素
INTX=h->数据;
//类似于阵列实现的i=l-1
结构节点*i=l->prev;
//类似于“for(int j=l;j
将pivot设置为low元素,即第一个元素,如下所示:
int x = l->data;
这与我用示例代码(使用第一个节点作为轴心)回答的问题非常相似。