快排相关

本文介绍了快速排序算法及其两种变种实现:求第n小的数和单链表快速排序。通过C语言代码详细展示了如何优化快速排序,并提供完整的程序示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【1】快排

(1)个数小的时候用插入排序;

(2)分割的基点用三个点的中位数。

#include <stdio.h>
#include <stdlib.h>
int *__median(int *a, int *b, int *c)
{
    if(*a<*b)
        if(*b<*c)
            return b;
        else
            if(*a<*c)
                return c;
            else
                return a;
    else
        if(*a<*c)
            return a;
        else
            if(*b<*c)
                return c;
            else
                return b;
}

void swap(int *a, int *b)
{
    int t=*a;
    *a=*b;
    *b=t;
}
void insert_sort(int*a, int *b)
{
    int i,j;
    int n=b-a;
    for(i=1;i<n;i++)
    {
        int t=a[i];
        for(j=i-1;j>=0 && a[j]>t;j--)a[j+1]=a[j];
            a[j+1]=t;
    }
}
void qs(int* begin, int *end)
{
    if(end-begin<5)                  // 长度小的时候,用插入排序
    {
        insert_sort(begin,end);
        return;
    }
    int *p_swap=__median(begin, begin+((end-begin)>>1), end-1);  // 中位数
    swap(begin, p_swap);
    int i;
 //  for(i=0;i<end-begin; i++)
   //     printf("%d \n", begin[i]);
    //printf("\n");
    int pivot=*begin;
    int *s=begin;
    int *e=end;
    ++begin;
    while(1)
    {  //begin<e &&
        while( *begin < pivot ){++begin;if(begin>e)printf("eee\n");}
        --end;
        //end>=s &&
        while( *end > pivot)--end;
        if(begin>=end)break;
        
        swap(begin, end);
        ++begin;
    }
    swap(end, s);
    qs(s, end);
    qs(begin, e);
    
}
#define N 14
int main()
{

    int ap[N]={4000,3,1,2};
    int i;
    for(i=3;i<N; i++)
        ap[i]=rand()%1000;
    for(i=0;i<N; i++)
        printf("%d \n", ap[i]);
    printf("\n");
    
    qs(ap,ap+N);
    for(i=0;i<N; i++)
        printf("%d\n", ap[i]);
    printf("\n");
    for(i=0;i<N-1; i++)
        if(ap[i]>ap[i+1])
        {
            printf("error\n");
            break;
        }

    return 0;
}

chen@chen-book1:~$ gcc t.c -o t
chen@chen-book1:~$ ./t
4000
3
1
383
886
777
915
793
335
386
492
649
421
362

1
3
335
362
383
386
421
492
649
777
793
886
915
4000

【2】求nth

#include <stdio.h>
#include <stdlib.h>
int *__median(int *a, int *b, int *c)
{
    if(*a<*b)
        if(*b<*c)
            return b;
        else
            if(*a<*c)
                return c;
            else
                return a;
    else
        if(*a<*c)
            return a;
        else
            if(*b<*c)
                return c;
            else
                return b;
}

void swap(int *a, int *b)
{
    int t=*a;
    *a=*b;
    *b=t;
}
void insert_sort(int*a, int *b)
{
    int i,j;
    int n=b-a;
    for(i=1;i<n;i++)
    {
        int t=a[i];
        for(j=i-1;j>=0 && a[j]>t;j--)a[j+1]=a[j];
            a[j+1]=t;
    }
}


void nth(int* begin, int *end, int *pn)
{
    if(end-begin<5)                  // 长度小的时候,用插入排序
    {
        insert_sort(begin,end);
        return;
    }
    int *p_swap=__median(begin, begin+((end-begin)>>1), end-1);  // 中位数
    swap(begin, p_swap);
    int i;

    int pivot=*begin;
    int *s=begin;
    int *e=end;
    ++begin;
    while(1)
    {  //begin<e &&
        while( *begin < pivot ){++begin;if(begin>e)printf("eee\n");}
        --end;
        //end>=s &&
        while( *end > pivot)--end;
        if(begin>=end)break;
        
        swap(begin, end);
        ++begin;
    }
    
    swap(end, s);
    if(end>=pn)
        nth(s, end, pn);
    else
        nth(end, e, end+(pn-end));
    
}

#define N 14
int main()
{

    int ap[N]={4000,3,1,2};
    int i;
    for(i=3;i<N; i++)
        ap[i]=rand()%1000;
    for(i=0;i<N; i++)
        printf("%d \n", ap[i]);
    printf("\n");
    int x=9;                  // 第10小的数字
    nth(ap,ap+N,ap+x); // 第x+1小的数字
    for(i=0;i<N; i++)
        printf("%d\n", ap[i]);
    printf("\n");
    for(i=0;i<x; i++)
        if(ap[i]>ap[x])
        {
            printf("error\n");
            break;
        }
    for(i=x+1;i<N; i++)
        if(ap[i]<ap[x])
        {
            printf("error\n");
            break;
        }
    return 0;
}

chen@chen-book1:~$ gcc t.c -o t
chen@chen-book1:~$ ./t
4000
3
1
383
886
777
915
793
335
386
492
649
421
362

335
3
1
383
362
386
421
492
649
777                            9
793
4000
915
886

【3】单链表快排

注意:应该交换指针,不能交换值。另外,这里不是用交换的思路来解决的。

参考:http://blog.youkuaiyun.com/yangalbert/article/details/7577782

#include <stdlib.h>
#include <stdio.h>

struct node
{
    int v;
    struct node *next;
};

// 两个子链表。
// 起初:head->f->...; pivot=f->v
// 然后,第一个子链表:head->..->p 比pivot小的数
// 第二个子链表:f->..->q,大于等于pivot的数字

void nsort(struct node *head, struct node*end)
{
    if(head->next==end || head->next->next==end) // 0/1个元素
        return;
        
    struct node *p=head;
    struct node *f=head->next; // fixed
    struct node *q=head->next;
    int pivot=f->v; //fixed
    struct node*t=f->next;
    
    while(t!=end) // 不是while(t)
    {
        if(t->v < pivot)
        {
            p->next=t;
            p=p->next;
        }else
        {
            q->next=t;
            q=q->next;
        }
        t=t->next;
    }
    p->next=f;
    q->next=end;
    
    nsort(head,f);
    nsort(f,end);
}

int main()
{
    struct node head={0, &head};
    int n=10;
    int i;
    struct node *pt=&head;
    struct node *p;
    for(i=0; i<n; i++)
    {
        p=(struct node*)malloc(sizeof( struct node) );
        p->v=rand()%100;
        pt->next=p;
        pt=pt->next;
    }
    pt->next=NULL;
    
    
    ///
    nsort(&head,NULL);
    
    ///
    pt=(&head)->next;
    while(pt)
    {
        printf("%d\n", pt->v);
        pt=pt->next;
    }
    pt=(&head)->next;
    while(pt)
    {
        p=pt;
        pt=pt->next;
        free(p);
    }
    return 0;    
}

chen@chen-book1:~$ gcc node.c -o node -g
chen@chen-book1:~$ ./node
15
21
35
49
77
83
86
86
92
93
chen@chen-book1:~$


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值