From 07605f246dac3255f57b9ecaeda707a6f13ee879 Mon Sep 17 00:00:00 2001 From: thebrahminator Date: Mon, 1 Oct 2018 13:12:06 +0530 Subject: [PATCH 1/3] heap_sort.cpp Adds Heap Sort Algorithm This adds Heap Sort Algorithm which sorts the elements in ascending order. Closes https://github.com/NITSkmOS/Algorithms/issues/22 --- heap_sort/C++/heap_sort.cpp | 53 +++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 heap_sort/C++/heap_sort.cpp diff --git a/heap_sort/C++/heap_sort.cpp b/heap_sort/C++/heap_sort.cpp new file mode 100644 index 00000000..f6373701 --- /dev/null +++ b/heap_sort/C++/heap_sort.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; + +void heapify(int input[], int n, int i) +{ + int largest = i; + int left = 2*i + 1; + int right = 2*i + 2; + + if (left < n && input[left] > input[largest]) + largest = left; + + if (right < n && input[right] > input[largest]) + largest = right; + + if (largest != i) + { + swap(input[i], input[largest]); + + heapify(input, n, largest); + } +} + +void heapSort(int input[], int n) +{ + for (int i = n / 2 - 1; i >= 0; i--) + heapify(input, n, i); + for (int i=n-1; i>=0; i--) + { + swap(input[0], input[i]); + heapify(input, i, 0); + } +} +int main() +{ + int n; + cout<<"Enter the number of elements"<>n; + int array[n]; + cout<<"Enter the elements of array"<>array[i]; + } + heapSort(array, n); + cout << "Sorted array is \n"; + for(int i=0; i Date: Mon, 1 Oct 2018 13:12:06 +0530 Subject: [PATCH 2/3] heap_sort.cpp Adds Heap Sort Algorithm This adds Heap Sort Algorithm which sorts the elements in ascending order. Closes https://github.com/NITSkmOS/Algorithms/issues/22 --- README.md | 2 +- heap_sort/C++/heap_sort.cpp | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 heap_sort/C++/heap_sort.cpp diff --git a/README.md b/README.md index a21d0b07..f8e96586 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ This repository contains examples of various algorithms written on different pro | [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | | | [:octocat:](radix_sort/Python) | | [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | | | | [:octocat:](binary_search/Python) | | [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | | [:octocat:](bubble_sort/C++) | | | - +| [Heap Sort](https://en.wikipedia.org/wiki/Heapsort) | | [:octocat:](heap_sort/C++) | | | ## Implemented Data Structures diff --git a/heap_sort/C++/heap_sort.cpp b/heap_sort/C++/heap_sort.cpp new file mode 100644 index 00000000..4862b94f --- /dev/null +++ b/heap_sort/C++/heap_sort.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +void heapify(int input[], int n, int i) +{ + + int largest = i; // Assign the largest as root + int left = 2*i + 1; // Calculate the index of left element + int right = 2*i + 2; // Calculate the index of right element + + //Checking if the left is larger than current large value + if (left < n && input[left] > input[largest]) + largest = left; + + //Checking if the right is larger than current large value + if (right < n && input[right] > input[largest]) + largest = right; + + if (largest != i) + { + swap(input[i], input[largest]); + + heapify(input, n, largest); + } +} + +void heapSort(int input[], int n) +{ // Function to perform heap sort. + + // Building the heap, effectively rearranging the array. + for (int i = n / 2 - 1; i >= 0; i--) + heapify(input, n, i); + + for (int i=n-1; i>=0; i--) + { + swap(input[0], input[i]); + heapify(input, i, 0); + } +} + +int main() +{ + int n; + cout<<"Enter the number of elements"<>n; + int array[n]; + cout<<"Enter the elements of array"<>array[i]; + } + heapSort(array, n); + cout << "Sorted array is \n"; + for(int i=0; i Date: Sun, 30 Sep 2018 22:06:04 +0200 Subject: [PATCH 3/3] # This is a combination of 2 commits. # This is the 1st commit message: bubble_sort: Add C++ Implementation Add a simple C++ implementation of bubble sort Closes https://github.com/NITSkmOS/Algorithms/issues/20 # This is the commit message #2: heap_sort.cpp Adds Heap Sort Algorithm This adds Heap Sort Algorithm which sorts the elements in ascending order. Closes https://github.com/NITSkmOS/Algorithms/issues/22 --- README.md | 5 +-- bubble_sort/C++/bubble_sort.cpp | 36 ++++++++++++++++++++ heap_sort/C++/heap_sort.cpp | 60 +++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 bubble_sort/C++/bubble_sort.cpp create mode 100644 heap_sort/C++/heap_sort.cpp diff --git a/README.md b/README.md index 37f0089b..f8e96586 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,9 @@ This repository contains examples of various algorithms written on different pro | [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort) | [:octocat:](insertion_sort/C) | | | [:octocat:](insertion_sort/Python) | | [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) | | | | [:octocat:](counting_sort/Python) | | [Radix Sort](https://en.wikipedia.org/wiki/Radix_sort) | | | | [:octocat:](radix_sort/Python) | -| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | | | | [:octocat:](binary_search/Python) | - +| [Binary Search](https://en.wikipedia.org/wiki/Binary_search_algorithm) | | | | [:octocat:](binary_search/Python) | +| [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort) | | [:octocat:](bubble_sort/C++) | | | +| [Heap Sort](https://en.wikipedia.org/wiki/Heapsort) | | [:octocat:](heap_sort/C++) | | | ## Implemented Data Structures diff --git a/bubble_sort/C++/bubble_sort.cpp b/bubble_sort/C++/bubble_sort.cpp new file mode 100644 index 00000000..8150b8aa --- /dev/null +++ b/bubble_sort/C++/bubble_sort.cpp @@ -0,0 +1,36 @@ +#include +#include + +using namespace std; + +template +void bubble_sort(vector& arr) +{ + bool swapped = true; + while(swapped) + { + swapped = false; + for(size_t i = 1; i < arr.size(); i++) + { + if(arr[i-1] > arr[i]) + { + swap(arr[i-1], arr[i]); + swapped = true; + } + } + } +} + +int main() +{ + vector arr = {3,1,2,5,4,6}; + bubble_sort(arr); + + for(auto e : arr) + { + cout << e << " "; + } + cout << endl; + + return 0; +} diff --git a/heap_sort/C++/heap_sort.cpp b/heap_sort/C++/heap_sort.cpp new file mode 100644 index 00000000..4862b94f --- /dev/null +++ b/heap_sort/C++/heap_sort.cpp @@ -0,0 +1,60 @@ +#include + +using namespace std; + +void heapify(int input[], int n, int i) +{ + + int largest = i; // Assign the largest as root + int left = 2*i + 1; // Calculate the index of left element + int right = 2*i + 2; // Calculate the index of right element + + //Checking if the left is larger than current large value + if (left < n && input[left] > input[largest]) + largest = left; + + //Checking if the right is larger than current large value + if (right < n && input[right] > input[largest]) + largest = right; + + if (largest != i) + { + swap(input[i], input[largest]); + + heapify(input, n, largest); + } +} + +void heapSort(int input[], int n) +{ // Function to perform heap sort. + + // Building the heap, effectively rearranging the array. + for (int i = n / 2 - 1; i >= 0; i--) + heapify(input, n, i); + + for (int i=n-1; i>=0; i--) + { + swap(input[0], input[i]); + heapify(input, i, 0); + } +} + +int main() +{ + int n; + cout<<"Enter the number of elements"<>n; + int array[n]; + cout<<"Enter the elements of array"<>array[i]; + } + heapSort(array, n); + cout << "Sorted array is \n"; + for(int i=0; i