快排优化方法:
方法一:随机值法
方法二:三位取中法
方法三:聚集相同元素法
方法四:插排替代法
随机值法:
#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;
}