MergeSort algorithm in c# -


i wrote below code merge class :

class merge {      public static void sort(icomparable[] a)     {         sort(a, 0, a.length);     }      public static void sort(icomparable[] a, int low, int high)     {         int n = high - low;         if (n <= 1)             return;          int mid = low + n / 2;          sort(a, low, mid);         sort(a, mid, high);          icomparable[] aux = new icomparable[n];         int = low, j = mid;         (int k = 0; k < n; k++)         {             if (i == mid) aux[k] = a[j++];             else if (j == high) aux[k] = a[i++];             else if (a[j].compareto(a[i]) < 0) aux[k] = a[j++];             else aux[k] = a[i++];         }          (int k = 0; k < n; k++)         {             a[low + k] = aux[k];         }     }      private static boolean issorted(icomparable[] a)     {         (int = 1; < a.length; i++)             if (a[i].compareto(a[i - 1]) < 0) return false;         return true;     }  } 

and below code implementation. thought below code should't wrong! it's does't compile...

class program {     static void main(string[] args)     {     merge ms = new merge();      double[] myarray = { 80,10,52,7,36,7,67,1,8,54 };     console.writeline("first array is: \n");     (int k = 0; k < myarray.length; k++)     {         console.write(myarray[k]);         if (k<9)            console.write(" , ");     }     ms.sort(myarray);  // error here. does't compile !!!     console.writeline("\n");     console.writeline("\nsorted array is: \n ");     (int k = 0; k < myarray.length; k++)     {         console.write(myarray[k]);         if (k<9)            console.write(" , ");     }     console.readline();     } } 

it's does't compile. error in ms.sort(myarray);. doing wrong? please lead me...

regards

there 2 problems code:

  1. the signature doesn't match, icomparable[] not directly compatible double[] in case
  2. you cannot call sort method through instance directly

the minimal amount of changes fix make method generic, , call merge.sort instead of ms.sort.

here's how implement sort:

public static void sort<t>(t[] a)     t : icomparable<t> {     sort(a, 0, a.length); }  public static void sort<t>(t[] a, int low, int high)     t : icomparable<t> {     int n = high - low;     if (n <= 1)         return;      int mid = low + n / 2;      sort(a, low, mid);     sort(a, mid, high);      t[] aux = new t[n];     int = low, j = mid;     (int k = 0; k < n; k++)     {         if (i == mid) aux[k] = a[j++];         else if (j == high) aux[k] = a[i++];         else if (a[j].compareto(a[i]) < 0) aux[k] = a[j++];         else aux[k] = a[i++];     }      (int k = 0; k < n; k++)     {         a[low + k] = aux[k];     } } 

note changed using t instead of icomparable, , added constraint saying need t implements icomparable<t>.

additionally, change call this:

ms.sort(...); 

to this:

merge.sort(...); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -