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
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
# Data Structures & Algorithms
This repository contains implementation of some fundamental data structures and algorithms in Computer Science. It is primarily used as a teaching resource and is currently being developed by ex-2040s students.

The project uses Gradle and the structure is optimised for IntelliJ IDEA since implementation are mostly written in Java.
This repository contains implementation of some fundamental data structures and algorithms in Computer Science. It is
primarily used as a teaching resource and is currently being developed by ex-2040s students.

**Note**: This is still being developed. Those below with links mean that they are complete (alongside testing). We project to complete CS2040s course content by November and along the way, add interesting algorithms/problems. We are hopeful that the subsequent batches of students (from AY23/24 S2) will benefit greatly from this.
The project uses Gradle and the structure is optimised for IntelliJ IDEA since implementation are mostly written in
Java.

**Note**: This is still being developed. Those below with links mean that they are complete (alongside testing). We
project to complete CS2040s course content by November and along the way, add interesting algorithms/problems. We are
hopeful that the subsequent batches of students (from AY23/24 S2) will benefit greatly from this.

If you wish to contribute, do drop an email at andre_lin@u.nus.edu.

## Full List of Implementation (in alphabetical order):

## Structures

- Adelson-Velskii and Landis (AVL) Binary Search Tree
- Disjoint Set / Union Find
* Quick Find
* Weighted Union
* Weighted Union
* Path compression
- [Hashing](src/main/java/dataStructures/hashSet)
* [Chaining](src/main/java/dataStructures/hashSet/chaining)
Expand All @@ -25,12 +32,12 @@ If you wish to contribute, do drop an email at andre_lin@u.nus.edu.
- [Queue](src/main/java/dataStructures/queue)
- Segment Tree
* Array implementation
* TreeNode implementation
* TreeNode implementation
- [Stack](src/main/java/dataStructures/stack)
- Trie


## Algorithms

- [Counting Sort](src/main/java/algorithms/sorting/countingSort)
- [Cyclic Sort](src/main/java/algorithms/sorting/cyclicSort)
* [Special case](src/main/java/algorithms/sorting/cyclicSort/simple) of O(n) time complexity
Expand All @@ -49,8 +56,8 @@ If you wish to contribute, do drop an email at andre_lin@u.nus.edu.
* [3-way Partitioning](src/main/java/algorithms/sorting/quickSort/threeWayPartitioning)
- Radix Sort


## Short-cut to CS2040S Material

1. Basic structures
* [Linked List](src/main/java/dataStructures/linkedList)
* [Stack](src/main/java/dataStructures/stack)
Expand Down Expand Up @@ -91,12 +98,13 @@ If you wish to contribute, do drop an email at andre_lin@u.nus.edu.
* Dijkstra
* Directed acyclic graphs
10. Minimum spanning tree
* Prim's
* Prim's
* Kruskal's


## Running Custom Inputs

See [here](scripts/README.md).

## Contributors

See the [team](docs/team/profiles.md)!
783 changes: 422 additions & 361 deletions config/checkstyle/checkstyle.xml

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions config/checkstyle/suppressions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>

<!DOCTYPE suppressions PUBLIC
"-//Checkstyle//DTD SuppressionFilter Configuration 1.2//EN"
"https://checkstyle.org/dtds/suppressions_1_2.dtd">

<suppressions>
<suppress checks="JavadocType" files=".*Test\.java"/>
<suppress checks="MissingJavadocMethodCheck" files=".*Test\.java"/>
</suppressions>
25 changes: 19 additions & 6 deletions scripts/algorithms/patternFinding/RunKMP.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,42 @@

import java.util.List;

/**
* Runs the KMP algorithm.
*/
public class RunKMP {
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
////////////////////////////////////////// This section is for user input \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
static String seq = "abclaabcabcabc";
static String pattern = "abc";
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\
////////////////////////////////////////// This section is for user input \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
private static String seq = "abclaabcabcabc";
private static String pattern = "abc";

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\

public static void main(String[] args) {
List<Integer> indices = KMP.findOccurrences(seq, pattern);
display(indices);
prettyPrint(seq, pattern, indices);
}

/**
* Prints the string representation of the List.
* @param arr the given List.
*/
public static void display(List<Integer> arr) {
StringBuilder toDisplay = new StringBuilder("[");
for (int num : arr) {
toDisplay.append(String.format("%d ", num));
}
toDisplay = toDisplay.replace(toDisplay.length()-1, toDisplay.length(), "]");
toDisplay = toDisplay.replace(toDisplay.length() - 1, toDisplay.length(), "]");
System.out.println(toDisplay);
}

/**
* Pretty prints the sequence and pattern.
* @param seq the given string sequence.
* @param pattern the given string pattern.
* @param indices TODO.
*/
public static void prettyPrint(String seq, String pattern, List<Integer> indices) {
StringBuilder prettySeq = new StringBuilder(" Sequence: ");
StringBuilder prettyPtr = new StringBuilder("Start Pts: ");
Expand Down
22 changes: 15 additions & 7 deletions scripts/algorithms/sorting/RunCountingSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,34 @@

import algorithms.sorting.countingSort.CountingSort;

/**
* Script to run Counting Sort.
*/
public class RunCountingSort {

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
////////////////////////////////////////// This section is for user input \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
static int[] toSort =
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
////////////////////////////////////////// This section is for user input \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
private static int[] toSort =
new int[] {3, 4, 2, 65, 76, 93, 22, 1, 5, 7, 88, 54, 44, 7, 5, 6, 2, 64, 43, 22, 27, 33, 59, 64, 76, 99, 37, 7};

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

public static void main(String[] args) {
toSort = CountingSort.sort(toSort);
display(toSort);
toSort = CountingSort.sort(toSort);
display(toSort);
}

/**
* Prints the string representation of the array.
*
* @param arr the given array.
*/
public static void display(int[] arr) {
StringBuilder toDisplay = new StringBuilder("[");
for (int num : arr) {
toDisplay.append(String.format("%d ", num));
}
toDisplay = toDisplay.replace(toDisplay.length()-1, toDisplay.length(), "]");
toDisplay = toDisplay.replace(toDisplay.length() - 1, toDisplay.length(), "]");
System.out.println(toDisplay);
}
}
4 changes: 2 additions & 2 deletions src/main/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Implementation
# Implementation

This folder contains the source code for all the algorithms and data structures listed on the home page.
This folder contains the source code for all the algorithms and data structures listed on the home page.
18 changes: 10 additions & 8 deletions src/main/java/algorithms/binarySearch/BinarySearch.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
package algorithms.binarySearch;

/** Here, we are implementing BinarySearch where we search an array for a target value at O(log n) time complexity.
*
/**
* Here, we are implementing BinarySearch where we search an array for a target value at O(log n) time complexity.
* <p>
* Assumptions:
* The array is sorted in ascending order.
* All elements in the array are unique. (to allow for easy testing)
*
* <p>
* Brief Description and Implementation Invariant:
* With the assumption that the array is sorted in ascending order, BinarySearch reduces the search range by half or
* half + 1 (due to floor division) after every loop. This is done by reassigning the max (high) or min (low) of the
* search range to the middle of the search range when the target value is smaller than or larger than the current
* middle value respectively, and continuing the search thereafter.
*
* <p>
* In both scenarios where the target is not equal to arr[mid], the high and low pointers are reassigned mid decremented
* /incremented by 1. This ensures that there will never be an infinite loop as the search range will no longer include
* the mid-value, causing the search range to constantly "shrink". We know we can decrement/increment mid by 1 during
* the reassignment as the mid-value is definitely not the target and should no longer be in the search range.
*
* <p>
* At the end of every loop, we know that the target value is either within the search range or does not exist in the
* array, thereby ensuring the correctness of the algorithm.
*
* <p>
* Since after each iteration, the search range is halved, it will only take a maximum of O(log n) times before the
* target is either found or determined to not exist in the array.
*/
public class BinarySearch {
/**
* Searches for a target value within a sorted array using the binary search algorithm.
* @param arr the sorted array in which the search is to be performed.
*
* @param arr the sorted array in which the search is to be performed.
* @param target the value to be searched for.
* @return the index of the target if found, otherwise -1.
*/
Expand All @@ -46,4 +48,4 @@ public static int search(int[] arr, int target) {

return -1;
}
}
}
20 changes: 11 additions & 9 deletions src/main/java/algorithms/binarySearch/BinarySearchTemplated.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package algorithms.binarySearch;

/** Here, we are implementing BinarySearchTemplated where we search an array for a target value at O(log n) time
/**
* Here, we are implementing BinarySearchTemplated where we search an array for a target value at O(log n) time
* complexity.
*
* <p>
* Assumptions:
* The array is sorted in ascending order.
* All elements in the array are unique. (to allow for easy testing)
*
* <p>
* Brief Description and Implementation Invariant:
* With the assumption that the array is sorted in ascending order, BinarySearchTemplated reduces the search range by
* half or half + 1 (due to floor division) after every loop. This is done by reassigning the max (high) or min (low) of
* the search range to the middle of the search range when the target value is smaller than or larger than the current
* middle value respectively, and continuing the search thereafter.
*
* <p>
* In this version, there is no condition to check if the current mid is equal to the target to prematurely end the
* search. Hence, the only way for the loop to complete is when low exceeds high.
*
* <p>
* At the end of every loop, we know that the target value is either within the search range or does not exist in the
* array, thereby ensuring the correctness of the algorithm.
*
* <p>
* Since after each iteration, the search range is halved, it will only take a maximum of O(log n) times before the
* target is either found or determined to not exist in the array.
*/
public class BinarySearchTemplated {
/**
* A utility method to compare two integers.
* @param value The current value from the array.
*
* @param value The current value from the array.
* @param target The value being searched for.
* @return true if the current value is less than the target, otherwise false.
*/
Expand All @@ -37,7 +39,7 @@ public static boolean condition(int value, int target) {
/**
* Conducts a binary search to find the target within the sorted array.
*
* @param arr The sorted input array.
* @param arr The sorted input array.
* @param target The value to be searched within the array.
* @return The index of the target value if found, otherwise -1.
*/
Expand All @@ -63,4 +65,4 @@ public static int search(int[] arr, int target) {
// Returns -1 if loop was exited without finding the target
return -1;
}
}
}
Loading