排序_快排优化

快排优化方法:

方法一:随机值法
方法二:三位取中法
方法三:聚集相同元素法
方法四:插排替代法

随机值法:
#include<stdio.h>
#include<stdlib.h>
#include<stack>
#include<time.h>

void Quick(int arr[], int startindex, int endindex);
int partition(int arr[], int left, int right);
void Swap(int arr[], int first, int second);
void Show(int arr[], int len);

void QuickSort(int arr[], int len)
{
    if(arr == NULL || len<2)
    return ;
    Quick(arr, 0, len - 1);
}

void Quick(int arr[], int startindex, int endindex)
{
    if (startindex < endindex)
    {            
        Swap(arr,startindex,rand()%(endindex - startindex + 1) + startindex);
        int k = partition(arr, startindex, endindex);
        Quick(arr, startindex, k - 1);
        Quick(arr, k + 1, endindex);
    }
}

int partition(int arr[], int left, int right)
{
    int key = arr[left];
    while (left < right)
    {
        while (left < right && arr[right] >= key)right--;
        arr[left] = arr[right];
        while (left < right && arr[left] <= key)left++;
        arr[right] = arr[left];
    }
    arr[left] = key;
    return left;
}

void Show(int arr[], int len)
{
    for (int i = 0; i < len; ++i)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

void Swap(int arr[], int first, int second)
{
    int tmp = arr[first];
    arr[first] = arr[second];
    arr[second] = tmp;

}

int main()
{
    int arr[] = {32, 23, 28, 5, 2, 31, 21, 1, 9, 42, 53, 22};
    int len = sizeof(arr) / sizeof(arr[0]);
    QuickSort(arr, len);
    Show(arr, len);
    return 0;
}

 

三位取中法:
#include<stdio.h>
#include<stdlib.h>
#include<stack>
#include<time.h>

void Quick(int arr[], int startindex, int endindex);
int partition(int arr[], int left, int right);
void FindMiddleNumber(int arr[], int left, int mid, int right);
void Swap(int arr[], int first, int second);
void Show(int arr[], int len);

void QuickSort(int arr[], int len)
{
    if(arr == NULL || len<2)
    return ;
    Quick(arr, 0, len - 1);
}

void Quick(int arr[], int startindex, int endindex)
{
    if (startindex < endindex)
    {            
        int mid = (startindex + endindex) / 2;
        FindMiddleNumber(arr, startindex, mid, endindex);
        int k = partition(arr, startindex, endindex);
        Quick(arr, startindex, k - 1);
        Quick(arr, k + 1, endindex);
    }
}

int partition(int arr[], int left, int right)
{
    int key = arr[left];
    while (left < right)
    {
        while (left < right && arr[right] >= key)right--;
        arr[left] = arr[right];
        while (left < right && arr[left] <= key)left++;
        arr[right] = arr[left];
    }
    arr[left] = key;
    return left;
}

void FindMiddleNumber(int arr[], int left, int mid, int right)
{
    if (arr[mid] > arr[right])
    {
        Swap(arr, mid, right);
    }
    if (arr[left] > arr[right])
    {
        Swap(arr, left, right);
    }
    if (arr[left] < arr[mid])
    {
        Swap(arr, left, mid);
    }
}

void Show(int arr[], int len)
{
    for (int i = 0; i < len; ++i)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

void Swap(int arr[], int first, int second)
{
    int tmp = arr[first];
    arr[first] = arr[second];
    arr[second] = tmp;

}

int main()
{
    int arr[] = {32, 23, 28, 5, 2, 31, 21, 1, 9, 42, 53, 22};
    int len = sizeof(arr) / sizeof(arr[0]);
    QuickSort(arr, len);
    Show(arr, len);
    return 0;
}

 

聚集相同元素法:
#include<stdio.h>
#include<stdlib.h>
#include<stack>
#include<time.h>

void Quick(int arr[], int startindex, int endindex);
int partition(int arr[], int left, int right);
void Gather(int arr[],int startindex,int keyindex, int endindex,int* newleft, int* newright);
void Swap(int arr[], int first, int second);
void Show(int arr[], int len);

void QuickSort(int arr[], int len)
{
    if(arr == NULL || len<2)
    return ;
    Quick(arr, 0, len - 1);
}

void Quick(int arr[], int startindex, int endindex)
{
    if (startindex < endindex)
    {            
        int left = -1;
        int right = -1;
        int k = partition(arr, startindex, endindex);
        Gather(arr, startindex, k, endindex, &left, &right);
        Quick(arr, startindex,left);
        Quick(arr, right, endindex);
    }
}

int partition(int arr[], int left, int right)
{
    int key = arr[left];
    while (left < right)
    {
        while (left < right && arr[right] >= key)right--;
        arr[left] = arr[right];
        while (left < right && arr[left] <= key)left++;
        arr[right] = arr[left];
    }
    arr[left] = key;
    return left;
}

void Gather(int arr[],int startindex,int keyindex, int endindex,int* newleft, int* newright)
{
        int keyLeft = keyindex- 1;
        int keyRight = keyindex+ 1;
        for (int i = keyindex- 1; i >= start; i--)
        {
            if (arr[i] == arr[keyindex])
            {
                if (i != keyLeft )
                {
                    int tmp = arr[keyLeft];
                    arr[keyLeft] = arr[i];
                    arr[i] = tmp;
                }keyLeft--;  
            }
        }
        for (int j = keyindex + 1; j <= end; j++)
        {
            if (arr[j] == arr[keyindex])
            {
                if (j != keyRight)
                {
                    int tmp = arr[keyRight];
                    arr[keyRight] = arr[j];
                    arr[j] = tmp;
                }keyRight++;
            }
        }

    *newleft = keyLeftf;
    *newright = keyRight;
}

void Show(int arr[], int len)
{
    for (int i = 0; i < len; ++i)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

void Swap(int arr[], int first, int second)
{
    int tmp = arr[first];
    arr[first] = arr[second];
    arr[second] = tmp;

}

int main()
{
    int arr[] = {32, 23, 28, 5, 2, 31, 21, 1, 9, 42, 53, 22};
    int len = sizeof(arr) / sizeof(arr[0]);
    QuickSort(arr, len);
    Show(arr, len);
    return 0;
}

插排替代法:
#include<stdio.h>
#include<stdlib.h>
#include<stack>
#include<time.h>

void Quick(int arr[], int startindex, int endindex);
int partition(int arr[], int left, int right);
void insertSort(int arr[], int startindex, int endindex);
void Swap(int arr[], int first, int second);
void Show(int arr[], int len);

void QuickSort(int arr[], int len)
{
    if(arr == NULL || len<2)
    return ;
    Quick(arr, 0, len - 1);
}

void Quick(int arr[], int startindex, int endindex)
{
    if (startindex < endindex)
    {            
        if (endindex - startindex + 1 <= 30)
        {
            insertSort(arr, startindex, endindex);
            return;
        }

        int k = partition(arr, startindex, endindex);
        Quick(arr, startindex, k - 1);
        Quick(arr, k + 1, endindex);
    }
}

int partition(int arr[], int left, int right)
{
    int key = arr[left];
    while (left < right)
    {
        while (left < right && arr[right] >= key)right--;
        arr[left] = arr[right];
        while (left < right && arr[left] <= key)left++;
        arr[right] = arr[left];
    }
    arr[left] = key;
    return left;
}

void insertSort(int arr[], int startindex, int endindex)
{
    int tmp = 0;
    int i = startindex + 1;
    int j = i - 1;
    for (i; i <= endindex; ++i)
    {
        tmp = arr[i];
        for (j = i - 1; j >= startindex && arr[j] > tmp; --j)
        {
            arr[j + 1] = arr[j];
        }
        arr[j + 1] = tmp;
    }
}

void Show(int arr[], int len)
{
    for (int i = 0; i < len; ++i)
    {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

void Swap(int arr[], int first, int second)
{
    int tmp = arr[first];
    arr[first] = arr[second];
    arr[second] = tmp;

}

int main()
{
    int arr[] = {32, 23, 28, 5, 2, 31, 21, 1, 9, 42, 53, 22};
    int len = sizeof(arr) / sizeof(arr[0]);
    QuickSort(arr, len);
    Show(arr, len);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值