Advertisement
KingAesthetic

C# Example 3

Aug 13th, 2024 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4.  
  5. // Kaizer here. I implemented and compared different sorting algorithms using 3 main ones. quicksort, mergesort, and heapsort, in terms of performance.
  6. class Program
  7. {
  8.     // function to print an array
  9.     static void PrintArray(List<int> arr)
  10.     {
  11.         foreach (int num in arr)
  12.         {
  13.             Console.Write(num + " ");
  14.         }
  15.         Console.WriteLine();
  16.     }
  17.  
  18.     // quicksort implementation
  19.     static int Partition(List<int> arr, int low, int high)
  20.     {
  21.         int pivot = arr[high];
  22.         int i = low - 1;
  23.         for (int j = low; j < high; j++)
  24.         {
  25.             if (arr[j] < pivot)
  26.             {
  27.                 i++;
  28.                 int temp = arr[i];
  29.                 arr[i] = arr[j];
  30.                 arr[j] = temp;
  31.             }
  32.         }
  33.         int temp1 = arr[i + 1];
  34.         arr[i + 1] = arr[high];
  35.         arr[high] = temp1;
  36.         return i + 1;
  37.     }
  38.  
  39.     static void Quicksort(List<int> arr, int low, int high)
  40.     {
  41.         if (low < high)
  42.         {
  43.             int pi = Partition(arr, low, high);
  44.             Quicksort(arr, low, pi - 1);
  45.             Quicksort(arr, pi + 1, high);
  46.         }
  47.     }
  48.  
  49.     // mergesort implementation
  50.     static void Merge(List<int> arr, int left, int mid, int right)
  51.     {
  52.         int n1 = mid - left + 1;
  53.         int n2 = right - mid;
  54.         List<int> L = new List<int>(n1);
  55.         List<int> R = new List<int>(n2);
  56.  
  57.         for (int i = 0; i < n1; i++)
  58.             L.Add(arr[left + i]);
  59.         for (int j = 0; j < n2; j++)
  60.             R.Add(arr[mid + 1 + j]);
  61.  
  62.         int i1 = 0, j1 = 0, k = left;
  63.         while (i1 < n1 && j1 < n2)
  64.         {
  65.             if (L[i1] <= R[j1])
  66.             {
  67.                 arr[k] = L[i1];
  68.                 i1++;
  69.             }
  70.             else
  71.             {
  72.                 arr[k] = R[j1];
  73.                 j1++;
  74.             }
  75.             k++;
  76.         }
  77.  
  78.         while (i1 < n1)
  79.         {
  80.             arr[k] = L[i1];
  81.             i1++;
  82.             k++;
  83.         }
  84.  
  85.         while (j1 < n2)
  86.         {
  87.             arr[k] = R[j1];
  88.             j1++;
  89.             k++;
  90.         }
  91.     }
  92.  
  93.     static void Mergesort(List<int> arr, int left, int right)
  94.     {
  95.         if (left < right)
  96.         {
  97.             int mid = left + (right - left) / 2;
  98.             Mergesort(arr, left, mid);
  99.             Mergesort(arr, mid + 1, right);
  100.             Merge(arr, left, mid, right);
  101.         }
  102.     }
  103.  
  104.     // heapsort implementation
  105.     static void Heapify(List<int> arr, int n, int i)
  106.     {
  107.         int largest = i;
  108.         int left = 2 * i + 1;
  109.         int right = 2 * i + 2;
  110.  
  111.         if (left < n && arr[left] > arr[largest])
  112.             largest = left;
  113.  
  114.         if (right < n && arr[right] > arr[largest])
  115.             largest = right;
  116.  
  117.         if (largest != i)
  118.         {
  119.             int swap = arr[i];
  120.             arr[i] = arr[largest];
  121.             arr[largest] = swap;
  122.             Heapify(arr, n, largest);
  123.         }
  124.     }
  125.  
  126.     static void Heapsort(List<int> arr)
  127.     {
  128.         int n = arr.Count;
  129.         for (int i = n / 2 - 1; i >= 0; i--)
  130.             Heapify(arr, n, i);
  131.  
  132.         for (int i = n - 1; i > 0; i--)
  133.         {
  134.             int temp = arr[0];
  135.             arr[0] = arr[i];
  136.             arr[i] = temp;
  137.             Heapify(arr, i, 0);
  138.         }
  139.     }
  140.  
  141.     // function to measure and compare sorting times
  142.     static void CompareSorts(List<int> arr)
  143.     {
  144.         List<int> arr1 = new List<int>(arr);
  145.         List<int> arr2 = new List<int>(arr);
  146.         List<int> arr3 = new List<int>(arr);
  147.  
  148.         Stopwatch stopwatch = new Stopwatch();
  149.  
  150.         stopwatch.Start();
  151.         Quicksort(arr1, 0, arr1.Count - 1);
  152.         stopwatch.Stop();
  153.         Console.WriteLine($"Quicksort time: {stopwatch.Elapsed.TotalSeconds} seconds");
  154.  
  155.         stopwatch.Restart();
  156.         Mergesort(arr2, 0, arr2.Count - 1);
  157.         stopwatch.Stop();
  158.         Console.WriteLine($"Mergesort time: {stopwatch.Elapsed.TotalSeconds} seconds");
  159.  
  160.         stopwatch.Restart();
  161.         Heapsort(arr3);
  162.         stopwatch.Stop();
  163.         Console.WriteLine($"Heapsort time: {stopwatch.Elapsed.TotalSeconds} seconds");
  164.     }
  165.  
  166.     static void Main(string[] args)
  167.     {
  168.         List<int> arr = new List<int> { 12, 11, 13, 5, 6, 7 };
  169.         Console.Write("Original array: ");
  170.         PrintArray(arr);
  171.  
  172.         CompareSorts(arr);
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement