Skip to content

Instantly share code, notes, and snippets.

@vivek781113
Last active November 12, 2021 13:22
Show Gist options
  • Select an option

  • Save vivek781113/652057f9c9a4a6ff72f7bface2246cb4 to your computer and use it in GitHub Desktop.

Select an option

Save vivek781113/652057f9c9a4a6ff72f7bface2246cb4 to your computer and use it in GitHub Desktop.

Revisions

  1. vivek781113 revised this gist Nov 12, 2021. 1 changed file with 0 additions and 9 deletions.
    9 changes: 0 additions & 9 deletions StrategyPatternDemo.cs
    Original file line number Diff line number Diff line change
    @@ -1,9 +0,0 @@
    public interface ISort
    {
    public void Sort(int[] nums)
    }

    public class MergeSort : ISort
    {

    }
  2. vivek781113 revised this gist Nov 12, 2021. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions StrategyPatternDemo.cs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,9 @@
    public interface ISort
    {
    public void Sort(int[] nums)
    }

    public class MergeSort : ISort
    {

    }
  3. vivek781113 revised this gist Nov 12, 2021. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion StrategyPatternDemo.cs
    Original file line number Diff line number Diff line change
    @@ -1 +1,4 @@
    ‎‎​
    public interface ISort
    {
    public void Sort(int[] nums)
    }
  4. vivek781113 revised this gist Nov 12, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions StrategyPatternDemo.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ‎‎​
  5. vivek781113 revised this gist Jul 10, 2021. No changes.
  6. vivek781113 revised this gist Jul 10, 2021. No changes.
  7. vivek781113 revised this gist Jul 10, 2021. No changes.
  8. vivek781113 revised this gist Jul 10, 2021. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion SortingAlgo.cs
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,6 @@ public int[] HeapSort(int[] nums)
    for (int i = heapsize; i > 0; i--)
    {
    //Extract Max OR min by swapping the root element with leaf element
    System.Console.WriteLine($"{nums[0]} -- {nums[i]}");
    (nums[0], nums[i]) = (nums[i], nums[0]);
    heapsize--;
    MaxHeapify(nums, 0, heapsize);
  9. vivek781113 revised this gist Jul 10, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions SortingAlgo.cs
    Original file line number Diff line number Diff line change
    @@ -19,13 +19,13 @@ public int[] HeapSort(int[] nums)
    //nums = BuildMaxHeap(nums);

    int heapsize = nums.Length - 1;
    for (int i = heapsize; i > 0; i--) //this loop is running N-1 TO 1 as last would be sorted
    for (int i = heapsize; i > 0; i--)
    {
    //Extract Max OR min by swapping the root element with leaf element
    System.Console.WriteLine($"{nums[0]} -- {nums[i]}");
    (nums[0], nums[i]) = (nums[i], nums[0]); //3, 6, 8, 2, 1, 4, 9
    (nums[0], nums[i]) = (nums[i], nums[0]);
    heapsize--;
    MaxHeapify(nums, 0, heapsize);//3, 6, 8, 2, 1, 4, 9
    MaxHeapify(nums, 0, heapsize);
    }

    return nums;
  10. vivek781113 revised this gist Jul 10, 2021. No changes.
  11. vivek781113 renamed this gist Jul 10, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  12. vivek781113 renamed this gist Jul 10, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  13. vivek781113 revised this gist Jul 10, 2021. 1 changed file with 114 additions and 1 deletion.
    115 changes: 114 additions & 1 deletion SortingAlgo
    Original file line number Diff line number Diff line change
    @@ -1 +1,114 @@
    ‎‎​
    public class SortAlgorithm
    {

    /*
    * Below for loop is alog for build heap
    * FOR (N/2-1) TO 0 <= Range of internal nodes
    * HEAPIFIY(arr, i)
    */
    public void BuildMaxHeap(int[] arr)
    {
    int len = arr.Length;
    for (int i = (len / 2 - 1); i >= 0; i--)
    MaxHeapify(arr, i, len - 1);
    }

    public int[] HeapSort(int[] nums)
    {

    //nums = BuildMaxHeap(nums);

    int heapsize = nums.Length - 1;
    for (int i = heapsize; i > 0; i--) //this loop is running N-1 TO 1 as last would be sorted
    {
    //Extract Max OR min by swapping the root element with leaf element
    System.Console.WriteLine($"{nums[0]} -- {nums[i]}");
    (nums[0], nums[i]) = (nums[i], nums[0]); //3, 6, 8, 2, 1, 4, 9
    heapsize--;
    MaxHeapify(nums, 0, heapsize);//3, 6, 8, 2, 1, 4, 9
    }

    return nums;

    }

    private void MaxHeapify(int[] array, int parentIndex, int indexLimit)
    {
    int leftChildIndex = 2 * parentIndex + 1;
    int rightChildIndex = 2 * parentIndex + 2;
    int maxIndex = parentIndex;
    if (leftChildIndex <= indexLimit && array[leftChildIndex] > array[parentIndex])
    {
    maxIndex = leftChildIndex;
    }

    if (rightChildIndex <= indexLimit && array[rightChildIndex] > array[maxIndex])
    {
    maxIndex = rightChildIndex;
    }

    if (maxIndex != parentIndex)
    {
    (array[parentIndex], array[maxIndex]) = (array[maxIndex], array[parentIndex]);
    MaxHeapify(array, maxIndex, indexLimit);
    }
    }

    public int[] MergeSort(int[] nums)
    {
    int size = nums.Length;
    if (size < 2)
    return nums;
    int mid = size / 2;
    int[] leftHalf = new int[mid];
    int[] rightHalf = new int[size - mid];

    for (int i = 0; i < mid; i++)
    {
    leftHalf[i] = nums[i];
    }
    for (int i = mid; i < size; i++)
    {
    rightHalf[i-mid] = nums[i];
    }
    MergeSort(leftHalf);
    MergeSort(rightHalf);
    Merge(nums, leftHalf, rightHalf, mid, size - mid);
    return nums;
    }

    private void Merge(int[] arr, int[] leftHalf, int[] rightHalf, int leftBoundary, int rightBoundary)
    {
    int leftIndex = 0;
    int rightIndex = 0;
    int arrayIndex = 0;

    while (leftIndex < leftBoundary && rightIndex < rightBoundary)
    {
    if (leftHalf[leftIndex] < rightHalf[rightIndex])
    {
    arr[arrayIndex] = leftHalf[leftIndex];
    leftIndex++;
    }
    else
    {
    arr[arrayIndex] = rightHalf[rightIndex];
    rightIndex++;
    }

    arrayIndex++;
    }
    while (leftIndex < leftBoundary)
    {
    arr[arrayIndex] = leftHalf[leftIndex];
    arrayIndex++;
    leftIndex++;
    }
    while (rightIndex < rightBoundary)
    {
    arr[arrayIndex] = rightHalf[rightIndex];
    arrayIndex++;
    rightIndex++;
    }
    }
    }
  14. vivek781113 created this gist Jul 10, 2021.
    1 change: 1 addition & 0 deletions SortingAlgo
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ‎‎​