标签:style blog http io color ar sp for div
最近发现C#基础渣得不行啊,狂轰乱补中,以下是敲出来的冒泡排序,以便记忆。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading; 6 using System.Threading.Tasks; 7 8 namespace 冒泡排序Demo 9 { 10 class Program 11 { 12 public static Thread thread = null;//用于调用排序函数的进程 13 public static int[] array = new int[10] { 9, 3, 6, 7, 12, 23, 54, 78, 1, 34 };//共10个值 14 public static Thread th = null;//显示结果的进程 15 16 17 public static void Bubble_Sort(int[] arr)//关键是不是未引用对象static的,用线程调用时会报错“未引用对象” 18 { 19 for (int i = 0; i < arr.Length - 1; i++)//arr.Length-1表明一共有10个值,但只需比较9次 20 { 21 for (int j = 0; j < arr.Length - 1 - i; j++)//arr.Length-1-i表明,最后的i个值可以不用比较 22 { 23 if (arr[j] > arr[j + 1])//不用第三方变量交换 24 { 25 arr[j] = arr[j + 1] + arr[j]; 26 arr[j + 1] = arr[j] - arr[j + 1]; 27 arr[j] = arr[j] - arr[j + 1]; 28 } 29 } 30 } 31 } 32 public static void show() 33 { 34 foreach (int demo in array) 35 { 36 Console.Write(demo); 37 Console.Write(" "); 38 } 39 } 40 /// <summary> 41 /// 冒泡排序 42 /// </summary> 43 /// <param name="args"></param> 44 static void Main(string[] args) 45 { 46 47 //for(i = 0;i<array.Length ;i++) 48 //{ 49 // array[i] = Console.Read(); 50 //} 51 thread = new Thread(delegate() { Bubble_Sort(array); });//因为Bubble_Sort()方法有参数,所以写成,delegate(){Bubble_Sort(array);} 52 thread.IsBackground = true; 53 thread.Start(); 54 //thread.Abort(); 55 Thread.Sleep(10);//先让主线程休眠 56 57 th = new Thread(show); 58 th.IsBackground = true; 59 th.Start(); 60 Thread.Sleep(1000); 61 //thread.Abort(); 62 } 63 } 64 }
闲来无事,写了个冒泡复习复习,往后还要加上泛型,委托,事件等等...纯属为了学习...
博客园 :http://www.cnblogs.com/gu-zhan/ 非道
标签:style blog http io color ar sp for div
原文地址:http://www.cnblogs.com/gu-zhan/p/4080324.html