C # implements all classical sorting algorithms

Source: Internet
Author: User

Select Sort


class Selectionsorter


{


private int min;


public void Sort (int[] arr)


  {


for (int i = 0; i < arr. Length-1; ++i)


    {


min = i;


for (int j = i + 1; j < arr. Length; ++J)


      {


if (Arr[j] < arr[min])


min = j;


      }


int t = arr[min];


Arr[min] = arr[i];


arr[i] = t;


    }


  }


static void Main (string[] args)


  {


int[] array = new int[] {1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47};


selectionsorter s = new Selectionsorter ();


S.sort (array);


foreach (int m in array)


Console.WriteLine ("{0}", m);


  }


}


//Bubble sort


class Ebullitionsorter


{


public void Sort (int[] arr)


  {


int I, j, temp;


bool done = false;


j = 1;


while (J < arr. Length) && (!done))//judge the lengths


    {


done = true;


for (i = 0; i < arr. Length-j; i++)


      {


if (Arr[i] > arr[i + 1])


        {


done = false;


temp = arr[i];


Arr[i] = arr[i + 1];//Exchange data


arr[i + 1] = temp;


        }


      }


J + +;


    }


  }





static void Main (string[] args)


  {


int[] array = new int[] {1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47};


ebullitionsorter e = new Ebullitionsorter ();


E.sort (array);


foreach (int m in array)


Console.WriteLine ("{0}", m);





  }


}


//Quick Sort


class Quicksorter


{


private void Swap (ref int l, ref int R)


  {


int temp;


temp = l;


L = r;


r = temp;


  }


public void Sort (int[] list, int low, int high)


  {


int pivot;//Storage sub-Fulcrum


int L, R;


int mid;


if (high <= low)


return;


else if (high = = low + 1)


    {


if (List[low] > List[high])


swap (ref List[low], ref list[high]);


return;


    }


mid = (low + high) >> 1;


pivot = List[mid];


swap (ref List[low], ref list[mid]);


L = low + 1;


r = high;


do


    {


while (l <= R && List[l] < pivot)


l++;


while (List[r] >= pivot)


r--;


if (L < R)


swap (ref list[l], ref list[r]);


} while (L < R);


List[low] = List[r];


List[r] = pivot;


if (low + 1 < R)


Sort (list, low, r-1);


if (r + 1 < high)


Sort (list, R + 1, high);


  }





static void Main (string[] args)


  {


int[] iarrary = new int[] {1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47};


Quicksorter q = new Quicksorter ();


Q.sort (iarrary, 0, 13);


for (int m = 0; M <= m++)


Console.WriteLine ("{0}", Iarrary[m]);


  }


}


//Insert Sort


public class Insertionsorter


{


public void Sort (int[] arr)


  {


for (int i = 1; i < arr. Length; i++)


    {


int t = arr[i];


int j = i;


while ((J > 0) && (arr[j-1] > t)


      {


Arr[j] = arr[j-1];//Exchange Order


--j;


      }


arr[j] = t;


    }


  }


static void Main (string[] args)


  {


int[] array = new int[] {1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47};


Insertionsorter i = new Insertionsorter ();


I.sort (array);


foreach (int m in array)


Console.WriteLine ("{0}", m);


  }


}


//Hill sort


public class Shellsorter


{


public void Sort (int[] arr)


  {


INT Inc;


for (inc = 1; Inc <= arr. LENGTH/9; inc = 3 * inc + 1);


for (; Inc > 0; inc/= 3)


    {


for (int i = inc + 1; I <= arr. Length; i = + Inc)


      {


int t = arr[i-1];


int j = i;


while ((J > Inc) && (Arr[j-inc-1] > t)


        {


arr[j-1] = arr[j-inc-1];//Exchange Data


J-= Inc;


        }


arr[j-1] = t;


      }


    }


  }





static void Main (string[] args)


  {


int[] array = new int[] {1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47};


shellsorter s = new Shellsorter ();


S.sort (array);


foreach (int m in array)


Console.WriteLine ("{0}", m);





  }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.