diff --git a/C++/bucket_sort b/C++/bucket_sort new file mode 100644 index 0000000..c486bef --- /dev/null +++ b/C++/bucket_sort @@ -0,0 +1,46 @@ +// C++ program to sort an +// array using bucket sort +#include +#include +#include +using namespace std; + +// Function to sort arr[] of +// size n using bucket sort +void bucketSort(float arr[], int n) +{ + + // 1) Create n empty buckets + vector b[n]; + + // 2) Put array elements + // in different buckets + for (int i = 0; i < n; i++) { + int bi = n * arr[i]; // Index in bucket + b[bi].push_back(arr[i]); + } + + // 3) Sort individual buckets + for (int i = 0; i < n; i++) + sort(b[i].begin(), b[i].end()); + + // 4) Concatenate all buckets into arr[] + int index = 0; + for (int i = 0; i < n; i++) + for (int j = 0; j < b[i].size(); j++) + arr[index++] = b[i][j]; +} + +/* Driver program to test above function */ +int main() +{ + float arr[] + = { 0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434 }; + int n = sizeof(arr) / sizeof(arr[0]); + bucketSort(arr, n); + + cout << "Sorted array is \n"; + for (int i = 0; i < n; i++) + cout << arr[i] << " "; + return 0; +} diff --git a/C++/stable_sort b/C++/stable_sort new file mode 100644 index 0000000..7cce17f --- /dev/null +++ b/C++/stable_sort @@ -0,0 +1,18 @@ +// C++ program to demonstrate default behaviour of stable sort() +#include +using namespace std; + +int main() +{ + int arr[] = { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 }; + int n = sizeof(arr) / sizeof(arr[0]); + + stable_sort(arr, arr + n); + + cout << "\nArray after sorting using " + "default sort is : \n"; + for (int i = 0; i < n; ++i) + cout << arr[i] << " "; + + return 0; +} diff --git a/Golang/heapSort.go b/Golang/heapSort.go new file mode 100644 index 0000000..abe8afc --- /dev/null +++ b/Golang/heapSort.go @@ -0,0 +1,84 @@ +package main + +import "fmt" + +type minheap struct { + arr []int +} + +func newMinHeap(arr []int) *minheap { + minheap := &minheap{ + arr: arr, + } + return minheap +} + +func (m *minheap) leftchildIndex(index int) int { + return 2*index + 1 +} + +func (m *minheap) rightchildIndex(index int) int { + return 2*index + 2 +} + +func (m *minheap) swap(first, second int) { + temp := m.arr[first] + m.arr[first] = m.arr[second] + m.arr[second] = temp +} + +func (m *minheap) leaf(index int, size int) bool { + if index >= (size/2) && index <= size { + return true + } + return false +} + +func (m *minheap) downHeapify(current int, size int) { + if m.leaf(current, size) { + return + } + smallest := current + leftChildIndex := m.leftchildIndex(current) + rightRightIndex := m.rightchildIndex(current) + if leftChildIndex < size && m.arr[leftChildIndex] < m.arr[smallest] { + smallest = leftChildIndex + } + if rightRightIndex < size && m.arr[rightRightIndex] < m.arr[smallest] { + smallest = rightRightIndex + } + if smallest != current { + m.swap(current, smallest) + m.downHeapify(smallest, size) + } + return +} + +func (m *minheap) buildMinHeap(size int) { + for index := ((size / 2) - 1); index >= 0; index-- { + m.downHeapify(index, size) + } +} + +func (m *minheap) sort(size int) { + m.buildMinHeap(size) + for i := size - 1; i > 0; i-- { + // Move current root to end + m.swap(0, i) + m.downHeapify(0, i) + } +} + +func (m *minheap) print() { + for _, val := range m.arr { + fmt.Println(val) + } +} + +func main() { + inputArray := []int{6, 5, 3, 7, 2, 8, -1} + minHeap := newMinHeap(inputArray) + minHeap.sort(len(inputArray)) + minHeap.print() + fmt.Scanln() +} diff --git a/README.md b/README.md index 9ee6432..4ac02a7 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,3 @@ looking up to contributions from various users to improve ## want to contribute look up our [contribution guidelines](https://github.com/tornado-12/learn-to-code/blob/master/CONTRIBUTING.md) -