单向链表快速排序

#include <iostream>
#include <time.h>

using namespace std;

typedef class node
{
public:
    int value;
    node *next;
} * nodep;

nodep linkedpartion(nodep left,nodep right)
{
    nodep p = left;
    nodep q = left->next;
    int tmp = left->value;
    while(q != right)
    {
        if(q->value < tmp)
        {
            p = p->next;
            swap(p->value,q->value);
        }
        q = q->next;
    }
    swap(left->value,p->value);
    return p;
}

void quickpass(nodep left,nodep right)
{
    if(left != right)
    {
        nodep tmp = linkedpartion(left,right);
        quickpass(left,tmp);
        quickpass(tmp->next,right);
    }
}

void quicksort(nodep head)
{
    quickpass(head->next,NULL);
}

int main()
{
    srand((unsigned int)time(NULL));
    nodep head = new node;
    nodep p = head;
    int n = rand()%20;
    for (int i = 0; i < n; i++)
    {
        p->next = new node;
        p = p->next;
        p->value = rand() % 100;
    }
    p->next = NULL;
    quicksort(head);
    nodep point = head->next;
    while(point != NULL)
    {
        cout << point->value << endl;
        point = point->next;
    }
}

版权声明:本文为MoriingRain原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。