下列具体实现了 希尔排序 插入排序 快速排序 归并排序(包括递归和非递归)
如若有任何不懂之处,欢迎评论,我会尽我之力解答
总结:对gap的运用
void ShellSort(int* nums,int numsSize)
{
int i = 0;
int end = 0;
int temp = 0;
int gap = numsSize;
while( gap > 1 )
{
gap = gap / 3 + 1;//+1是为了保证最后gap为1,进行插入排序,从而实现排序
for( i = 0 ; i < numsSize - gap ; i++)
{
end = i;
temp = nums[end+gap];
while( end >= 0 )
{
if( temp < nums[end])
{
nums[end+gap] = nums[end];
end-=gap;
}
else break;
}
nums[gap+end] = temp;
}
}
}
void SelectSort(int* nums,int numsSize)
{
int i = 0;
int temp = 0;
int end = 0;
for( i = 0 ; i < numsSize - 1 ; i++)
{
end = i;
temp = nums[end+1];
while( end >= 0 )
{
if( temp < nums[end])
{
nums[end+1] = nums[end];
end -= 1;
}
else break;
}
nums[end+1] = temp;
}
}
霍尔版本:
void swap(int* p1 ,int *p2 )
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void QuickSort1(int* nums,int left,int right )
{
int keyi = left;
int start = left;
int rear = right;
if( left >= right )
return;
while( right > left )
{
while( nums[right] >= nums[keyi] && right > left )
{
right--;
}
while( nums[left] <= nums[keyi] && right > left )
{
left++;
}
swap(&nums[right],&nums[left]);
}
swap(&nums[left],&nums[keyi]);
keyi = left;
QuickSort1(nums,start,keyi-1);
QuickSort1(nums,keyi+1,rear);
}
双指针版本:
void swap(int* p1 ,int *p2 )
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void QuickSort2(int* nums ,int left ,int right )
{
int keyi = left;
int cur = left+1;
int prev = left;
if( left >= right )
return ;
while( cur <= right )
{
if( nums[cur] < nums[keyi] )
{
prev++;
swap(&nums[cur],&nums[prev]);
}
cur++;
}
swap(&nums[keyi],&nums[prev]);
keyi = prev;
QuickSort2(nums,left,keyi-1);
QuickSort2(nums,keyi+1,right);
}
void mergesort(int* nums , int left , int right , int* temp)//
{
int mid = (left+right)/2;
int begin1 = left;
int end1 = mid;
int begin2 = mid+1;
int end2 = right;
int i = left;
if( left >= right )return ;
mergesort(nums,left,mid,temp);
mergesort(nums,mid+1,right,temp);
while( begin1 <= end1 && begin2 <= end2 )
{
if( nums[begin1] < nums[begin2])
{
temp[i++] = nums[begin1++];
}
else
{
temp[i++] = nums[begin2++];
}
}
while( begin1 <= end1 )
{
temp[i++] = nums[begin1++];
}
while( begin2 <= end2 )
{
temp[i++] = nums[begin2++];
}
memcpy(nums+left,temp+left,sizeof(int)*(right-left+1));
}
void MergeSort(int* nums,int numsSize)
{
int* temp = (int*)malloc(sizeof(int)*numsSize);
mergesort(nums,0,numsSize-1,temp);
free(temp);
temp = NULL;
}
void _MergeSort(int* nums,int numsSize )
{
int* str = (int*)malloc(sizeof(int)*numsSize);
int begin1 = 0;
int end1 = 0;
int begin2 = 0;
int end2 = 0;
int i = 0;
int j = 0;
int k = 0;
int gap = 1;
while( gap < numsSize )
{
for( j = 0 ; j < numsSize ; j += 2*gap )
{
i = j;
begin1 = i;
end1 = begin1 + gap - 1;
begin2 = end1 + 1;
end2 = begin2 + gap - 1;
if( end1 >= numsSize || begin1 >= numsSize )break;
if( end2 >= numsSize ) end2 = numsSize - 1;
while( begin1 <= end1 && begin2 <= end2 )
{
if( nums[begin1] < nums[begin2] )
{
str[i++] = nums[begin1++];
}
else
{
str[i++] = nums[begin2++];
}
}
while( begin1 <= end1 )
{
str[i++] = nums[begin1++];
}
while( begin2 <= end2 )
{
str[i++] = nums[begin2++];
}
}
gap *= 2;
memcpy(nums,str,sizeof(int)*numsSize);
}
}