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

//在一个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 + "	");
            }


        }
    }
}

排序结果:

统计时间:0.001
排序后的数列
1       7       9       8       4       6       4       6       4
原文地址:https://www.cnblogs.com/wangjunqiao/p/7440390.html