C#练习源代码下载请到http://download.csdn.net/detail/hsttmht/3751088
引用请注明http://blog.csdn.net/hsttmht
c#选择排序
using System; using System.Collections.Generic; namespace select { public class SelectSorter { private int min; public void s(int[] list) { for(int i=0;i<list.Length-1;i++) { min=i; for(int j=i+1;j<list.Length;j++) { if(list[j]<list[min]) min=j; } int h=list[min]; list[min]=list[i]; list[i]=h; } } } public class test { public static void Main() { int[] arrary = new int[]{24,51,1,66,13,87,46,51,77,9,-4}; SelectSorter ss = new SelectSorter(); ss.s(arrary); for(int a=0;a<arrary.Length;a++) Console.Write("{0},",arrary[a]); Console.ReadLine(); } } }
C#冒泡排序
using System; using System.Collections.Generic; using System.Text; namespace GanggangApplication { class Program { static void Main(string[] args) { SortedNumbers(); } /// 该方法获得需要排序的数组,表调用排序方法进行排序 public static void SortedNumbers() { int numberCount; int[] numbers; Console.WriteLine("冒泡排序法"); Console.WriteLine("请问您要对多少为数字进行排序?"); numberCount = Convert.ToInt32(Console.ReadLine()); numbers = new int[numberCount]; Console.WriteLine("请输入您要进行排序的这{0}个数字:",numberCount); for (int i = 0; i < numberCount; i++) { numbers[i] = Convert.ToInt32(Console.ReadLine()); } Console.WriteLine("\n您要进行排序的{0}个数字分别为:",numberCount); for (int i = 0; i < numberCount; i++) { Console.Write(numbers[i].ToString() + "\t"); } Console.WriteLine("\n您要对这{0}个数字进行什么排序?(1表示升序,2表示降序)",numberCount); int method = Convert.ToInt32(Console.ReadLine()); while (method != 1 && method != 2) { Console.WriteLine("只能输入1或者2,请您重新输入!"); method = Convert.ToInt32(Console.ReadLine()); } //调用排序方法 ExecuteSortedMethod(numbers, method); Console.WriteLine("排序后的结果为:"); for (int i = 0; i < numberCount; i++) { Console.Write(numbers[i].ToString() + "\t"); } Console.WriteLine("\n----------刚刚冒泡排序法----------"); Console.ReadKey(); } // 接受数字参数和排序方法参数,进行冒泡排序 ///<param name="sortedMethod">排序方法标识:1为升序,2为降序</param> public static void ExecuteSortedMethod(int[] num, int sortedMethod) { if (sortedMethod == 1) //升序排列 { for (int i = 0; i < num.Length -1; i++) { for (int j = 0; j < num.Length - 1 - i; j++) { if (num[j] > num[j + 1]) { int temp = num[j]; num[j] = num[j + 1]; num[j + 1] = temp; } } } } if (sortedMethod == 2) //降序排列 { for (int i = 0; i < num.Length - 1; i++) { for (int j = 0; j < num.Length - 1 - i; j++) { if (num[j] < num[j + 1]) { int temp = num[j]; num[j] = num[j + 1]; num[j + 1] = temp; } } } } } } }
冒泡排序简单版本
using System; namespace BubbleSorter { public class BubbleSorter { public void Sort(int [] list) { int i,j,temp; bool done=false; j=1; while((j<list.Length)&&(!done)) { done=true; for(i=0;i<list.Length-j;i++) { if(list[i]>list[i+1]) { done=false; temp=list[i]; list[i]=list[i+1]; list[i+1]=temp; } } j++; } } } public class MainClass { public static void Main() { int[] iArrary=new int[]{24,51,1,66,13,87,46,51,77,9,-4}; BubbleSorter sh=new BubbleSorter(); sh.Sort(iArrary); for(int m=0;m<iArrary.Length;m++) Console.Write("{0} ",iArrary[m]); Console.WriteLine(); Console.Read(); } } }