算法实现一行数据左边奇数右边偶数
来源:互联网 发布:数控程序员考试 编辑:程序博客网 时间: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; } } } } }}
0 0
- 算法实现一行数据左边奇数右边偶数
- 冒泡排序的方式实现一组数据偶数放在左边奇数放在右边
- 递归-奇数放左边,偶数放右边
- 算法:将数组中的整数进行排列,奇数在左边,偶数在右边
- 链表之交换奇偶数,实现左边为奇数,右边为偶数
- 能实现把一个数组元素中的奇数放左边,偶数放右边
- 剑指offer11--让数组变成左边奇数右边偶数
- 重排数组使得数组左边为奇数,右边为偶数
- 重新排列数组使得左边为奇数,右边为偶数
- 【每天一道算法题】给定一个存放整数的数组,要求数组左边为奇数,右边为偶数
- 已知一个整数数组A[n],写出算法实现将奇数元素放在数组的左边,将偶数放在数组的右边。要求时间复杂度为O(n)。
- 设计算法将线性表中的元素分为两部分,要求左边都是奇数,右边都是偶数,时间复杂度O(n),空间复杂度O(1)
- 将一个数列中的奇数放在左边,偶数放在右边
- 重新排列数组使得数组左边为奇数,右边为偶数
- 写正确函数需要注意的地方:奇偶数分开,左边偶数,右边奇数。
- 重新排列数组使得数组左边为奇数,右边为偶数
- 对数组重新排列,使左边为奇数,右边为偶数
- 83.第 3 组百度面试题 数组使得数组左边为奇数,右边为偶数
- iPhone开发之UIScrollView滚动组件的使用(一) 拖线实现——大图的移动展示
- POJ 2342 Anniversary party(树形DP入门)
- iOS入门如何选择Swift和objective-c
- MII接口全家福
- HDU-2553-N皇后问题
- 算法实现一行数据左边奇数右边偶数
- 顺序表
- 从零开始学Java之单例模式:懒汉模式和饱汉模式
- 关于/etc/hosts文件
- HDU1213 How Many Tables - 并查集
- Mac实用工具
- vmware11.1.2里安装hyper-v
- (2)如何在Javascript中自定义类 & 面向对象编程 (封装)
- 在线调试微信打开的HTML5页面