快排算法代码

/*************************************************************************
 @Author: wanghao
 @Created Time : Sat 12 May 2018 02:22:56 AM PDT
 @File Name: qsort.c
 @Description:
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>


#define MAXSIZE 10
typedef int data_t;


typedef struct node
{
data_t data[MAXSIZE];
int length;
}Node;


Node *create_node(void)
{
Node *node = NULL;


node = (Node *)malloc(sizeof(node));
if(node == NULL)
{
printf("Malloc node fail!\n");
return NULL;
}


node->length = 0;
return node;
}


int quicksort(Node *seqlist, int low, int high)
{
int i, j;
int key, temp;


i = low;
j = high;


if(i >= j)
{
return;
}


key = seqlist->data[low];
while(i < j)
{
while((key <= seqlist->data[j]) && (i < j))
{
j--;
}


seqlist->data[i] = seqlist->data[j];


while((key >= seqlist->data[i]) && (i < j))
{
i++;
}


seqlist->data[j] = seqlist->data[i];
}
seqlist->data[i] = key;


quicksort(seqlist, low, i-1);
quicksort(seqlist, i+1, high);


}
int print_node(Node *seqlist)
{
int i;


if(!seqlist)
{
printf("The node does not exist!\n");
return -1;
}


for(i = 0; i < seqlist->length; i++)
{
printf("%d\t",seqlist->data[i]);
}
printf("\n");


return 0;
}


int main(int argc, const char *argv[])
{
int i;
Node *phead = NULL;


phead = create_node();
if(phead == NULL)
{
return -1;
}
srand(time(NULL));


for(i = 0; i< MAXSIZE; i++)
{
phead->data[phead->length] = rand() % 100 + 1;
phead->length++;
}


print_node(phead);
quicksort(phead, 0, phead->length-1);
print_node(phead);


return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42048417/article/details/80328327
今日推荐