Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions CPP/bucketsort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <iostream>

using namespace std;

template <class T>
void Print(T& vec, int n, string s){
cout << s << ": [" << flush;
for (int i=0; i<n; i++){
cout << vec[i] << flush;
if (i < n-1){
cout << ", " << flush;
}
}
cout << "]" << endl;
}

int Max(int A[], int n){
int max = -32768;
for (int i=0; i<n; i++){
if (A[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<max+1; i++){
bins[i] = nullptr;
}

// Update count array values based on A values
for (int i=0; i<n; i++){
Insert(bins, A[i]);
}

// Update A with sorted elements
int i = 0;
int j = 0;
while (i < max+1){
while (bins[i] != nullptr){
A[j++] = Delete(bins, i);
}
i++;
}

// Delete heap memory
delete [] bins;
}

int main() {

int A[] = {2, 5, 8, 12, 3, 6, 7, 10};
int n = sizeof(A)/sizeof(A[0]);

Print(A, n, "\t\tA");
BinSort(A, n);
Print(A, n, " Sorted A");
cout << endl;
return 0;
}
94 changes: 94 additions & 0 deletions CPP/primspanningtree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <iostream>
#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<V-2; i++){
int c = G[T[0][i]][T[1][i]];
cout << "[" << T[0][i] << "]---[" << T[1][i] << "] cost: " << c << endl;
sum += c;
}
cout << endl;
cout << "Total cost of MST: " << sum << endl;
}

void PrimsMST(int G[V][V], int n){
int u;
int v;
int min {I};
int track [V];
int T[2][V-2] {0};

// Initial: Find min cost edge
for (int i {1}; i<V; i++){
track[i] = I; // Initialize track array with INFINITY
for (int j {i}; j<V; j++){
if (G[i][j] < min){
min = G[i][j];
u = i;
v = j;
}
}
}
T[0][0] = u;
T[1][0] = v;
track[u] = track[v] = 0;

// Initialize track array to track min cost edges
for (int i {1}; i<V; i++){
if (track[i] != 0){
if (G[i][u] < G[i][v]){
track[i] = u;
} else {
track[i] = v;
}
}
}

// Repeat
for (int i {1}; i<n-1; i++){
int k;
min = I;
for (int j {1}; j<V; j++){
if (track[j] != 0 && G[j][track[j]] < min){
k = j;
min = G[j][track[j]];
}
}
T[0][i] = k;
T[1][i] = track[k];
track[k] = 0;

// Update track array to track min cost edges
for (int j {1}; j<V; j++){
if (track[j] != 0 && G[j][k] < G[j][track[j]]){
track[j] = k;
}
}
}
PrintMST(T, G);
}

int main() {

int cost [V][V] {
{I, I, I, I, I, I, I, I},
{I, I, 25, I, I, I, 5, I},
{I, 25, I, 12, I, I, I, 10},
{I, I, 12, I, 8, I, I, I},
{I, I, I, 8, I, 16, I, 14},
{I, I, I, I, 16, I, 20, 18},
{I, 5, I, I, I, 20, I, I},
{I, I, 10, I, 14, 18, I, I},
};

int n = sizeof(cost[0])/sizeof(cost[0][0]) - 1;

PrimsMST(cost, n);

return 0;
}