有一个整数数组,里面存放着5,7,1,3,9,13,11,15,19,17,请序列升序排列并倒序输出
答案:2 悬赏:80 手机版
解决时间 2021-04-08 07:25
- 提问者网友:喧嚣尘世
- 2021-04-07 10:55
有一个整数数组,里面存放着5,7,1,3,9,13,11,15,19,17,请序列升序排列并倒序输出
最佳答案
- 五星知识达人网友:从此江山别
- 2021-04-07 11:12
static void Main(string[] args)
{
int[] nums = new int[] { 5, 7, 1, 3, 9, 13, 11, 15, 19, 17 };
nums = Sort(nums, 0, nums.Length - 1);
for (int t = nums.Length - 1; t >= 0; t--)
{
Console.WriteLine(nums[t]);
}
}
private static int[] Sort(int[] nums, int left, int right)
{
if (left < right)
{
int mid = nums[(left + right) / 2];
int i = left;
int j = right;
while (true)
{
while (nums[i] < mid)
{
i++;
}
while (nums[j] > mid)
{
j--;
}
if (i >= j)
break;
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
Sort(nums, left, i - 1);
Sort(nums, j + 1, right);
}
return nums;
}
{
int[] nums = new int[] { 5, 7, 1, 3, 9, 13, 11, 15, 19, 17 };
nums = Sort(nums, 0, nums.Length - 1);
for (int t = nums.Length - 1; t >= 0; t--)
{
Console.WriteLine(nums[t]);
}
}
private static int[] Sort(int[] nums, int left, int right)
{
if (left < right)
{
int mid = nums[(left + right) / 2];
int i = left;
int j = right;
while (true)
{
while (nums[i] < mid)
{
i++;
}
while (nums[j] > mid)
{
j--;
}
if (i >= j)
break;
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
Sort(nums, left, i - 1);
Sort(nums, j + 1, right);
}
return nums;
}
全部回答
- 1楼网友:躲不过心动
- 2021-04-07 12:35
1 3 5 7 9 11 13 15 17 19
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯