|
在写PML程序中用到比较复杂数组的排序,但是大概找了下帮助没有我想要的,就看了下之前的C#里面写过的冒泡排序,在PML中也写了下,有点意思
- <P>--PML数组冒泡排序法
- --09:07:40 PM, October 02, 2013, COSCO Shipyard_LiYuan.
- !nums = array()
- !nums.append(2)
- !nums.append(12)
- !nums.append(14)
- !nums.append(14)
- !nums.append(20)
- !nums.append(1)
- !nums.append(50)
- !nums.append(99)
- !nums.append(8)
- !nums.append(30)</P>
- <P>do !a to !nums.size()
- do !b to (!nums.size() - !a)
- if (!nums[!b] gt !nums[!b + 1]) then $* //升序排列, if (!nums[!b] lt !nums[!b + 1]) then //降序排列
- !temp = !nums[!b]
- !nums[!b] = !nums[!b + 1]
- !nums[!b + 1] = !temp
- endif
- enddo
- enddo</P>
- <P>q var!nums </P>
复制代码 C#中简单的冒泡- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Lx_array
- {
- class Program
- {
- static void Main(string[] args)
- {
- ShowUI();
- int k = 1;
- int[] nums = new int[10];
- for (int i = 0; i <nums.Length; i++)
- {
- try
- {
- Console.Write("请输入第{0}/{1}个数字: ", i + 1,nums.Length);
- nums[i] = Convert.ToInt32(Console.ReadLine());
- }
- catch
- {
- if (k < 3)
- {
- Console.WriteLine("输入的数据不合法, 请重新输入!");
- i--;
- k++;
- }
- else
- {
- Console.WriteLine("超过最大输入次数!");
- goto Finish;
- }
- }
- }
- for (int n = 0; n <nums.Length; n++)
- {
- Console.WriteLine("第{0}/{1}个数字:{2} ", n + 1, nums.Length, nums[n]);
- }
- //数组元素冒泡排序
- for (int g = 0; g < nums.Length - 1; g++)
- {
- for (int j = 0; j < nums.Length -g- 1; j++)
- {
- if (nums[j] < nums[j + 1])
- {
- int temp = nums[j];
- nums[j] = nums[j + 1];
- nums[j + 1] = temp;
- }
- }
- }
- for (int ii = 0; ii < nums.Length; ii++)
- {
- if (ii < nums.Length-1)
- {
- Console.Write("{0} > ", nums[ii]);
- }
- else
- {
- Console.Write(nums[ii]);
- }
- }
- Finish:
- Console.ReadKey();
- ShowUI();
- Console.ReadKey();
- }
- /// <summary>
- /// 显示软件主界面的方法
- /// </summary>
- public static void ShowUI()
- {
- Console.Clear();
- Console.WriteLine("**********************************************");
- Console.WriteLine("** 冒泡排序 **");
- Console.WriteLine("**********************************************");
- Console.WriteLine("按任意键退出!");
- //Console.ReadKey();
- }
- }
- }
复制代码 |
|