// test9.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL){
}
};
void swap(int &a,int &b)
{
int temp = a;
a = b;
b = temp;
}
ListNode * partion(ListNode *begin, ListNode *end)
{
int key = begin->val;
ListNode *p = begin;
ListNode *q = p->next;
while(q != end)
{
if(q->val < key)
{
swap(p->val,q->val);
p = p->next;
}
q = q->next;
}
p->val = key;
return p;
}
void qSort(ListNode *begin,ListNode *end)
{
if(begin != end)
{
ListNode *p = partion(begin,end);
qSort(begin,p);
if(p->next != NULL)
qSort(p->next,end);
}
}
int main(int argc, char* argv[])
{
ListNode *root = new ListNode(1);
ListNode *p = new ListNode(9);
root->next = p;
p->next = new ListNode(8);
p = p->next;
p->next = new ListNode(18);
qSort(root,NULL);
while(root)
{
cout<<root->val<<" ";
root = root->next;
}
return 0;
}