目录
一.快速排序
1.左右指针法
我们找到一个key,begin去找比key大的值,end去找比key小的值,找到了就将begin和end交换,等到相遇时就将相遇的值与key交换,这样左边都比key小,右边都比key大

这是快速排序的单趟思想,排完之后,左边比key小,右边比key大,而为了让整体有序,我们可以分为两个区间,将左边的值再次排序,右边的值再次排序,中间的不需要再动,因为已经排好,不断重复这个过程,直到排好

注意:1.选择右边的值做key,要让左边的begin先走,这样能够保证相遇时的位置的值比key大
2.选择左边的值做key,要让右边的end先走,相遇时比key小
//[begin,end],左右指针法
int PartSort1(int* a, int begin, int end)
{
	int keyindex = end;
	while (begin < end)
	{
		//begin找小
		while (begin < end && a[begin] <= a[keyindex])
		{
			++begin;
		}
		//end找大
		while (begin < end && a[end] >= a[keyindex])
		{
			--end;
		}
		Swap(&a[begin], &a[end]);
	}
	Swap(&a[begin], &a[keyindex]);
	return begin;
}
void QuickSort(int* a, int left, int right)
{
	assert(a);
	if (left >= right)
		return;
	
	int div = PartSort1(a, left, right);
	//[left,div-1] div [div+1,right]
	QuickSort(a, left, div - 1);
	QuickSort(a, div + 1, right);
} 

我们可以看到快速排序的处理速度还是很不错的,但是当我们给定一组排好序的数,快排就会慢很多

可以看到快排的效率和插入排序差不多了
那么如何优化?我们在这里用到三数取中,我们不选到最小或者最大的,选择中间的

//三数取中
int GetMidIndex(int* a, int begin, int end)
{
	int mid = (begin + end) / 2;
	if (a[begin] < a[mid])
	{
		if (a[mid] < a[end])
		{
			return mid;
		}
		else if(a[begin] > a[end])
		{
			return begin;
		}
		else
		{
			return end;
		}
	}
	else
	{
		if (a[mid] > a[end])
		{
			return mid;
		}
		else if (a[begin] < a[end])
		{
			return begin;
		}
		else
		{
			return end;
		}
	}
} 
经过三数取中即使给定一组有序的数组,效率亦不会有什么影响
2.挖坑法
挖坑法与左右指针相似,我们选择一个key,将他的位置作为坑,用begin找大,找到了将begin指向的值放入坑内,begin的位置就成了新坑,用end找小,找到小的放入坑内,end的位置成为新坑,当begin和end相遇了,将key放入坑内

//挖坑法
int PartSort2(int* a, int begin, int end)
{
	int midIndex = GetMidIndex(a, begin, end);
	Swap(&a[midIndex], &a[end]);
	//坑
	int key = a[end];
	while (begin < end)
	{
		while (begin < end && a[begin] <= key)
		{
			++begin;
		}
		//左边找到比key大的值填到右边的坑,begin位置成为新坑
		a[end] = a[begin];
		while (begin < end && a[end] >= key)
		{
			--end;
		}
		//右边找到比key小的值填到左边的坑,end位置成为新坑
		a[begin] = a[end];
	}
	a[begin] = key;
	return begin;
} 
3.前后指针法
先找到一个key,cur从数组第一个元素走,prev为cur前一个位置,cur找比key小的,找到了就停下来,++prev,交换prev,找到大的就继续走

//前后指针法
int PartSort3(int* a, int begin, int end)
{
	int prev = begin - 1;
	int cur = begin;
	int keyindex = end;
	while (cur < end)
	{
		if (a[cur] < a[keyindex] && ++prev != cur)
			Swap(&a[prev], &a[cur]);
		++cur;
	}
	Swap(&a[++prev], &a[keyindex]);
	return prev;
} 
4.非递归实现
那么我们已经用递归实现了快速排序,为什么还要用非递归?
1.提高效率(递归建栈有消耗)
2.递归太深可能栈溢出,系统空间不大一般在MB级别,数据结构栈模拟非递归,数据存储在堆上,而堆是G级别的空间
void QuickSortNonR(int* a, int left, int right)
{
	//用栈模拟
	Stack st;
	StackInit(&st);
	StackPush(&st, right);
	StackPush(&st, left);
	while (!StackEmpty(&st))
	{
		int begin = StackTop(&st);
		StackPop(&st);
		int end = StackTop(&st);
		StackPop(&st);
		int div = PartSort3(a, begin, end);
		if (div + 1 < end)
		{
			StackPush(&st, end);
			StackPush(&st, div + 1);
		}
		if (begin < div - 1)
		{
			StackPush(&st, div - 1);
			StackPush(&st, begin);
		}
	}
	StackDestory(&st);
} 
5.快速排序特性总结
1.快速排序整体的综合性能和使用场景都是比较好的
2.时间复杂度:O(N*logN)
3.空间复杂度:O(logN)
4.稳定性:不稳定
二.整体代码
1.Sort.h
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <time.h>
void PrintArray(int* a, int n);
void Swap(int* p1, int* p2);
// 快速排序递归实现
// 快速排序hoare版本
int PartSort1(int* a, int begin, int end);
// 快速排序挖坑法
int PartSort2(int* a, int begin, int end);
// 快速排序前后指针法
int PartSort3(int* a, int begin, int end);
void QuickSort(int* a, int left, int right);
// 快速排序 非递归实现
void QuickSortNonR(int* a, int left, int right); 
2.Sort.c
#include "Sort.h"
#include "Stack.h"
void PrintArray(int* a, int n)
{
	for (int i = 0; i < n; ++i)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}
void Swap(int* p1, int* p2)
{
	int tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}
//三数取中
int GetMidIndex(int* a, int begin, int end)
{
	int mid = (begin + end) / 2;
	if (a[begin] < a[mid])
	{
		if (a[mid] < a[end])
		{
			return mid;
		}
		else if(a[begin] > a[end])
		{
			return begin;
		}
		else
		{
			return end;
		}
	}
	else
	{
		if (a[mid] > a[end])
		{
			return mid;
		}
		else if (a[begin] < a[end])
		{
			return begin;
		}
		else
		{
			return end;
		}
	}
}
//[begin,end],左右指针法
int PartSort1(int* a, int begin, int end)
{
	int midIndex = GetMidIndex(a, begin, end);
	Swap(&a[midIndex], &a[end]);
	int keyindex = end;
	while (begin < end)
	{
		//begin找小
		while (begin < end && a[begin] <= a[keyindex])
		{
			++begin;
		}
		//end找大
		while (begin < end && a[end] >= a[keyindex])
		{
			--end;
		}
		Swap(&a[begin], &a[end]);
	}
	Swap(&a[begin], &a[keyindex]);
	return begin;
}
//挖坑法
int PartSort2(int* a, int begin, int end)
{
	int midIndex = GetMidIndex(a, begin, end);
	Swap(&a[midIndex], &a[end]);
	//坑
	int key = a[end];
	while (begin < end)
	{
		while (begin < end && a[begin] <= key)
		{
			++begin;
		}
		//左边找到比key大的值填到右边的坑,begin位置成为新坑
		a[end] = a[begin];
		while (begin < end && a[end] >= key)
		{
			--end;
		}
		//右边找到比key小的值填到左边的坑,end位置成为新坑
		a[begin] = a[end];
	}
	a[begin] = key;
	return begin;
}
//前后指针法
int PartSort3(int* a, int begin, int end)
{
	int prev = begin - 1;
	int cur = begin;
	int keyindex = end;
	while (cur < end)
	{
		if (a[cur] < a[keyindex] && ++prev != cur)
			Swap(&a[prev], &a[cur]);
		++cur;
	}
	Swap(&a[++prev], &a[keyindex]);
	return prev;
}
void QuickSort(int* a, int left, int right)
{
	assert(a);
	if (left >= right)
		return;
	
	int div = PartSort1(a, left, right);
	//[left,div-1] div [div+1,right]
	QuickSort(a, left, div - 1);
	QuickSort(a, div + 1, right);
}
// 快速排序 非递归实现
//非递归:1.提高效率(递归建栈有消耗)
//2.递归太深可能栈溢出,系统空间不大一般在G级别
//数据结构栈模拟非递归,数据存储在堆上,而堆是G级别的空间
void QuickSortNonR(int* a, int left, int right)
{
	//用栈模拟
	Stack st;
	StackInit(&st);
	StackPush(&st, right);
	StackPush(&st, left);
	while (!StackEmpty(&st))
	{
		int begin = StackTop(&st);
		StackPop(&st);
		int end = StackTop(&st);
		StackPop(&st);
		int div = PartSort3(a, begin, end);
		if (div + 1 < end)
		{
			StackPush(&st, end);
			StackPush(&st, div + 1);
		}
		if (begin < div - 1)
		{
			StackPush(&st, div - 1);
			StackPush(&st, begin);
		}
	}
	StackDestory(&st);
} 
3.Stack.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef int STDataType;
typedef struct Stack
{
    STDataType* _a;
    int _top;//栈顶下标
    int _capacity;
}Stack;
void StackInit(Stack* pst);
void StackDestory(Stack* pst);
void StackPush(Stack* pst, STDataType x);
void StackPop(Stack* pst);
int StackSize(Stack* pst);
int StackEmpty(Stack* pst);
STDataType StackTop(Stack* pst); 
4.Stack.c
#include "Stack.h"
//初始化
void StackInit(Stack* pst)
{
	pst->_a = (STDataType*)malloc(sizeof(STDataType) * 4);
	if (pst->_a == NULL)
	{
		printf("申请内存失败\n");
	}
	pst->_top = 0;
	pst->_capacity = 4;
}
//销毁
void StackDestory(Stack* pst)
{
	free(pst->_a);
	pst->_a = NULL;
	pst->_capacity = 0;
	pst->_top = 0;
}
//入栈
void StackPush(Stack* pst, STDataType x)
{
	assert(pst);
    if (pst->_top == pst->_capacity)
    {
        pst->_capacity *= 2;
        STDataType* tmp = (STDataType*)realloc(pst->_a, sizeof(STDataType) * pst->_capacity);
        if (tmp == NULL)
        {
            printf("增容失败\n");
            exit(-1);
        }
        else
        {
            pst->_a = tmp;
        }
    }
    pst->_a[pst->_top] = x;
    pst->_top++;
}
//出栈
void StackPop(Stack* pst)
{
    assert(pst);
    assert(pst->_top > 0);
    --pst->_top;
}
//获取数据个数
int StackSize(Stack* pst)
{
    assert(pst);
    return pst->_top;
}
//返回1为空,返回0为非空
int StackEmpty(Stack* pst)
{
    assert(pst);
    return pst->_top == 0 ? 1 : 0;
}
//获取栈顶数据
STDataType StackTop(Stack* pst)
{
    assert(pst);
    assert(pst->_top > 0);
    return pst->_a[pst->_top - 1];
} 
5.test.c
#include "Sort.h"
// 测试排序的性能对比
void TestOP()
{
	srand(time(0));
	const int N = 3000;
	int* a1 = (int*)malloc(sizeof(int) * N);
    for (int i = 0; i < N; ++i)
    {
	    a1[i] = rand();
    }
    int begin1 = clock();
    QuickSort(a1, 0,N - 1);
    int end1 = clock();
    printf("QuickSort:%d\n", end1 - begin1);
    free(a1);
}
void TestQuickSort()
{
	int a[] = { 6,1,2,7,9,3,4,8,10,5 };
	PrintArray(a, sizeof(a) / sizeof(int));
	QuickSortNonR(a, 0, sizeof(a) / sizeof(int) - 1);
	PrintArray(a, sizeof(a) / sizeof(int));
}
int main()
{
	TestQuickSort();
	TestOP();
}
 
                
                  
                  
                  
                  
                            
      
          
                
                
                
                
              
                
                
                
                
                
              
                
                
              
            
                  
					781
					
被折叠的  条评论
		 为什么被折叠?
		 
		 
		
    
  
    
  
            


            