头像

悲liu伤




离线:10小时前


最近来访(3)
用户头像
无須終有_2
用户头像
种花家的虎式坦克
用户头像
あこがれの秋

活动打卡代码 AcWing 787. 归并排序

悲liu伤
11小时前
import java.util.Scanner;

public class Main {
    public static Scanner sc = new Scanner(System.in);

    private static int[] array = new int[100010];
    private static int[] temp = new int[100010];

    public static void main(String[] args) {
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) array[i] = sc.nextInt();

        merge_sort(array, 0, n - 1);

        for (int i = 0; i < n; i++)  System.out.print(array[i] + " ");;
    }

    private static void merge_sort(int[] array, int l, int r) {
        if (l >= r) return;

        int mid = (l + r) >> 1;
        merge_sort(array, l, mid);
        merge_sort(array, mid + 1, r);

        int i = l, j = mid + 1, z = l;
        while (i <= mid && j <= r) {
            if (array[i] < array[j]) {
                temp[z++] = array[i++];
            } else {
                temp[z++] = array[j++];
            }
        }
        while (i <= mid) temp[z++] = array[i++];
        while (j <= r) temp[z++] = array[j++];

        System.arraycopy(temp, l, array, l, r - l + 1);
    }
}



活动打卡代码 AcWing 786. 第k个数

悲liu伤
11小时前
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] array = new int[n];
        for (int i = 0; i < n; i++) array[i] = sc.nextInt();

        quick_sort(array, 0, n - 1);

        System.out.println(array[k - 1]);;
    }

    private static void quick_sort(int[] array, int l, int r) {
        if (l >= r) return;

        int x = array[l + r >> 1], i = l - 1, j = r + 1;
        while (i < j) {
            while (array[++i] < x);
            while (array[--j] > x);

            if (i < j) sweep(array, i, j);
        }
        quick_sort(array, l, j);
        quick_sort(array, j + 1, r);
    }

    private static void sweep(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}



活动打卡代码 AcWing 785. 快速排序

悲liu伤
11小时前
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int[] array = new int[sc.nextInt()];
        for (int i = 0; i < n; i++) array[i] = sc.nextInt();

        quit_sort(array, 0, n - 1);

        for (int i = 0; i < n; i++)  System.out.print(array[i] + " ");;
    }

    private static void quit_sort(int[] array, int l, int r) {
        if (l >= r) return;

        int x = array[l];
        int i = l - 1, j = r + 1;

        while (i < j) {
            while (array[++i] < x);
            while (array[--j] > x);

            if (i < j) sweep(array, i, j);
        }
        quit_sort(array, l, j);
        quit_sort(array, j + 1, r);
    }

    private static void sweep(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}




悲liu伤
16天前

AcWing《算法基础课》拼团优惠!https://www.acwing.com/activity/content/introduction/11/group_buy/106653/



新鲜事 原文

悲liu伤
16天前
AcWing《算法基础课》拼团优惠!https://www.acwing.com/activity/content/introduction/11/group_buy/106653/


新鲜事 原文

悲liu伤
17天前
AcWing《算法提高课》拼团优惠!https://www.acwing.com/activity/content/introduction/16/group_buy/106474/ 三等一