int cmp(const void* a, const void* b){ return *(int*)a-*(int*)b; } void nextPermutation(int* nums, int numsSize){ int i,left,pst=0,val,temp; int* arr=(int*)calloc(sizeof(int),numsSize); for(i=numsSize-1; i>=0; i--){ if (i!=numsSize-1 && nums[i]<nums[i+1]) { val=nums[i]; left=i; break; } arr[pst++]=nums[i]; } if (i==-1) { qsort(nums,numsSize,sizeof(int),cmp); return; } for (i=0; i<pst && arr[i]<=val; i++); nums[left]=arr[i]; arr[i]=val; for (i=left+1; i<numsSize; i++) nums[i]=arr[i-(left+1)]; }