Code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace sapxeplai { class Program { public static void Swap(ref int a, ref int b) { int t=a; a = b; b=t; } public static void interchangesort(int[] a) { for (int i = 0; i < a.Length-1; i++) for (int j = i + 1; j < a.Length; j++) if (a[i] > a[j]) Swap(ref a[i],ref a[j]); } public static void bubblesort(int[] a) { for(int i=0;i<a.Length-1;i++) for(int j=a.Length-1;j>i;j--) if(a[j] < a[j-1]) Swap(ref a[j] ,ref a[j-1]); } public static void insertsort(int[] a) { int x, vt; for (int i = 1; i < a.Length; i++) { x = a[i]; vt = i - 1; while (vt >= 0 && a[vt] > x) { a[vt +1] = a[vt]; vt--; } a[vt +1] = x; } } public static void xuat(int[] a) { for (int i = 0; i < a.Length; i++) Console.Write(a[i].ToString() + " "); } static void Main(string[] args) { int[] mang = new int[] { 9,8,1}; Console.WriteLine("mang truoc khi xep:"); xuat(mang); interchangesort(mang); //bubblesort(mang); // insertsort(mang); Console.WriteLine("\nmang sau khi xep:"); xuat(mang); Console.ReadLine(); } } }
Comment