Solutions for some common algorithm problems written in Java.

Overview

Algorithms Build Status

This repository contains my solution for common algorithms. I've created this repository to learn about algorithms and improve solutions to common computer science problems. I'll try to add more solutions if I have time. :)

Each solution has a Java program and is tested. Some problems contain multiple solutions with different implementations.

You can check the solution executing tests inside tests directory. Some problems were resolved using TDD.

Problems

Trees

Linked List

Strings

Binary Numbers and bits operators

Math Operations

Sequences

Arrays

Sorting Algorithms

Misc

Developed By

Follow me on Twitter Add me to Linkedin

License

Copyright 2014 Pedro Vicente Gómez Sánchez

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Comments
  • visibility and initialization

    visibility and initialization

    https://github.com/pedrovgs/Algorithms/blob/7729e8135393710083184ce6ab6a8a1faebd447b/src/main/java/com/github/pedrovgs/problem46/BinaryTreeSerialization.java#L54

    This variable could be defined as: private int index; and placed above the methods

    opened by lprone 4
  • Refactoring Sum of binary numbers & incorrect time complexity of square root.

    Refactoring Sum of binary numbers & incorrect time complexity of square root.

    Refactored code of two binary number summations. Less code replication.

    & Elaborated on complexity of square root. Not same as binary search, though binary search is used to find closest number.

    opened by ajinkyakolhe112 4
  • Frequency

    Frequency

    I'm a university student just doing an assignment :) This algorithm returns the count of the number of times an element is present in the collection.

    opened by honwenxuan 3
  • Package names are not descriptive:

    Package names are not descriptive: "package com.github.pedrovgs.problem65"

    I guess package names should not contain anything as meaningless as "problem65". The greatest virtue of today's world is a code reuse. I would like to import code from something with a descriptive name so as not to confuse my followers.

    Why isn't it called package com.github.pedrovgs.algorithms.IsTreeBalanced?

    opened by FractalizeR 3
  • fix quick sort for special inputs and update testcase

    fix quick sort for special inputs and update testcase

    Fixes #51

    Actually, I think there are some flaws in the test for sorting. At least you should have lots of random cases to find out the lurking issues.

    The updated quicksort pass through the following scale of tests:

        private static final int TEST_COUNT = 100_000;
        private static final int TEST_ARRAY_MIN_SIZE = 1;
        private static final int TEST_ARRAY_MAX_SIZE = 100_000;
        private static final int TEST_ARRAY_RANGE = 100_000;
    
    opened by Hearen 2
  • bug in problem 32

    bug in problem 32

    it should check the length of the w2 string, or it will throws a StringIndexOutOfBoundsException Test Case like this: String word1 = "drso"; String word2 = "Pedro";

    opened by ZephyrJung 2
  • Fixed typo and few sentences

    Fixed typo and few sentences

    Hi Pedro, Hope you are doing fine. You are a wonderful programmer. I follow you. During the follow up about your recent update on 'SplitArray', I have brushed up few sentences a little bit. Please have a look and if you feel it is worth modification, please merge it.

    It would be greatly appreciated if you allow me to work with you in your open source projects. Have a wonderful time.

    Best regards, Azizur

    opened by azizurice 2
  • I am confused about the bubble sort.

    I am confused about the bubble sort.

    I read your code and I don't think the bubble sort is like what I read from the algorithm books. I don't get it. The comment says 'compares each pair of adjacent items and swaps them if they are in the wrong order',but it is not. The code is compare the elment to the others every step(not adjacent ones). It works but it is not as the comment says.

    question 
    opened by bigscorpions 2
  • Why don't you have a main?

    Why don't you have a main?

    Disclaimer: noob

    When I type javac Trie.java I get this output

    Trie.java:5: error: package com.jwetherell.algorithms.data_structures.interfaces does not exist import com.jwetherell.algorithms.data_structures.interfaces.ITree

    question 
    opened by hsavit1 2
  • 80 quicksort is into infinite loop

    80 quicksort is into infinite loop

    Please have a try with the following test using JUnit, it will fail with timeout.

    Please hold a sec, I am starting a PR for this along with tests later (today).

    public class QuickSortGitTest {
        private static final int[] unorderedIntegerArrayThree = {12, -37, -5, 43, 62, 45, -95, -70, -55, -62, -24, -14,
                -75, 43, 9, 58, -62, -22, -55};
    
        private static void quickSort(int[] numbers, int left, int right) {
            if (left < right) {
                int pivotIndex = partition(numbers, left, right);
                quickSort(numbers, left, pivotIndex - 1);  //sort left of pivot
                quickSort(numbers, pivotIndex, right);  //sort right of pivot
            }
        }
    
    //    private static int partition(int[] array, int low, int high) {
    //        int pivot = array[high];
    //        int i = low - 1;
    //        for (int j = low; j <= high - 1; j++)
    //            if (array[j] <= pivot)
    //                swap(array, ++i, j);
    //
    //        swap(array, i + 1, high);
    //        return i + 1;
    //    }
    //
    //    private static void swap(int[] array, int i, int j) {
    //        int temp;
    //        temp = array[i];
    //        array[i] = array[j];
    //        array[j] = temp;
    //    }
    
        private static int partition(int[] numbers, int left, int right) {
            int pivot = numbers[right];
            while (left < right) {
                while (numbers[left] < pivot) {
                    left++;
                }
                while (numbers[right] > pivot) {
                    right--;
                }
                if (left <= right) {
                    int temp = numbers[left];
                    numbers[left] = numbers[right];
                    numbers[right] = temp;
                }
            }
            return left; //pivot index
        }
    
        @Test(timeout = 5 * 1000)
        public void testRecursionZero() {
            quickSort(unorderedIntegerArrayThree, 0, unorderedIntegerArrayThree.length - 1);
        }
    }
    
    opened by Hearen 1
  • visibility and initialization

    visibility and initialization

    fixing issue https://github.com/pedrovgs/Algorithms/issues/49 for definition and initialization of variables on : BinaryTreeSerialization PathCalculator

    opened by lprone 1
  • Bump junit from 4.11 to 4.13.1

    Bump junit from 4.11 to 4.13.1

    Bumps junit from 4.11 to 4.13.1.

    Release notes

    Sourced from junit's releases.

    JUnit 4.13.1

    Please refer to the release notes for details.

    JUnit 4.13

    Please refer to the release notes for details.

    JUnit 4.13 RC 2

    Please refer to the release notes for details.

    JUnit 4.13 RC 1

    Please refer to the release notes for details.

    JUnit 4.13 Beta 3

    Please refer to the release notes for details.

    JUnit 4.13 Beta 2

    Please refer to the release notes for details.

    JUnit 4.13 Beta 1

    Please refer to the release notes for details.

    JUnit 4.12

    Please refer to the release notes for details.

    JUnit 4.12 Beta 3

    Please refer to the release notes for details.

    JUnit 4.12 Beta 2

    No release notes provided.

    JUnit 4.12 Beta 1

    No release notes provided.

    Commits
    • 1b683f4 [maven-release-plugin] prepare release r4.13.1
    • ce6ce3a Draft 4.13.1 release notes
    • c29dd82 Change version to 4.13.1-SNAPSHOT
    • 1d17486 Add a link to assertThrows in exception testing
    • 543905d Use separate line for annotation in Javadoc
    • 510e906 Add sub headlines to class Javadoc
    • 610155b Merge pull request from GHSA-269g-pwp5-87pp
    • b6cfd1e Explicitly wrap float parameter for consistency (#1671)
    • a5d205c Fix GitHub link in FAQ (#1672)
    • 3a5c6b4 Deprecated since jdk9 replacing constructor instance of Double and Float (#1660)
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 0
Owner
Pedro Vicente Gómez Sánchez
"Sometimes it is the people no one imagines anything of who do the things no one can imagine." - Alan Turing
Pedro Vicente Gómez Sánchez
MCQs and coding questions solutions of Object-Oriented Programming java of coding ninjas

cn-java-sols (⌐■_■) Link to This repository Other similar repository of my friend Link ?? enjoy having full marks ?? ?? now answers avaible up to Stri

Sanyam Mahajan 11 Dec 27, 2022
Fast and stable sort algorithm that uses O(1) memory. Public domain.

WikiSort WikiSort is an implementation of "block merge sort", which is a stable merge sort based on the work described in "Ratio based stable in-place

Mike McFadden 1.2k Jan 1, 2023
Modern Java - A Guide to Java 8

Modern Java - A Guide to Java 8 This article was originally posted on my blog. You should also read my Java 11 Tutorial (including new language and AP

Benjamin Winterberg 16.1k Dec 29, 2022
Design patterns implemented in Java

Design patterns implemented in Java Read in different language : CN, KR, FR, TR, AR Introduction Design patterns are the best formalized practices a p

Ilkka Seppälä 79k Jan 2, 2023
Java EE 7 Samples

Java EE 7 Samples This workspace consists of Java EE 7 Samples and unit tests. They are categorized in different directories, one for each Technology/

JavaEE Samples 2.5k Dec 20, 2022
Algorithms and Data Structures implemented in Java

Java : Algorithms and Data Structure The algorithms and data structures are implemented in Java. This is a collection of algorithms and data structure

Justin Wetherell 4.2k Jan 5, 2023
Rework of html-java-dsl to work with newer Javas

java-html-dsl Example DSL for writing html in Java. Rework of benjiman/java-html-dsl to work with newer versions of Java This String doc = html(

Benji Weber 19 Jan 25, 2022
A toolchain for Minecraft: Java Edition that builds a workspace to interact with the game using the official mappings provided to the public by Mojang Studios.

VanillaGradle is a toolchain for Minecraft: Java Edition that provides a workspace to interact with the game using official mappings provided by Mojan

SpongePowered 75 Nov 22, 2022
Amazing Ruby's "Enumerable" ported to Java

Overview How to use? .all .any .none .select .map .count .reject .find How to contribute? Contributors Overview enumerable4j is a Ruby's well known En

Yurii Dubinka 30 Oct 28, 2022
Java 16 Features

Java 16 Features. Features are separated by package name.

Rahman Usta 9 Jan 7, 2022
Java Kampında derslerde yazılan projeler

nLayeredDemo JavaCampDay5Lesson https://www.youtube.com/watch?v=yaBPeS65vwM&ab_channel=EnginDemiro%C4%9F linkteki derste yapılan proje. Nortwind JavaC

Zeyneb Eda YILMAZ 10 Dec 14, 2021
Engin DEMIROG yotube java kamp HW

JavaBootCamp https://www.kodlama.io/courses/enrolled/1332369 Engin DEMIROG yotube javaBootCamp HW ve projeler Java & React Bootcamp (https://kodlama.i

Melik KARACA 8 Nov 13, 2022
Software Developer Training Camp (JAVA + REACT) works under the guidance of Engin Demiroğ

https://kodlama.io/p/yazilim-gelistirici-yetistirme-kampi2 [EN] Java_React-BootCamp Software Developer Training Camp (JAVA + REACT) works under the gu

Saba ÜRGÜP 18 Dec 24, 2022
A Java game Solitaire, made with JavaFX

Java Solitaire A game made with JavaFX Installation requirements: At least Java 11 installed. setup: 2.1. Intellij -> Open the file in the IDE and exe

Walter Alleyz 15 May 6, 2021
Bitcoin SV Library for Java

Introduction Overview Bitcoin4J is a Bitcoin library for the Java Language licensed under the Apache License 2.0. This library has been built in line

null 17 May 25, 2022
Repository for Bryn and Ethan's Java with MicroServices Batch

210607-FeederProgram This repository houses examples and environment setup for the Revature feeder program beginning on 6/7/2021 Environment Setup Gui

Bryn Portella 17 May 22, 2022
http://kodlama.io "Java & React Bootcamp" up to date Lectures and Homeworks.

Java & React Bootcamp (https://kodlama.io/) Lectures Lecture 1 intro Lecture 2 oopIntro homework Lecture 3 oopIntro2 inheritance inheritance2 homework

Karcan Ozbal 237 Dec 29, 2022
Códigos do Bootcamp Java Básico DIO

Curso Java Básico Digital Innovation One https://digitalinnovation.one Tive a honra de fazer parceria com o pessoal da Digital Innovation One, com doi

Nicole Bidigaray 3 Jul 28, 2021
Slicer4J is an accurate, low-overhead dynamic slicer for Java programs.

Slicer4J This repository hosts Slicer4J, an accurate, low-overhead dynamic slicer for Java programs. Slicer4J automatically generates a backward dynam

The Reliable, Secure, and Sustainable Software Lab 25 Dec 19, 2022