Blue Bridge Cup Algorithm Training the shortest circuit (chain front star, spfa)

2023-01-23   ES  

merger sorting is an effective sorting algorithm based on merger operations. This algorithm is a very typical application of Divide and Conquer.

The

merger sorting method is to merge two (or more) or more meters into a new order, that is, divide the sequence to be sorted into several sequences, and each subsequent sequence is orderly. Then merge the order sequence to the overall order.
Merge the existing subsequent sequence to get a completely orderly sequence; that is, first make each subsequent sequence order in order, and then order the sub -sequence segment in an orderly manner. If the two ordered tables are merged into an orderly table, it is called 2-roads. Combined sorting is also called merger sorting.
Not much to say, the code:

import java.util.Scanner;
// Merge sorting
public class Test10 {

    public static void mergeSort(int a[], int left, int right) {
        if (left < right) {
   // At least 2 elements
            int i = (left + right) / 2; // Take the middle point
            mergeSort(a, left, i);
            mergeSort(a, i + 1, right);
            int b[]=new int[a.length];
            merge(a, b, left, i, right); // Merge two array segments to a new array B
            Copy(a, b, left, right); // Copy the merged array segments and then copy the return array A
        }
    }

    public static void merge(int c[], int d[], int l, int m, int r) {
        int i = l, j = m + 1, k = l;
        while ((i <= m) && (j <= r))
            if (c[i] <= c[j])
                d[k++] = c[i++];
            else
                d[k++] = c[j++];
        if (i > m)
            for (int q = j; q <= r; q++)
                d[k++] = c[q];
        else
            for (int q = i; q <= m; q++)
                d[k++] = c[q];
    }

    public static void Copy(int a[], int b[], int m, int n) {
        for (int i = m; i <= n; i++)
            a[i] = b[i];
    }

    public static void main(String[] args) {
        try {
            System.out.println("Sorting elements:");
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            int list[] = new int[n];
            System.out.println("Enter all elements in turn:");
            for (int i = 0; i <= list.length - 1; i++) {

                list[i] = sc.nextInt();
            }
            mergeSort(list, 0, list.length-1);
            for (int k = 0; k < list.length; k++) {
                System.out.print(list[k] + " ");

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Run results:
这里写图片描述
Reference to other languages:
Baidu Encyclopedia

source

Related Posts

Use deep learning to achieve breast cancer category, with Python drills! Every male compatriot meeting!

The most dirty technical interpretation in history, I even understood in seconds

[Unity] Create the project environment with githubdesktop management GIT code installation process FMJ

2021-03-02 COMP9021 Fifth Lesson Note

Blue Bridge Cup Algorithm Training the shortest circuit (chain front star, spfa)

Random Posts

TensorFlow’s anti -convolutional implementation

HDU 2795 Billboard

computer inlet, original code, inverse code, supplementary code, code transfer related knowledge

JENKINS Local Publisher Sweet SHE

application.properties file and Application.yml file? Amelia What is the difference between