C语言实现数组及链表的快速排序

1. 数组快排:

 方法一:

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

//交换
void swap(int *pi, int *pj)
{
    int temp = *pi;
    *pi = *pj;
    *pj = temp;
}

//显示数组
void show(int *p, int n)
{
    printf("\n此时数组为:");
    for (int i = 0; i < n; i++)
    {
        printf("%4d", p[i]);
    }
}

//快排(双冒泡)
//方法一:
void quickSort(int *arr, int iLeft, int iRight)
{
    int i = iLeft;         //从左边开始循环
    int j = iRight + 1;    //从右边开始循环(为什么+1呢?)为了让i<j则成立,i>=j就终止

    if (i < j)
    {

        do
        {
            do
            {
                i++;

            } while (arr[i] <= arr[iLeft] && i <= iRight);
            //最左边第一个大于它的数


            do
            {
                j--;

            } while (arr[j] >= arr[iLeft] && j > iLeft);//这里不能为j >= iLeft,因为取最左边为边界
            //最右边第一个小于它的数


            if (i < j)
            {
                swap(&arr[i], &arr[j]);    //交换
            }

            printf("\n枢轴为:(%d)", arr[iLeft]);
            show(arr, 10);

        } while (i<j);

        swap(&arr[iLeft], &arr[j]);
        show(arr, 10);
        printf("\n\n---------------------------------------------------");

        quickSort(arr, iLeft, j - 1);     //分割左边
        quickSort(arr, j + 1, iRight);    //分割右边
    }
}

void main()
{
    int num[10] = { 10,9,20,19,13,8,9,22,0,91 };
    printf("\n排序前:");
    show(num, 10);
    printf("\n\n");

    quickSort(num, 0, 10 - 1);

    printf("\n排序后:");
    show(num, 10);

    system("pause");
}

  运行结果如下:

方法二:双指针错开

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

//交换
void swap(int *pi, int *pj)
{
    int temp = *pi;
    *pi = *pj;
    *pj = temp;
}

//显示数组
void show(int *p, int n)
{
    printf("\n此时数组为:");
    for (int i = 0; i < n; i++)
    {
        printf("%4d", p[i]);
    }
}

//方法二:适用于链表的快排
int partition(int *arr, int iLeft, int iRight)    //将数组进行分割
{
    if (iLeft == iRight)
        return 0;

    int key = arr[iLeft];
    int i = iLeft;
    int j = iLeft + 1;

    while (j < (iRight + 1))
    {
        if (arr[j] < key)
        {
            ++i;
            swap(&arr[i], &arr[j]);
        }
        ++j;

        printf("\n枢轴为:(%d)", arr[iLeft]);
        show(arr, 10);
    }

    swap(&arr[i], &arr[iLeft]);
    show(arr, 10);
    printf("\n\n---------------------------------------------------");

    return i;
}

void QuickSort(int *arr, int iLeft, int iRight)
{
    if (iLeft < iRight)
    {
        int split = partition(arr, iLeft, iRight);

        QuickSort(arr, iLeft, split);          //分割左边
        QuickSort(arr, split + 1, iRight);     //分割右边
    }
}

void main()
{
    int num[10] = { 10,9,20,19,13,8,9,22,0,91 };
    printf("\n排序前:");
    show(num, 10);
    printf("\n\n");

    QuickSort(num, 0, 10 - 1);

    printf("\n排序后:");
    show(num, 10);

    system("pause");
}

   运行结果如下:

2. 链表快排:

Node *fen(Node *pbegin, Node *pback)
{
    int key = pbegin->data;        //以第一个数据为分段

    Node *p = pbegin;             //第一个节点
    Node *q = pbegin->pNext;      //第二个节点

    while (q != pback)
    {
        if (q->data < key)
        {
            p = p->pNext;        //循环下一个节点

            int temp = p->data;  //交换
            p->data = q->data;
            q->data = temp;
        }
        q = q->pNext;            //循环第二个指针

        printf("\n枢轴为:(%d)", key);
        printf("\n此时数:");
        ShowAll(pbegin);

    }

    int temp = p->data;         //交换
    p->data = pbegin->data;
    pbegin->data = temp;

    printf("\n\n交换值:");
    ShowAll(pbegin);
    printf("\n-----------------------------------------------");

    return p;
}

//快速排序法:(双冒泡)
void quickSort(Node *pbegin,Node *pback)
{
    if (pbegin != pback)
    {
        Node *pfen = fen(pbegin, pback);    //取中间点,分成两段分别再进行快排

        quickSort(pbegin, pfen);            //前半段快排
        quickSort(pfen->pNext, pback);      //后半段快排
    }
}

猜你喜欢

转载自www.cnblogs.com/si-lei/p/9490523.html