• 在一个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
  • 相关阅读:
    selenium产生的垃圾文件清理
    英语学习词根法
    ecommerce学习
    tfs2012安装
    转载文件,英语学习
    Mishka and Divisors CodeForces
    Codeforces Round #364 (Div. 1) (差一个后缀自动机)
    莫比乌斯反演练习
    bzoj 1267 Kth Number I (点分治,堆)
    程序员的绘图利器 — Gnuplot
  • 原文地址:https://www.cnblogs.com/wangjunqiao/p/7440390.html
Copyright © 2020-2023  润新知