标签:
冒泡排序
class CArray { private int[] arr; private int upper; private int numElements; public CArray(int size) { arr =new int[size]; upper = size - 1; numElements = 0; } public void Insert(int item) { arr[numElements] = item; numElements++; } public void DisplayElements() { for (int i = 0; i <= upper; i++) { Console.WriteLine(arr[i]+" "); } } public void Clear() { for (int i = 0; i <= upper; i++) { arr[i] = 0; } numElements = 0; } } class Program { static void Main(string[] args) { int[] nums = new int[10]; //nums.DisplayElements(); //for (int i = 0; i <=49; i++) //{ // nums.Insert(i); //} //nums.DisplayElements(); Random rnd = new Random(100); for (int i = 0; i <10; i++) { nums[i]=(rnd.Next(0,100)); } DisplayNums(nums); Console.WriteLine("start"); BubbleSort(nums); Console.WriteLine("end"); DisplayNums(nums); Console.ReadKey(); } public static void BubbleSort(int[] nums) { for (int i = 0; i < nums.Length-1; i++) { for (int j = nums.Length - 1; j > i;j-- ) { if(nums[j]>nums[j-1]) { int temp = nums[j]; nums[j]=nums[j-1]; nums[j - 1] = temp; } } DisplayNums(nums); } } public static void BubbleSort2(int[] nums) { Boolean IsChange = false; int temp; for (int i = 0; i < nums.Length-1; i++) { for (int j = nums.Length - 1; j > i;j-- ) { if(nums[j]>nums[j-1]) { temp=nums[j]; nums[j]=nums[j-1]; nums[j-1]=temp; IsChange = true; } } if(!IsChange) { break; } } } public static void DisplayNums(int[] nums) { for (int i = 0; i < nums.Length; i++) { Console.Write(" "+nums[i]); } Console.WriteLine(" *"); } }
选择排序
class Program { static void Main(string[] args) { int[] nums = new int[10]; //nums.DisplayElements(); //for (int i = 0; i <=49; i++) //{ // nums.Insert(i); //} //nums.DisplayElements(); Random rnd = new Random(100); for (int i = 0; i <10; i++) { nums[i]=(rnd.Next(0,100)); } DisplayNums(nums); Console.WriteLine("start"); SelectionSort(nums); Console.WriteLine("end"); DisplayNums(nums); Console.ReadKey(); } public static void BubbleSort(int[] nums) { for (int i = 0; i < nums.Length-1; i++) { for (int j = nums.Length - 1; j > i;j-- ) { if(nums[j]>nums[j-1]) { int temp = nums[j]; nums[j]=nums[j-1]; nums[j - 1] = temp; } } DisplayNums(nums); } } public static void BubbleSort2(int[] nums) { Boolean IsChange = false; int temp; for (int i = 0; i < nums.Length-1; i++) { for (int j = nums.Length - 1; j > i;j-- ) { if(nums[j]>nums[j-1]) { temp=nums[j]; nums[j]=nums[j-1]; nums[j-1]=temp; IsChange = true; } } if(!IsChange) { break; } } } public static void SelectionSort(int[] nums) { int temp; int nindex = 0; for (int i = 0; i < nums.Length-1; i++) { nindex = i; for (int j = i+1; j < nums.Length; j++) { //Console.Write(i+ "s "); if(nums[nindex] > nums[j]) nindex = j; } if(nindex!=i) { temp=nums[i]; nums[i]=nums[nindex]; nums[nindex] = temp; } DisplayNums(nums); } } public static void DisplayNums(int[] nums) { for (int i = 0; i < nums.Length; i++) { Console.Write(" "+nums[i]); } Console.WriteLine(" *"); } }
插入排序
class Program { static void Main(string[] args) { int[] nums = new int[10]; Random rnd = new Random(100); for (int i = 0; i < 10; i++) { nums[i] = (rnd.Next(0, 100)); } DisplayNums(nums); Console.WriteLine("start"); InsertSort(nums); Console.WriteLine("end"); DisplayNums(nums); Console.ReadKey(); } public static void InsertSort(int[] nums) { for (int i = 1; i < nums.Length; i++) { int j = i - 1; int temp = nums[i]; while (j >= 0 && nums[j] > temp) { nums[j + 1] = nums[j]; j--; } if (j != i - 1) nums[j + 1] = temp; DisplayNums(nums); } } //public static void InsertSort2(int[] nums) //{ // for (int i = 1; i < nums.Length; i++) // { // int j = 0; // while (j < i && nums[j] < nums[i]) // j++; // if (j < i) // { // int k = j; // int temp = nums[i]; // while (k > j) // { // nums[k] = nums[k - 1]; // k--; // } // nums[k] = temp; // } // } // DisplayNums(nums); //} public static void DisplayNums(int[] nums) { for (int i = 0; i < nums.Length; i++) { Console.Write(" " + nums[i]); } Console.WriteLine(" *"); } }
二分查找
class Program { static void Main(string[] args) { int[] nums = new int[10]; // Random rnd = new Random(100); for (int i = 0; i < 10; i++) { nums[i] = i;//(rnd.Next(0, 100)); } DisplayNums(nums); Console.WriteLine("start"); //InsertSort(nums); Console.WriteLine(" " + Binsearch2(nums, 0, 9, 5)); Console.WriteLine("end"); DisplayNums(nums); Console.ReadKey(); } static int BinSearch(int[] nums, int low, int high, int key) { if (low <= high) { int mid = (low + high) / 2; if (key < nums[mid]) return BinSearch(nums, low, mid - 1, key); else if (key > nums[mid]) return BinSearch(nums, mid + 1, high, key); else return mid; } else { return -1; } } static int Binsearch2(int[] nums,int low,int high,int key) { while (low <= high) { int mid=(low + high)/2; if (key == mid) return mid; if (key > mid) low = mid + 1; if (key < mid) high = mid - 1; } return -1; } public static void DisplayNums(int[] nums) { for (int i = 0; i < nums.Length; i++) { Console.Write(" " + nums[i]); } Console.WriteLine(" *"); } }
堆栈 using System.Collections;
class CStack { private int p_index; private ArrayList list; public CStack() { list = new ArrayList(); p_index = -1; } public int count { get { return list.Count; } } public void push(object item) { list.Add(item); p_index++; } public object pop() { object obj=list[p_index]; list.Remove(p_index); p_index--; return obj; } public void clear() { list.Clear(); p_index = -1; } public object peek() { return list[p_index]; } } class Program { static void Main(string[] args) { bool isPali=true; Stack list = new Stack(); string ch; string world = "sees"; for (int i = 0; i < world.Length; i++) { list.Push(world.Substring(i,1)); } int pos = 0; while(list.Count>0) { ch=list.Pop().ToString(); if(ch!=world.Substring(pos,1)) { isPali = false; break; } pos++; } if (isPali) { Console.WriteLine(" " + world); } else { Console.WriteLine(" not "); } Console.ReadKey(); } }
标签:
原文地址:http://www.cnblogs.com/futengsheng/p/5627835.html