Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Sorts
  4. {
  5.     public class Class1
  6.     {
  7.         public static void BubbleSort(int[] array)
  8.         {
  9.             for (int i = 0; i < array.Length; i++)
  10.             {
  11.                 for (int j = 0; j < array.Length - i - 1; j++)
  12.                 {
  13.                     if (array[j] > array[j + 1])
  14.                     {
  15.                         int t = array[j];
  16.                         array[j] = array[j + 1];
  17.                         array[j + 1] = t;
  18.                     }
  19.                 }
  20.             }
  21.         }
  22.  
  23.         public static void Quicksort(int[] array, int lo, int hi)
  24.         {
  25.             if (lo != hi)
  26.             {
  27.                 int l = lo;
  28.                 int h = hi;
  29.                 while (l < h)
  30.                 {
  31.                     while (array[l] < array[(l + h) / 2])
  32.                         l++;
  33.                     while (array[h] > array[(l + h) / 2])
  34.                         h--;
  35.                     if (l <= h)
  36.                     {
  37.                         int t = array[h];
  38.                         array[h] = array[l];
  39.                         array[l] = t;
  40.                     }
  41.                 }
  42.  
  43.                 Quicksort(array, lo, (l + h) / 2);
  44.                 Quicksort(array, (l + h) / 2 + 1, hi);
  45.             }
  46.             else
  47.             {
  48.                 return;
  49.             }
  50.         }
  51.  
  52.         public static void SelectionSort(int[] array)
  53.         {
  54.             for (int j = 0; j < array.Length; j++)
  55.             {
  56.                 int min = array[j];
  57.                 int minindex = j;
  58.                 for (int i = j; i < array.Length; i++)
  59.                 {
  60.                     if (array[i] < min)
  61.                         minindex = i;
  62.                 }
  63.  
  64.                 int t = array[j];
  65.                 array[j] = array[minindex];
  66.                 array[minindex] = t;
  67.             }
  68.         }
  69.  
  70.  
  71.         public static void InsetionSort(int[] array)
  72.         {
  73.             int key;
  74.             for (int j = 1; j < array.Length; j++)
  75.             {
  76.                 key = array[j];
  77.                 int i = j - 1;
  78.                 while (i >= 0 && array[i] > key)
  79.                 {
  80.                     array[i + 1] = array[i];
  81.                     i--;
  82.                 }
  83.  
  84.                 array[i + 1] = key;
  85.             }
  86.         }
  87.  
  88.         public static void Heapify(int[] a, int l)
  89.         {
  90.             for (int i = (int) (Math.Log(2, a.Length) - 1); i > 1; i--)
  91.             {
  92.                 if (2 * i < a.Length)
  93.                 {
  94.                     if ((a[2 * i] > a[i]) || ((a[2 * i] > a[2 * i + 1])))
  95.                     {
  96.                         int t = a[2 * i];
  97.                         a[2 * i] = a[i];
  98.                         a[i] = t;
  99.                     }
  100.                 }
  101.  
  102.                 if (2 * i + 1 < a.Length)
  103.                 {
  104.                     if ((a[2 * i + 1] > a[i]) || ((a[2 * i + 1] > a[2 * i])))
  105.                     {
  106.                         int t = a[2 * i + 1];
  107.                         a[2 * i + 1] = a[i];
  108.                         a[i] = t;
  109.                     }
  110.                 }
  111.             }
  112.  
  113.             int k = a[a.Length - 1];
  114.             a[a.Length - 1] = a[0];
  115.             a[0] = k;
  116.             if (l > 0)
  117.             {
  118.                 Heapify(a, l - 1);
  119.             }
  120.         }
  121.  
  122.         public static void PiramideSort(int[] a)
  123.         {
  124.             Heapify(a, a.Length);
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement