Skip to content
Merged
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
48 changes: 31 additions & 17 deletions src/data_structures/binary_heap.d
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import std.stdio;
import std.algorithm : swap;

/**
* Implementation of a Min Heap (Minimum Priority Queue) data structure.
* A Min Heap is a complete binary tree where the value of each node is
* less than or equal to the values of its children.
* **Implementation of a Min Heap (Minimum Priority Queue) data structure.**
* **A Min Heap is a complete binary tree where the value of each node is**
* **less than or equal to the values of its children.**
*/
class MinHeap {
private int[] harr; // Array to store heap elements
Expand All @@ -14,7 +14,8 @@ class MinHeap {
/**
* Constructs a new MinHeap with the specified capacity.
*
* @param cap the maximum number of elements the heap can hold
* Params:
* cap = the maximum number of elements the heap can hold
*/
this(int cap) {
heap_size = 0;
Expand All @@ -25,7 +26,8 @@ class MinHeap {
/**
* Recursively heapifies a subtree rooted at index i to maintain the min-heap property.
*
* @param i the index of the root node of the subtree to heapify
* Params:
* i = the index of the root node of the subtree to heapify
*/
void minHeapify(int i) {
int l = left(i); // Index of left child
Expand All @@ -48,32 +50,39 @@ class MinHeap {
/**
* Returns the index of the parent node for a given index.
*
* @param i the index of the child node
* @return the index of the parent node
* Params:
* i = the index of the child node
* Returns:
* the index of the parent node
*/
int parent(int i) { return (i - 1) / 2; }

/**
* Returns the index of the left child for a given index.
*
* @param i the index of the parent node
* @return the index of the left child node
* Params :
* i = the index of the parent node
* Returns:
* the index of the left child node
*/
int left(int i) { return (2 * i + 1); }

/**
* Returns the index of the right child for a given index.
*
* @param i the index of the parent node
* @return the index of the right child node
* Params:
* i = the index of the parent node
* Returns:
* the index of the right child node
*/
int right(int i) { return (2 * i + 2); }

/**
* Removes and returns the minimum element (root) from the heap.
* Maintains the heap property after removal.
*
* @return the minimum element in the heap, or int.max if heap is empty
* Returns:
* the minimum element in the heap, or int.max if heap is empty
*/
int extractMin() {
if (heap_size <= 0)
Expand All @@ -95,8 +104,10 @@ class MinHeap {
* Decreases the value of a key at the specified index to a new value.
* Adjusts the heap to maintain the min-heap property.
*
* @param i the index of the key to decrease
* @param new_val the new value for the key (must be smaller than current value)
* Params:
* i = the index of the key to decrease
* Params:
* new_val = the new value for the key (must be smaller than current value)
*/
void decreaseKey(int i, int new_val) {
harr[i] = new_val;
Expand All @@ -110,14 +121,16 @@ class MinHeap {
/**
* Returns the minimum element from the heap without removing it.
*
* @return the minimum element in the heap (root element)
* Returns:
* the minimum element in the heap (root element)
*/
int getMin() { return harr[0]; }

/**
* Deletes a key at the specified index from the heap.
*
* @param i the index of the key to delete
* Params:
* i = the index of the key to delete
*/
void deleteKey(int i) {
decreaseKey(i, int.min); // Decrease key to minimum value
Expand All @@ -128,7 +141,8 @@ class MinHeap {
* Inserts a new key into the heap.
* Maintains the heap property after insertion.
*
* @param k the key value to insert
* Params:
* k = the key value to insert
*/
void insertKey(int k) {
if (heap_size == capacity) {
Expand Down
19 changes: 10 additions & 9 deletions src/dynamic_programming/cut_rod.d
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import std;

/**
* Calculates the maximum profit that can be obtained by cutting a rod
* of length n and selling the pieces based on the given price array.
* ## Calculates the maximum profit that can be obtained by cutting a rod, of length n and selling the pieces based on the given price array.
*
* Params:
* price = an array of integers representing the prices for each
* length of the rod (1-indexed, where price[i-1] is the
* price for a rod of length i).
* n = the total length of the rod.
*
* @param price an array of integers representing the prices for each
* length of the rod (1-indexed, where price[i-1] is the
* price for a rod of length i).
* @param n the total length of the rod.
* @return the maximum profit that can be obtained by cutting the rod.
* Returns:
* the maximum profit that can be obtained by cutting the rod.
*/
int maxProfitByCuttingRod(int[] price, uint64_t n) {
int maxProfitByCuttingRod(int[] price, size_t n) {
// Array to store maximum profits for each length of the rod
int[] profit = new int[n + 1];

Expand Down
37 changes: 22 additions & 15 deletions src/math/math_parser.d
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import std;

/**
* The Parser class is responsible for parsing and evaluating mathematical expressions.
* ## The Parser class is responsible for parsing and evaluating mathematical expressions.
*/
class Parser {
private string input; // The input string containing the expression to parse.
private int pos; // The current position in the input string.

/**
* Constructs a Parser with the given input string.
* **Constructs a Parser with the given input string.**
*
* @param input The string representation of the mathematical expression.
* Params:
* input The string representation of the mathematical expression.
*/
this(string input) {
this.input = input;
this.pos = 0; // Initialize position to the start of the input.
}

/**
* Peeks at the next character in the input without consuming it.
* **Peeks at the next character in the input without consuming it.**
*
* @return The next character or '0' if the end of the input is reached.
* Returns:
* The next character or '0' if the end of the input is reached.
*/
private char peek() {
if (pos < input.length) {
Expand All @@ -30,9 +32,10 @@ class Parser {
}

/**
* Consumes and returns the next character in the input.
* **Consumes and returns the next character in the input.**
*
* @return The next character or '0' if the end of the input is reached.
* Returns :
* The next character or '0' if the end of the input is reached.
*/
private char next() {
if (pos < input.length) {
Expand All @@ -52,9 +55,10 @@ class Parser {
}

/**
* Parses an expression, which may consist of terms combined by '+' or '-'.
* **Parses an expression, which may consist of terms combined by '+' or '-'.**
*
* @return The result of evaluating the expression as a double.
* Returns:
* The result of evaluating the expression as a double.
*/
double parseExpr() {
double result = parseTerm(); // Start with the first term.
Expand All @@ -77,9 +81,10 @@ class Parser {
}

/**
* Parses a term, which may consist of factors combined by '*' or '/'.
* **Parses a term, which may consist of factors combined by multiplication or division.**
*
* @return The result of evaluating the term as a double.
* Returns:
* The result of evaluating the term as a double.
*/
double parseTerm() {
double result = parseFactor(); // Start with the first factor.
Expand All @@ -102,9 +107,10 @@ class Parser {
}

/**
* Parses a factor, which may be a number or a nested expression in parentheses.
* **Parses a factor, which may be a number or a nested expression in parentheses.**
*
* @return The result of evaluating the factor as a double.
* Returns:
* The result of evaluating the factor as a double.
*/
double parseFactor() {
skipWhitespace(); // Skip any whitespace before processing factor.
Expand All @@ -119,9 +125,10 @@ class Parser {
}

/**
* Parses a number from the input string.
* **Parses a number from the input `string`.**
*
* @return The parsed number as a double.
* Returns:
* The parsed number as a double.
*/
double parseNumber() {
skipWhitespace(); // Skip any whitespace before processing number.
Expand Down
11 changes: 6 additions & 5 deletions src/sort/bubble_sort.d
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import std.stdio;

/**
* Sorts an array of integers in ascending order using the Bubble Sort algorithm.
* ## Sorts an array of integers in ascending order using the Bubble Sort algorithm.
*
* This algorithm repeatedly steps through the list, compares adjacent elements,
* and swaps them if they are in the wrong order. The pass through the list is
* repeated until the list is sorted.
* This algorithm repeatedly steps through the list, compares adjacent elements,
* and swaps them if they are in the wrong order. The pass through the list is
* repeated until the list is sorted.
*
* @param arr the array of integers to be sorted
* Params:
* arr = the array of integers to be sorted
*/
void bubbleSort(int[] arr) {
// Get the length of the array
Expand Down
13 changes: 7 additions & 6 deletions src/sort/insert_sort.d
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import std.stdio;

/*
/**
* Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list.
* It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards.
* Then, you pick a card from the unsorted group and put it in the right place in the sorted group.
*
* 1) We start with second element of the array as first element in the array is assumed to be sorted.
* 2) Compare second element with the first element and check if the second element is smaller then swap them.
* 3) Move to the third element and compare it with the first two elements and put at its correct position
* 4) Repeat until the entire array is sorted.
* 1. We start with second element of the array as first element in the array is assumed to be sorted.
* 2. Compare second element with the first element and check if the second element is smaller then swap them.
* 3. Move to the third element and compare it with the first two elements and put at its correct position
* 4. Repeat until the entire array is sorted.
*
* @param arr the array of integers to be sorted
* Params :
* arr = the array of integers to be sorted
*/
void insert_sort(ulong[] arr) {
for (int i = 1; i < arr.length; i++) {
Expand Down
6 changes: 3 additions & 3 deletions src/sort/quick_sort.d
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
* Quick sort is a sorting algorithm that belongs to the "divide and conquer" algorithms.
/**
* ## Quick sort is a sorting algorithm that belongs to the "divide and conquer" algorithms.
* It works by choosing a pivot element from the array and dividing it into two subarrays, where one
* array contains elements less than the pivot, and the other contains elements greater than the pivot, then the arrays
* are sorted recursively and later combined together.
* Steps to implement the algorithm:
* ### Steps to implement the algorithm:
* 1. If the length of the array is less than two, return it since it does not need to be sorted.
* 2. Choose an element that will serve as the pivot.
* 3. Divide the array into two subarrays with elements less than the pivot and elements greater than the pivot.
Expand Down
19 changes: 10 additions & 9 deletions src/sort/selection_sort.d
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import std.stdio;

/*
/**
* Selection Sort is a comparison-based sorting algorithm.
* It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element.
* This process continues until the entire array is sorted.
*
* 1) First we find the smallest element and swap it with the first element. This way we get the smallest element at its correct position.
* 2) Then we find the smallest among remaining elements (or second smallest) and move it to its correct position by swapping.
* 3) We keep doing this until we get all elements moved to correct position.
* 1. First we find the smallest element and swap it with the first element. This way we get the smallest element at its correct position.
* 2. Then we find the smallest among remaining elements (or second smallest) and move it to its correct position by swapping.
* 3. We keep doing this until we get all elements moved to correct position.
*
* @param arr the array of integers to be sorted
* Params:
* arr = the array of integers to be sorted
*/
void selection_sort(ulong[] arr) {
// Get the length of the array
ulong n = arr.length;
size_t n = arr.length;

for (ulong i = 0; i < n-1; i++) {
for (size_t i = 0; i < n-1; i++) {
// Assume the current position holds the minimum element
ulong min_index = i;
size_t min_index = i;

// Iterate through the unsorted portion to find the actual minimum
for (ulong j = i + 1; j < n; j++) {
for (size_t j = i + 1; j < n; j++) {
// Update min_idx if a smaller element is found
if (arr[j] < arr[min_index]) min_index = j;
}
Expand Down
18 changes: 9 additions & 9 deletions src/sort/stalin_sort.d
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import std.stdio;

/*
/**
* Stalin Sort is a humorous sorting algorithm that modifies the input array in-place.
* It keeps elements that are in non-decreasing order and removes elements that break this order.
*
* 1) Start with the first element of the array as the base.
* 2) Iterate through the array, keeping only elements that are greater than or equal to the last kept element.
* 3) Shift all valid elements to the left, effectively removing invalid ones.
* 4) Resize the array to include only valid elements.
* 1. Start with the first element of the array as the base.
* 2. Iterate through the array, keeping only elements that are greater than or equal to the last kept element.
* 3. Shift all valid elements to the left, effectively removing invalid ones.
* 4. Resize the array to include only valid elements.
*
* @param arr the array of integers to be sorted
* Params:
* arr = the array of integers to be sorted
*/
void stalin_sort(ref ulong[] arr) {
if (arr.length == 0) return; // No action for an empty array

ulong index = 0; // Position to keep the current element
size_t index = 0; // Position to keep the current element

for (ulong i = 1; i < arr.length; i++) {
for (size_t i = 1; i < arr.length; i++) {
if (arr[i] >= arr[index]) {
index++;
arr[index] = arr[i]; // Keep valid element in place
Expand Down
Loading