Advertisement
ItsNoah

SorterClassForCS

Mar 9th, 2022
776
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.95 KB | None | 0 0
  1. namespace HelloWorld
  2. {
  3.     class Sorter
  4.     {
  5.        
  6.     // ---------------------------------------- Bubble Sort ---------------------------------------- //
  7.  
  8.     public static void bubbleSort(double[] array) { // Method to Run
  9.         for (int i = 0; i < array.Length - 1; i++)
  10.             for (int j = 0; j < array.Length - i - 1; j++)
  11.                 if (array[j] > array[j + 1])
  12.                     swap(array, j, j + 1);
  13.     }
  14.  
  15.  
  16.     // ---------------------------------------- Insertion Sort ---------------------------------------- //
  17.  
  18.     public static void insertionSort(double[] array) { // Method to Run
  19.         for (int i = 1; i < array.Length; i++) {
  20.             double temp = array[i];
  21.             int j = i - 1;
  22.             while (j >= 0 && array[j] > temp) {
  23.                 array[j + 1] = array[j];
  24.                 j--;
  25.             }
  26.             array[j + 1] = temp;
  27.         }
  28.     }
  29.  
  30.  
  31.     // ---------------------------------------- Merge Sort ---------------------------------------- //
  32.  
  33.     public static void mergeSort(double[] array) { // Method to Run
  34.         Console.WriteLine("Mergesort is currently not working (Stack Overflow)...");
  35.     }
  36.    
  37.     public static void ERRORmergeSort(double[] array) { // Method to Run
  38.         int length = array.Length;
  39.  
  40.         int middle = length / 2;
  41.         double[] leftArray = new double[middle];
  42.         double[] rightArray = new double[length - middle];
  43.  
  44.         int i = 0;
  45.         int j = 0;
  46.  
  47.         for (; i < length; i++) {
  48.             if (i < middle)
  49.                 leftArray[i] = array[i];
  50.             else {
  51.                 rightArray[j] = array[i];
  52.                 j++;
  53.             }
  54.         }
  55.         mergeSort(leftArray);
  56.         mergeSort(rightArray);
  57.         merge(leftArray, rightArray, array);
  58.     }
  59.  
  60.     private static void merge(double[] leftArray, double[] rightArray, double[] array) {
  61.         int leftSize = array.Length / 2;
  62.         int rightSize = array.Length - leftSize;
  63.         int i = 0, l = 0, r = 0;
  64.  
  65.         while (l < leftSize && r < rightSize) {
  66.             if (leftArray[l] < rightArray[r]) {
  67.                 array[i] = leftArray[l];
  68.                 i++; l++;
  69.             } else {
  70.                 array[i] = rightArray[r];
  71.                 i++; r++;
  72.             }
  73.         }
  74.         while (l < leftSize) {
  75.             array[i] = leftArray[l];
  76.             i++; l++;
  77.         }
  78.         while (r < rightSize) {
  79.             array[i] = rightArray[r];
  80.             i++; r++;
  81.         }
  82.     }
  83.  
  84.  
  85.     // ---------------------------------------- Quick Sort ---------------------------------------- //
  86.  
  87.     public static void quickSort(double[] array) { // Method to Run
  88.         quickSort(array, 0, array.Length - 1);
  89.     }
  90.  
  91.     private static void quickSort(double[] array, int start, int end) {
  92.         if (end <= start) return;
  93.  
  94.         int pivot = partition(array, start, end);
  95.         quickSort(array, start, pivot - 1);
  96.         quickSort(array, pivot + 1, end);
  97.     }
  98.  
  99.     private static int partition(double[] array, int start, int end) {
  100.         double pivot = array[end];
  101.         int i = start - 1;
  102.  
  103.         for (int j = start; j <= end - 1; j++) {
  104.             if (array[j] < pivot) {
  105.                 i++;
  106.                 double tmp = array[i];
  107.                 array[i] = array[j];
  108.                 array[j] = tmp;
  109.             }
  110.         }
  111.         i++;
  112.  
  113.         double temp = array[i];
  114.         array[i] = array[end];
  115.         array[end] = temp;
  116.         return i;
  117.     }
  118.  
  119.     // ---------------------------------------- Selection Sort ---------------------------------------- //
  120.  
  121.     public static void selectionSort(double[] array) { // Method to Run
  122.         for (int i = 0; i < array.Length - 1; i++) {
  123.             int min = i;
  124.             for (int j = i + 1; j < array.Length; j++) {
  125.                 if (array[min] > array[j])
  126.                     min = j;
  127.             }
  128.  
  129.             double temp = array[i];
  130.             array[i] = array[min];
  131.             array[min] = temp;
  132.         }
  133.     }
  134.  
  135.  
  136.     // ---------------------------------------- Shell Sort ---------------------------------------- //
  137.  
  138.     public static void shellSort(double[] array) { // Method to Run
  139.         int h = 1;
  140.         while (h <= array.Length / 4) {
  141.             h = h * 4 + 1;
  142.         }
  143.  
  144.         while (h > 0) {
  145.             for (int outer = h; outer < array.Length; outer++) {
  146.                 double tmp = array[outer];
  147.                 int inner = outer;
  148.                 while (inner > h - 1 && array[inner - h ] >= tmp) {
  149.                     array[inner] = array[inner - h];
  150.                     inner -= h;
  151.                 }
  152.                 array[inner] = tmp;
  153.             }
  154.             h = (h - 1) / 4;
  155.         }
  156.     }
  157.  
  158.  
  159.     // ---------------------------------------- Heap Sort ---------------------------------------- //
  160.  
  161.     public static void heapSort(double[] array) { // Method to Run
  162.         buildMaxHeap(array);
  163.         for (int i = array.Length - 1; i > 0; i--) {
  164.             swap(array, i, 0);
  165.             seep(array, 0, i);
  166.         }
  167.     }
  168.     // create maxHeap tree in array
  169.     private static void buildMaxHeap(double[] arr) {
  170.         for (int i = (arr.Length / 2) - 1; i >= 0 ; i--) {
  171.             seep(arr, i, arr.Length);
  172.         }
  173.     }
  174.     // seep - Downheap
  175.     private static void seep(double[] arr, int i, int j) {
  176.  
  177.         while (i <= (j / 2) - 1) {
  178.  
  179.             // left child
  180.             int childIndex = ((i + 1) * 2) - 1;
  181.             // right child
  182.             if (childIndex + 1 <= j - 1) {
  183.                 if (arr[childIndex] < arr[childIndex + 1]) {
  184.                     childIndex++;
  185.                 }
  186.             }
  187.             // check if seep is neccessary
  188.             if (arr[i] < arr[childIndex]) {
  189.                 swap(arr, i, childIndex);
  190.                 i = childIndex;
  191.             } else break;
  192.         }
  193.     }
  194.  
  195.  
  196.  
  197.     // ---------------------------------------- Counting Sort ---------------------------------------- //
  198.  
  199.     public static void countingSort(double[] array) { // Method to Run
  200.         double max = array[0];
  201.         for (int i = 1; i < array.Length; i++)
  202.             if (array[i] > max) max = array[i];
  203.  
  204.         int[] helpArray = new int[(int)max + 1];
  205.         for (int i = 0; i < array.Length; i++)
  206.             helpArray[(int)array[i]]++;
  207.  
  208.         int pos = 0;
  209.         for (int i = 0; i <= max; i++) {
  210.             for (int j = 0; j < helpArray[i]; j++) {
  211.                 array[pos] = i;
  212.                 pos++;
  213.             }
  214.         }
  215.     }
  216.  
  217.  
  218.     // ---------------------------------------- Radix Sort ---------------------------------------- //
  219.  
  220.     public static void radixSort(double[] array) { // Method to Run
  221.         double max = array[0];
  222.         for (int i = 1; i < array.Length; i++)
  223.             if (array[i] > max) max = array[i];
  224.  
  225.         for (int factor = 1; max / factor > 0; factor *= 10)
  226.             countingsort(array, factor);
  227.     }
  228.  
  229.     private static void countingsort(double[] array, int factor) {
  230.         int len = array.Length;
  231.         double[] output = new double[len];
  232.         int a;
  233.         int[] counting = new int[10];
  234.         for (a = 0; a < 10; a++)
  235.             counting[a] = 0;
  236.  
  237.         for (a = 0; a < len; a++)
  238.             counting[ (int)(array[a] / factor) % 10 ]++;
  239.         for (a = 1; a < 10; a++)
  240.             counting[a] += counting[a - 1];
  241.  
  242.         for (a = len - 1; a >= 0; a--) {
  243.             output[counting[ (int)(array[a] / factor) % 10 ] - 1] = array[a];
  244.             counting[ (int)(array[a] / factor) % 10 ]--;
  245.         }
  246.  
  247.         for (a = 0; a < len; a++)
  248.             array[a] = output[a];
  249.     }
  250.  
  251.  
  252.     // ---------------------------------------- Bucket Sort ---------------------------------------- //
  253.  
  254.     public static void bucketSort(double[] array) { // Method to Run
  255.         double max = array[0];
  256.         for (int i = 1; i < array.Length; i++)
  257.             if (array[i] > max) max = array[i];
  258.         double[] bucket = new double[(int)max + 1];
  259.  
  260.         for (int i = 0; i < array.Length; i++)
  261.             bucket[(int)array[i]]++;
  262.  
  263.         int outPos = 0;
  264.         for (int i = 0; i < bucket.Length; i++) {
  265.             for (int j = 0; j < bucket[i]; j++) {
  266.                 array[outPos++] = i;
  267.             }
  268.         }
  269.     }
  270.  
  271.     // ---- Swap ---- //
  272.  
  273.     private static void swap(double[] arr, int a, int b) {
  274.         double k = arr[a];
  275.         arr[a] = arr[b];
  276.         arr[b] = k;
  277.     }
  278.  
  279.     }
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement