#include<stdio.h>
#include<stdlib.h>
#include<stdint.h>
#include<stddef.h>
#include<limits.h>
#include<string.h>
extern int* sortArray(int* nums, int numsSize, int* returnSize);
int* sortArray(int* nums, int numsSize, int* returnSize)
{
int* buf = NULL;
int m = 0;
int n = 0;
int j = 0;
int min = INT_MAX;
int max = INT_MIN;
int tmp;
int* sort_buf = (int*)malloc((numsSize)*sizeof(int)); //申请的空间太小 导致的heap 溢出问题
memset(sort_buf,0,(numsSize)*sizeof(int));
if(nums == NULL)
{
*returnSize = -1;
return NULL;
}
if(numsSize == 0)
{
*returnSize = -1;
return NULL;
}
for(n=0;n<numsSize;n++)
{
for(m=n;m<(numsSize);m++)
{
if(nums[m]<min)
{
min = nums[m];
j = m;
}
if(nums[m]>max)
{
max = nums[m];
}
}
tmp = nums[j];
nums[j] = nums[n];
nums[n] = tmp;
sort_buf[n] = min;
min = max;
}
#if 0
for(n=0;n<numsSize;n++)
{
printf("%d\t",sort_buf[n]);
}
printf("\n");
#endif
#if 0
for(m=0;m<numsSize;m++)
{
printf("%d\t",nums[m]);
}
printf("numsize is%d size is%d\n",numsSize,*returnSize);
#endif
*returnSize = numsSize;
return sort_buf;
}
int main(void)
{
int nums[] = {5,3,2,1,20};
int return_size = -1;
int* sortarr = NULL;
int n;
printf("sort front:");
for(n=0;n<sizeof(nums)/sizeof(nums[0]);n++)
{
printf("%d\t",nums[n]);
}
printf("\n");
sortarr = sortArray(nums,sizeof(nums)/sizeof(nums[0]),&return_size);
printf("\n");
printf("return_size is%d\n",return_size);
printf("sort rear:");
for(n=0;n<return_size;n++)
{
printf("%d\t",sortarr[n]);
}
printf("\n");
free(sortarr);
return 0;
}
linux c语言之选择排序
最新推荐文章于 2024-08-27 15:17:48 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)