在一个N个整数数组里面,有多个奇数和偶数,设计一个排序算法,令所有的奇数都在左边。

时间:2024-09-19 08:37:02
//在一个N个整数数组里面,有多个奇数和偶数,设计一个排序算法,令所有的奇数都在左边。

// 例如: 当输入a = {8,4,1,6,7,4,9,6,4},

// a = {1,7,9,8,4,6,4,6,4}为一种满足条件的排序结果

using System;

namespace SuanFa
{
class Program
{
//在一个N个整数数组里面,有多个奇数和偶数,设计一个排序算法,令所有的奇数都在左边。
static void Main(string[] args)
{
int[] array = new int[] { , , , , , , , , };
int[] cache = new int[array.Length];
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
for (int i = ; i < array.Length; i++)
{
if (array[i] % != && i != )
{
for (int j = i; j > ; j--)
{
if (array[j] % != && array[j - ] % == )
{
int pre = array[j - ];
array[j - ] = array[j];
array[j] = pre;
} } }
}
watch.Stop();
Console.WriteLine("统计时间:" + watch.Elapsed.TotalMilliseconds); Console.WriteLine("排序后的数列");
foreach (var n in array)
{
Console.Write(n + "\t");
} }
}
}

排序结果:

统计时间:0.001
排序后的数列