longht88 Mod
Tổng số bài gửi : 152 Thanks : 7 Join date : 18/05/2010 Age : 35 Đến từ : /\__Nhà Tui__/\
| Tiêu đề: BubbleSort Cải Tiến Sun 31 Oct 2010, 03:24 | |
| - Code:
-
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace BuBBleSort { class Program { static void Main(string[] args) { Console.Write("Nhap so phan tu : "); int n = Int32.Parse(Console.ReadLine()); Console.WriteLine(); Random m_random = new Random(); a = new int[n]; for (int i = 0; i < n; i++) { a[i] = m_random.Next(0, 100); } Console.WriteLine(); for (int i = 0; i < n; i++) { Console.Write("{0} \t", a[i]); } Console.WriteLine();
m_BubbleSort(a, n);
for (int i = 0; i < n; i++) { Console.Write("{0} \t", a[i]); }
} static int[] a; static int n; public static void m_BubbleSort(int[] a,int n) { int i =0; int j =n-1; while(i<=j) { for (int x = j; x > i;x--) { if (a[x] < a[x - 1]) DoiCho(x, (x - 1)); } for (int y = i; y < j - 1; y++) { if (a[y] > a[y + 1]) { DoiCho(y, y + 1); }
} i++; j--;
} }
public static void DoiCho(int x,int y) { int t; t =a[x]; a[x]=a[y]; a[y]=t; }
} }
| |
|