diff --git a/CPP/bucketsort.cpp b/CPP/bucketsort.cpp new file mode 100644 index 00000000..11b500b5 --- /dev/null +++ b/CPP/bucketsort.cpp @@ -0,0 +1,98 @@ +#include + +using namespace std; + +template +void Print(T& vec, int n, string s){ + cout << s << ": [" << flush; + for (int i=0; i max){ + max = A[i]; + } + } + return max; +} + +// Linked List node +class Node{ +public: + int value; + Node* next; +}; + +void Insert(Node** ptrBins, int idx){ + Node* temp = new Node; + temp->value = idx; + temp->next = nullptr; + + if (ptrBins[idx] == nullptr){ // ptrBins[idx] is head ptr + ptrBins[idx] = temp; + } else { + Node* p = ptrBins[idx]; + while (p->next != nullptr){ + p = p->next; + } + p->next = temp; + } +} + +int Delete(Node** ptrBins, int idx){ + Node* p = ptrBins[idx]; // ptrBins[idx] is head ptr + ptrBins[idx] = ptrBins[idx]->next; + int x = p->value; + delete p; + return x; +} + +void BinSort(int A[], int n){ + int max = Max(A, n); + + // Create bins array + Node** bins = new Node* [max + 1]; + + // Initialize bins array with nullptr + for (int i=0; i +#define V 8 +#define I 32767 + +using namespace std; + +void PrintMST(int T[][V-2], int G[V][V]){ + cout << "\nMinimum Spanning Tree Edges (w/ cost)\n" << endl; + int sum {0}; + for (int i {0}; i