python统计单词出现次数并排序_PTA 题解:jmu-Java&Python-统计文字中的单词数量并按出现次数排序...

题目说明

题干

f80bf318453ec2e11dbd6d90232e9fca.png

测试数据 1

输入样例

failure is probably the fortification in your pole

it is like a peek your wallet as the thief when you

are thinking how to spend several hard-won lepta

when you are wondering whether new money it has laid

background because of you then at the heart of the

most lax alert and most low awareness and left it

godsend failed

!!!!!

输出样例

46

the=4

it=3

you=3

and=2

are=2

is=2

most=2

of=2

when=2

your=2

测试数据 2

输入样例

Failure is probably The fortification in your pole!

It is like a peek your wallet as the thief wh

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
快速排序是一种常用的排序算法,它的基本思想是通过一趟排序将待记录分隔成独立的两部分,其一部分记录的关键字均比另一部分记录的关键字小,然后再按此方法对这两部分记录分别进行快速排序,以达到整个序列有序的目的。 以下是JAVA快速排序的实现代码: ```java public class QuickSort { public static void sort(int[] arr, int start, int end) { if (start < end) { int pivot = partition(arr, start, end); sort(arr, start, pivot - 1); sort(arr, pivot + 1, end); } } private static int partition(int[] arr, int start, int end) { int pivot = arr[start]; while (start < end) { while (start < end && arr[end] >= pivot) { end--; } arr[start] = arr[end]; while (start < end && arr[start] <= pivot) { start++; } arr[end] = arr[start]; } arr[start] = pivot; return start; } public static void main(String[] args) { int[] arr = {5, 6, 8, 1, 3, 2, 9, 4, 7}; sort(arr, 0, arr.length - 1); for (int i : arr) { System.out.print(i + " "); } } } ``` 在快速排序,需要选择一个元素作为枢轴(pivot),这里选择第一个元素作为枢轴。然后将序列的元素分为两部分,一部分比枢轴小,一部分比枢轴大。然后再对这两部分分别进行快速排序,最终得到有序序列。 在上述代码,partition方法是关键,它实现了分割序列的功能。首先将枢轴存储在pivot变量,然后从序列的两端开始遍历,找到第一个比枢轴小的元素和第一个比枢轴大的元素,然后将它们交换位置。重复这个过程直到start和end重合,最后将枢轴放到这个位置上。 sort方法是递归实现的,每次选择一个枢轴,然后将序列分为两部分,分别对这两部分进行快速排序,直到序列有序为止。 最后在main方法,我们对一个无序序列进行快速排序,并输出有序序列。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值