search
HomeJavaJavagetting StartedWhat is the efficient way to merge ordered arrays in java

What is the efficient way to merge ordered arrays in java

Dec 14, 2020 pm 04:00 PM
javamethodordered array

What is the efficient way to merge ordered arrays in java

Let’s take a look at the original question first:

(Learning video sharing: java teaching video)

/**
 * 
 ClassName: MergeSortArray <br/>
 * 
 Function: 合并有序数组<br/>
 *   [1, 2, 2, 5]
 *   [3, 4, 7, 8, 9]
 *
 *
 */

Idea analysis :

Double pointers move the comparison from front to back, and then copy the remaining data to the merged array. In fact, this is also the core code of merge sort. Merge sort (split first and then merge) is divided and conquered. part of governance.

Implementation code:

public static int[] mergeSortArray(int[] a, int[] b){
        int length1 = a.length, length2 = b.length;
        int[] merge = new int[length1 + length2];
        int i = 0, j = 0, k = 0;
        while(i < length1 && j < length2){
            if(a[i] <= b[j]){
                merge[k++] = a[i++];
            }else{
                merge[k++] = b[j++];
            }
        }
        while(i < length1){
            merge[k++] = a[i++];
        }
        while(j < length2){
            merge[k++] = b[j++];
        }
        return merge;
    }


    public static void main(String[] args) {
        int[] a = {1, 2, 2, 5};
        int[] b = {3, 4, 7, 8, 9};
        int[] merge = mergeSortArray(a, b);
        for(int i = 0; i < merge.length; i++){
            System.out.println(merge[i]);
        }
    }

Running result:

1
2
2
3
4
5
7
8
9

Related recommendations: java introductory tutorial

The above is the detailed content of What is the efficient way to merge ordered arrays in java. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.