#include <iostream> using namespace std; void txsort(int* arr, int len) { if (!arr || len == 1) { return; } int negCount = 0; for (int i = 0; i < len; ++i) { if (arr[i] < 0) { negCount++; } } int swapingId = 0; int fixNegId = 0; int toSwapId = 0; for (int i = 0; i < len; ++i) { swapingId = (i - fixNegId) % (negCount - fixNegId) + fixNegId; toSwapId = i + negCount - fixNegId; /*arr[swapingId] < 0说明此处的负数未固定, * 将带交换元素下标定位至待固定元素下标 */ if (arr[swapingId] < 0) { toSwapId = fixNegId; fixNegId++; } swap(arr[swapingId], arr[toSwapId]); if (fixNegId == negCount) { break; } } }