算法实现一行数据左边奇数右边偶数

来源:互联网 发布:数控程序员考试 编辑:程序博客网 时间:2024/04/30 14:06

算法思想:


1.新建左游标,右游标;

2.向右移动左游标,遇到奇数,继续向右移动,移动一次左游标+1,直至找到偶数;

3.向左移动右游标,遇到偶数,继续向左移动,移动一次右游标-1, 直至找到奇数;

4. 交换step2 ,step3 中找到的奇,偶数

5. 重复step2,step3




提供3种解决方法:

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Diagnostics;namespace MyTestDemo{    public class TestDemo    {        public static void main()        {            //测试:比较运行8次            for (int i = 0; i < 8; i++)            {                int[] arr = Enumerable.Range(1, 5000000).ToArray();                int[] arr1 = arr.ToArray();                int[] arr2 = arr.ToArray();                int[] arr3 = arr.ToArray();                Console.WriteLine("第" + (i + 1) + "次");                Stopwatch sp = new Stopwatch();                sp.Start();                // Test Function 1                MyFuncionOne(arr1);                sp.Stop();                Console.WriteLine(sp.ElapsedMilliseconds * 1.0 / 1000 + "秒");                GC.Collect();                //重置计时器                sp.Reset();                sp.Start();                // Test Function 2                MyFuncionTwo(arr2);                sp.Stop();                Console.WriteLine(sp.ElapsedMilliseconds * 1.0 / 1000 + "秒");                GC.Collect();                //重置计时器                sp.Reset();                sp.Start();                // Test Function 3                MyFunctionThree(arr3);                sp.Stop();                Console.WriteLine(sp.ElapsedMilliseconds * 1.0 / 1000 + "秒");                GC.Collect();            }            Console.Read();        }        static void MyFuncionOne(int[] arr)        {            var prevIndex = arr.Length - 1;            for (var i = 0; i <= prevIndex; i++)            {                //偶数                 if (arr[i] % 2 == 0)                {                    bool stop = false;                    for (var j = prevIndex; j > i; j--)                    {                        if (arr[j] % 2 != 0)                        {                            //奇数                            var b = arr[i];                            prevIndex = j - 1;                            arr[i] = arr[j];                            arr[j] = b;                            break;                        }                        stop = j == i + 1;                    }                    if (stop)                    {                        break;                    }                }                else                {                    //奇数                    continue;                }            }        }        static void MyFuncionTwo(int[] ary)        {            int left = 0;            int right = ary.Length - 1;            while (left < right)            {                bool leftDouble = ary[left] % 2 == 0;                bool rightSingle = ary[right] % 2 != 0;                if (!leftDouble)                {                    left++;                }                if (!rightSingle)                {                    right--;                }                if (leftDouble && rightSingle)                {                    int t = ary[left];                    ary[left] = ary[right];                    ary[right] = t;                    left++;                    right--;                }            }        }        static void MyFunctionThree(int[] ary)        {            int left = 0;            int right = ary.Length - 1;            while (left < right)            {                bool leftDouble = ary[left] % 2 == 0;                //如果遇到左边是偶数                if (leftDouble)                {                    //移动右侧游标,直至找到一个奇数                    while (left < right)                    {                        if (ary[right] % 2 != 0)                        {                            //交换奇,偶数                            int t = ary[left];                            ary[left] = ary[right];                            ary[right] = t;                            left += 1;                            right -= 1;                            break;                        }                        else                            right -= 1;                    }                }                else                {                    //奇数                    left += 1;                    //移动左侧游标,直至找到偶数                    while (left < right)                    {                        if (ary[left] % 2 == 0)                        {                            break;                        }                        left += 1;                    }                }            }        }    }}





3中方法运行结果:



0 0
原创粉丝点击