minhlap Admin - Quản trị viên
Tổng số bài gửi : 129 Points : 374 Reputation : 5 Join date : 22/07/2009 Age : 34 Đến từ : TP Hồ Chí Minh
| Tiêu đề: Bài 2 sắp xếp nội Tue Sep 01, 2009 3:14 pm | |
| Tấc cả đều trong một lớp Mảng - Code:
-
using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Sort { class Mang { int[] a; public Mang(int n) { a = new int[n]; } public void nhap(int n) { string s; Console.WriteLine("Tao mang thu cong "); for (int i = 0; i < n; i++) { Console.Write("Nhap a[{0}] :", i); s = Console.ReadLine(); a[i] = Convert.ToInt32(s); } } public void nhaprand(int n) { Random s = new Random(); int x; Console.WriteLine("Tao mang ngau nhien"); for (int i = 0; i < n; i++) { x = s.Next() % 100; a[i] = x; } } public void xuat(int n) { Console.WriteLine("Mang vua nhap la :");
for (int i = 0; i < n; i++) { Console.WriteLine("Phan tu thu a[i] :{0}", a[i]); } } public void insertsort(int n) { int j,x; for (int i = 1; i < n; i++) { x = a[i]; j = i-1; while (j >= 0 && x < a[j]) { a[j+1] = a[j]; j--; } a[j+ 1] = x; } } void chen(int n)// tim phan tu nho nhat trong day con lai va doi cho { int i,j,k,x; for(i=0;i<n;i++) { x=a[i]; k=i; for(j=i+1;j<n;j++) { if(a[j]<x) { k=j; x=a[j]; } } a[k]=a[i]; a[i]=x; } } //--------------------------------------------------------- public void swap(int x,int y) { int z; z=a[x]; a[x]=a[y]; a[y]=z; } void bublesort(int n) { int i,j; for(i=0;i<n;i++) for(j=n-1;j>i;j--) if(a[j]<a[j-1]) swap(j,j-1); } void bublesort_flag(int n) { int i,j,ok; for(i=0;i<n;i++) { do { ok=0; for(j=n-1;j>i;j--) if(a[j]<a[j-1]) { swap(j,j-1); ok=1; } }while(ok!=0); } } //---------------------------------------------------- void shakersort(int n) { int i,l,r; l=0; r=n-1; do { for(i=r;i>l;i--) if(a[i]<a[i-1]) swap(i,i-1); l++; for(i=l;i<r;i++) if(a[i]>a[i+1]) swap(i,i+1); r--; }while(l<=r); } //------------------------------------- void sift(int q,int r) { int i, j; bool cont = true; int x; x=a[q]; i=q; j=i*2+1; while(j<=r && cont==true) { if(j<r && a[j]<a[j+1]) j++; if(x<a[j]) { a[i]=a[j]; i=j; j=i*i+1; } else cont=false; }; a[i]=x; } void heapsort(int n) { int i; for(i=n/2;i>=0;i--) sift(i,n-1); i=n-1; do { swap(0,i); i--; sift(0,i); }while(i>0); } //----------------------------------------- void qsort(int l,int r) { int i,j; int x; x=a[(l+r)/2]; i=l; j=r; do { while(a[i]<x) i++; while(a[j]>x) j--; if(i<=j) { swap(i,j); i++; j--; } }while(i<=j); if(l<j) qsort(l,j); if(i<r) qsort(i,r); } void quicksort(int n) { qsort(0,n-1); } //--------------------------------------------------- void shellsort(int n) { int i,j,t=5,k,m; int[] h=new int[5] {35,15,7,3,1}; int x; for(m=0;m<t;m++) { k=h[m]; for(i=k;i<n;i++) { x=a[i]; j=i-k; while(j>=0 && x<a[j]) { a[j+k]=a[j]; j=j-k; } a[j+k]=x; } } } //--------------------------------------- static void Main(string[] args) { int n; string s; Console.Write("Nhap n "); s = Console.ReadLine(); n = Convert.ToInt32(s); Mang a = new Mang(n); a.nhaprand(n); a.xuat(n); a.bublesort(n); a.xuat(n); } } } | |
|