- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace c4._4
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] num = new int[10];
- Program p = new Program();
- Console.WriteLine("原始数组:");
- p.randIt(num);
- p.show(num);
- p.sort(num);
- Console.WriteLine();
- Console.WriteLine("降序数组");
- p.show(num);
- Console.ReadKey();
- }
- public void randIt(int[] num)
- {
- Random randNum = new Random();
- for (int i = 0; i < num.Length; i++)
- {
- num[i] = randNum.Next(101);//101??
- }
- }
- //这是选择排序的算法
- public void sort(int[] arr)
- {
- for (int i = 0; i < arr.Length; i++)
- for (int j = i; j < arr.Length; j++)//看清楚j的终止条件
- { //选择跟冒泡有点不同,是先排第一个,然后往后排
- if (arr[i] < arr[j])
- {
- int temp = arr[j];
- arr[j] = arr[i];
- arr[i] = temp;
- }
- }
- }
- public void show(int[] arr)
- {
- foreach (int i in arr)
- {
- Console.Write("{0} ", i);
- }
- }
- }
- }
c#选择排序
最新推荐文章于 2023-09-01 15:50:39 发布