Giter VIP home page Giter VIP logo

mergingtwosortedarrays's Introduction

MergingTwoSortedArrays

Description

In this project I am creating and testing a method accepting 2 sorted integer arrays and returning a merged array.

Definition of an Array

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

(image

Getting Started

In the project I have tested created method using Junit unit-testing framework. In order to implement it first I added junit-jupiter dependency to my pom.xml file.

Dependencies

<dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.0</version>
        </dependency>

Solution of the problem

In my solution I used a while loop

Java while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop is considered as a repeating if statement. If the number of iterations is not fixed, it is recommended to use the while loop.

image

In my method I used a while loop that compares elements from the first array to elements of the second array and adds the accordingly to the merged array of the combined length of the first and second array

   int[] mergedArray = new int[arrayA.length + arrayB.length];

        int i = 0, j = 0, k = 0;

        while (i < arrayA.length && j < arrayB.length) {
            if (arrayA[i] < arrayB[j]) {
                mergedArray[k] = arrayA[i];
                i++;
                k++;
            } else {
                mergedArray[k] = arrayB[j];
                j++;
                k++;
            }
        }

If the input arrays have different length I use additional while loops to add the remaining elemants from the larger array

 while (i < arrayA.length) {
            mergedArray[k] = arrayA[i];
            i++;
            k++;
        }
        while (j < arrayB.length) {
            mergedArray[k] = arrayB[j];
            j++;
            k++;
        }

Also in order to make the algorithm more efficient at the beginning of my method I check for a situation when one or both of the arrays are empty. In this scenarion method will not enter the while loops and will be executed more efficiently

if (arrayA.length == 0) {
            return arrayB;
        } else if(arrayB.length == 0) {
            return
                    arrayA;
        }

Testing

In testing methods for my algorithm I tested for inputs of: 2 sorted arrays of different sizes, 2 empty arrays, 2 arrays with identical elements, 2 arrays of which one is empty, 2 arrays with negative values

Author

Patryk Blakala

Useful resources

[https://www.geeksforgeeks.org/java-while-loop-with-examples/)

[https://www.geeksforgeeks.org/java-program-to-merge-two-arrays/)

mergingtwosortedarrays's People

Contributors

patrykblakala avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.