//方法类 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cp4 { class BubbleSorter { //数组由小到大排序 public static void BubbleSortAscending(int[] arr) { for (int i = 1; i < arr.Length; i++) { for (int j = 0; j < arr.Length - 1; j++) { if (arr[j] > arr[j + 1]) { int temp; temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } //打印出数组 public static void PrintArray(int[] arr) { foreach (int print in arr) { Console.WriteLine(print); } } } } //测试类 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cp4 { class Test { public static void Main(string[] agrs) { int[] n = {12,56,98,42,62,35 }; BubbleSorter.BubbleSortAscending(n); //调用静态方法,输出数组 BubbleSorter.PrintArray(n); } } }