Last active
November 12, 2021 13:22
-
-
Save vivek781113/652057f9c9a4a6ff72f7bface2246cb4 to your computer and use it in GitHub Desktop.
Revisions
-
vivek781113 revised this gist
Nov 12, 2021 . 1 changed file with 0 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +0,0 @@ -
vivek781113 revised this gist
Nov 12, 2021 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 { } -
vivek781113 revised this gist
Nov 12, 2021 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1 +1,4 @@ public interface ISort { public void Sort(int[] nums) } -
vivek781113 revised this gist
Nov 12, 2021 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ -
vivek781113 revised this gist
Jul 10, 2021 . No changes.There are no files selected for viewing
-
vivek781113 revised this gist
Jul 10, 2021 . No changes.There are no files selected for viewing
-
vivek781113 revised this gist
Jul 10, 2021 . No changes.There are no files selected for viewing
-
vivek781113 revised this gist
Jul 10, 2021 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 (nums[0], nums[i]) = (nums[i], nums[0]); heapsize--; MaxHeapify(nums, 0, heapsize); -
vivek781113 revised this gist
Jul 10, 2021 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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--) { //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); } return nums; -
vivek781113 revised this gist
Jul 10, 2021 . No changes.There are no files selected for viewing
-
vivek781113 renamed this gist
Jul 10, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
vivek781113 renamed this gist
Jul 10, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
vivek781113 revised this gist
Jul 10, 2021 . 1 changed file with 114 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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++; } } } -
vivek781113 created this gist
Jul 10, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@