-
#include<stdio.h>
-
#include<stdlib.h>
-
-
typedef struct list{
-
int data;
-
struct list *next;
-
}list;
-
-
void swap(int *a,int *b)
-
{
-
int temp=*a;
-
*a=*b;
-
*b=temp;
-
return;
-
}
-
-
list* partition(list *head, list *tail)
-
{
-
list *i=head;
-
list *j=head->next;
-
int mid=head->data;
-
while(j!=tail)
-
{
-
if(mid > j->data)
-
{
-
i=i->next;
-
swap(&(i->data),&(j->data));
-
}
-
j=j->next;
-
}
-
swap(&(i->data),&(head->data));
-
return i;
-
}
-
-
void quicksort(list *head, list*tail)
-
{
-
if(head!=tail)
-
{
-
list *mid=partition(head,tail);
-
quicksort(head,mid);
-
quicksort(mid->next,tail);
-
}
-
}
-
-
list *createlist(int n)
-
{
-
int i;
-
list *head=NULL;
-
list *p=NULL;
-
for(i=0;i<n;i++)
-
{
-
list *temp=(list*)malloc(sizeof(list));
-
temp->data=n-i;
-
temp->next=NULL;
-
if(head==NULL)
-
{
-
head=temp;
-
p=temp;
-
}
-
else
-
{
-
p->next=temp;
-
p=p->next;
-
}
-
}
-
return head;
-
}
-
-
void show(list *head)
-
{
-
list *p=head;
-
while(p!=NULL)
-
{
-
printf("%d ",p->data);
-
p=p->next;
-
}
-
printf("\n");
-
return;
-
}
-
-
int main()
-
{
-
list *head=createlist(15);
-
show(head);
-
quicksort(head,NULL);
-
show(head);
- }