备战蓝桥杯,用JAVA刷洛谷算法题单:【算法1-2】排序

参考

【算法1-2】排序 - 题单 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

image-20240303093616205

计数排序

P1271 【深基9.例1】选举学生会

桶排序

package al1_2;

import java.util.Scanner;

public class P1271 {
    public static void main(String[] args) {
        // algo 桶排序
        // 每次投票的结果可能值为1-999,所以定义桶的个数为999个
        // 每次投票后都把对应结果的桶的值+1,最后从下标递增输出桶的下标(如果该桶值=0,表示没有该桶的投票,则不用输出)
        // 例如投票221,那么桶值为:1、2、0、0……,输出为1、2、2
        // 桶的值表示这种结果出现了几次,并且最后是根据下标输出,达到了排序的效果
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int[] t = new int[1000];
        while (m > 0) {
            int num = scanner.nextInt();
            t[num]++;
            m--;
        }
        for (int i = 0; i < t.length; i++) {
            while (t[i] > 0) {
                // 注意这里输出的是i(桶对应的结果值),而不是输出桶的值
                System.out.printf("%d ", i);
                t[i]--;
            }
        }

        scanner.close();
    }
}

快速排序

P1923 【深基9.例4】求第 k 小的数

快速排序求第k个小的数,每次排序完之后数轴值在全部顺序内的位置被固定,所以判断数轴值是不是在全部顺序内第k个即可

package al1_2;

import java.util.Arrays;
import java.util.Scanner;

public class P1923 {

    private static int[] nums;
    private static int n;
    private static int a;

    public static void main(String[] args) {
        // algo 排序:快速排序求第k小的值
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        a = scanner.nextInt();
        nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = scanner.nextInt();
        }
        kp(0, n - 1);
        scanner.close();
    }

    public static void kp(int start, int end) {
        int left = start, right = end;
        int temp = nums[left];
        while (left < right) {
            while (left < right && nums[right] >= temp) {
                right--;
            }
            nums[left] = nums[right];
            while (left < right && nums[left] <= temp) {
                left++;
            }
            nums[right] = nums[left];
        }
        nums[left] = temp;
        // 判断枢轴值是否为a
        if (left == a) {
            System.out.println(nums[left]);
        } else if (left < a) {
            kp(left + 1, end);
        } else {
            kp(start, left - 1);
        }
    }
}

去重+排序

P1059 [NOIP2006 普及组] 明明的随机数

Java自带类排序有三种,Collections、Arrays针对数组、类的sort方法(可以自定义Comparator.compare)

package al1_2;

import java.util.*;

public class P1059 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        // algo 去重+排序
        HashSet<Integer> hashSet = new HashSet<>();
        while (n-- > 0) {
            hashSet.add(scanner.nextInt());
        }
        ArrayList<Integer> arrayList = new ArrayList<>(hashSet);
        // algo 排序:自带类排序
        // 1 Collections.sort(List)
        Collections.sort(arrayList);
        // 2 Arrays.sort(int[])
        //Arrays.sort();
        // 3 list.sort(Comparator)
        //arrayList.sort(Comparator.naturalOrder());
        System.out.println(arrayList.size());
        // algo lambda:打印输出
        arrayList.forEach((x) -> System.out.printf("%d ", x));
        scanner.close();
    }
}

结构体排序

P1093 [NOIP2007 普及组] 奖学金

自定义Comparator写法,注意compare函数返回正整数不交换(记住默认升序写法return o1-o2

package al1_2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class P1093 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        ArrayList<Stu> stus = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            String[] line = scanner.nextLine().split(" ");
            // algo 数组类型转换:https://blog.csdn.net/jx520/article/details/79020698
            int[] scores = Arrays.stream(line).mapToInt(Integer::valueOf).toArray();
            stus.add(new Stu(i, scores[0], scores[1], scores[2]));
        }
        stus.sort(new Comparator<Stu>() {
            //compare返回正整数不交换
            @Override
            public int compare(Stu o1, Stu o2) {
                int sum1 = o1.shuxue + o1.yuwen + o1.yingyu;
                int sum2 = o2.shuxue + o2.yuwen + o2.yingyu;
                if (sum1 == sum2) {
                    if (o1.yuwen == o2.yuwen) {
                        return o1.num - o2.num;
                    } else return o2.yuwen - o1.yuwen;
                } else return sum2 - sum1;
            }
        });
        for (int i = 0; i < 5; i++) {
            Stu o1 = stus.get(i);
            System.out.printf("%d %d\n", o1.num, o1.shuxue + o1.yuwen + o1.yingyu);
        }
        scanner.close();
    }
}

class Stu {
    int num;
    int yuwen;
    int shuxue;
    int yingyu;

    public Stu(int num, int yuwen, int shuxue, int yingyu) {
        this.num = num;
        this.yuwen = yuwen;
        this.shuxue = shuxue;
        this.yingyu = yingyu;
    }
}

字符串排序

P1781 宇宙总统

注意点就是判定条件,要先判断长度,如果长度相同在判断大小

package al1_2;

import java.util.Scanner;

public class P1781 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        int m = 1;
        String score = "0";
        for (int i = 1; i <= n; i++) {
            String s = scanner.nextLine();
            if (s.length() > score.length() || (s.length() == score.length() && s.compareTo(score) > 0)) {
                score = s;
                m = i;
            }
        }
        System.out.println(m);
        System.out.println(score);
        scanner.close();
    }
}
 (s.length() == score.length() && s.compareTo(score) > 0)) {
                score = s;
                m = i;
            }
        }
        System.out.println(m);
        System.out.println(score);
        scanner.close();
    }
}
  • 6
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值