Small sum problem and Dutch flag problem

Xiao He problem
In an array, the sum of the numbers smaller than the current number on the left side of each number is called the sum of the arrays. Find the small sum of an array. Example:
[1,2,3,4]
A number smaller than 1 on the left, no;
2 the number smaller than 2 on the left, 1;
3 the number smaller than 3 on the left, 1, 2;
The number smaller than 4 on the left, 1,2,3,;
So the small sum is 1 + 1 + 2 + 1 + 2 + 3 = 10
Idea: the idea of merging and sorting: in the process of merging two arrays, count how many on the right side are larger than the number on the left, then multiply the number and add it

public class work1 {
    public static int mergeSort(int[] arr, int left, int right) {
        if(left == right)
            return 0;
        int mid = left + (right - left) / 2;
        return mergeSort(arr, left, mid) + mergeSort(arr, mid+1, right)
                + merge(arr, left, mid, right);
    }
    public static int merge(int[] arr, int left, int mid, int right) {
        int[] help = new int[right - left +1];
        int i = 0;
        int res = 0;
        int p1 = left;
        int p2 = mid + 1;
        while(p1 <= mid && p2 <= right) {
            //When merging, calculate how many elements on the right side are greater than arr[p1], and then multiply arr[p1] by the number to add up
            res += arr[p1] < arr[p2]? arr[p1] *(right - p2 + 1) : 0;
            help[i++] = arr[p1] < arr[p2]? arr[p1++] : arr[p2++];
        }
        while(p1 <= mid) {
            help[i++] = arr[p1++];
        }
        while(p2 <= right) {
            help[i++] = arr[p2++];
        }
        for(i = 0; i < help.length; i++)
            arr[left + i] = help[i];
            return res;
    }
    public static void main(String[] args)
    {
        int[] arr = new int[] {1,2,3,4};
        System.out.print("Primitive array:");
        for(int i = 0; i < arr.length; i++)
            if(i != arr.length - 1)
            System.out.print(arr[i] + " ");
            else
                System.out.println(arr[i]);
        int sum = mergeSort(arr, 0 ,arr.length - 1);
        System.out.print("Decimal sum:");
        System.out.println(sum);

    }
}

Operation result:

Dutch flag problem: for example, given an array: [334,6323,44,88,88,56,90], given a value of 88, then after processing the original array, we may get a situation that: [56, 6, 44, 88, 88, 323, 90, 334], it should be noted that the part less than 88 does not need to be ordered, the part greater than 88 does not need to be ordered, and the left and right subscripts equal to 4 parts are returned, that is, [3, 4]
Idea: the idea of fast row, use two pointers to maintain the left less than num and the right greater than num, scan once to complete the left less than num, the right greater than num, but the left and right do not need to be orderly

import java.util.Scanner;

public class work2 {
    public static int[] partition(int[] arr, int L, int R, int num) {
        int less = L - 1;
        int more = R + 1;
        while(L < more) {
            if(arr[L] < num) {
                swap(arr, ++less, L++);
            }else if(arr[L] > num) {
                swap(arr, --more, L);
            }else {
                L++;
            }
        }
        return new int[]{less + 1, more - 1};
    }
    public static void swap(int[] arr, int i,int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    } 
    public static void main(String[] args) {
        int[] arr = new int[]{334,6,323,44,88,88,56,90};
        System.out.print("Primitive array:");
        for(int i = 0; i < arr.length; i++)
            if(i != arr.length - 1)
                System.out.print(arr[i] + " ");
            else
                System.out.println(arr[i]);
        System.out.print("Please enter an integer:");
        Scanner in = new Scanner(System.in);
        int num = in.nextInt();
        int[] p = partition(arr, 0, arr.length - 1, num);
        System.out.println("partition after");
        for(int i = 0; i < arr.length; i++)
            if(i != arr.length - 1)
                System.out.print(arr[i] + " ");
            else
                System.out.println(arr[i]);
        System.out.println(num + "The left boundary of is:" + p[0] + ",The right border is:" + p[1]);
    }
}

Operation result:

Tags: Java less

Posted on Sat, 21 Mar 2020 11:44:04 -0400 by neogeek