//在一個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[] { 8, 4, 1, 6, 7, 4, 9, 6, 4 };
int[] cache = new int[array.Length];
System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
watch.Start();
for (int i = 0; i < array.Length; i++)
{
if (array[i] % 2 != 0 && i != 0)
{
for (int j = i; j > 0; j--)
{
if (array[j] % 2 != 0 && array[j - 1] % 2 == 0)
{
int pre = array[j - 1];
array[j - 1] = 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
排序後的數列
1 7 9 8 4 6 4 6 4
轉載于:https://www.cnblogs.com/wangjunqiao/p/7440390.html