Java中的桶排序
将给定数组的元素分配到多个桶中,使用不同的排序算法或递归地使用桶排序算法对每个桶进行排序的排序技术在Java中称为桶排序,其空间复杂度为O (1),最坏情况复杂度为 O(n^2),最好情况复杂度为 Omega(n+k),平均情况复杂度为 theta(n+k),桶排序技术对数组的给定元素进行排序的工作原理与其他排序算法相比,速度更快,并且使用桶排序算法排序的数组元素必须均匀分布。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
Java中进行桶排序的函数如下:
public static int[] bucketsort(int[] array, int maximum_value) { int[] newbucket = new int[maximum_value + 1]; int[] sorted_array = new int[array.length]; for (int a= 0; a <array.length; a++) newbucket[array[a]]++; int position = 0; for (int b = 0; b < newbucket.length; b++) for (int c = 0; c < newbucket[b]; c++) sorted_array[position++] = b; return sorted_array; }
其中 array 是要使用桶排序算法排序的输入数组,maximum_value 是给定数组中存在的 maximum_value,sorted_array 是由排序元素组成的结果数组。
Java 中桶排序算法的工作原理
Java中桶排序算法的工作原理如下:
- 桶排序算法的第一步是创建一个空数组,该数组被视为桶。
- 第二步是遍历整个要排序的输入数组,并将每个元素添加到桶中。
- 第三步是对桶中的每个元素进行排序。
- 第四步,遍历桶中的所有元素,并将每个元素按排序顺序添加到原始输入数组中。
Java 中桶排序的示例
以下是示例:
示例#1
Java 程序通过实现桶排序算法对给定数组的元素进行排序,然后将排序后的数组元素显示为屏幕上的输出:
代码:
import java.util.*; public class Main { public static int[] bucketsort(int[] array, int maximum_value) { //creating an empty array called newbucket which is considered as bucket array int[] newbucket = new int[maximum_value + 1]; //creating another empty array called sorted_array to store the result array int[] sorted_array = new int[array.length]; //traversing through the input array to add each element to the bucket array for (int a= 0; a <array.length; a++) newbucket[array[a]]++; //sorting each element in the bucket array and adding each sorted element in order to the original input array int position = 0; for (int b = 0; b < newbucket.length; b++) for (int c = 0; c < newbucket[b]; c++) sorted_array[position++] = b; return sorted_array; } //function to find the maximum value in the input array in order to sort the given array using bucket sort technique static int maximumValue(int[] array) { int maximum_value = 0; for (int d = 0; d < array.length; d++) if (array[d] > maximum_value) maximum_value = array[d]; return maximum_value; } //main function is called within which we display the resulting array public static void main(String args[]) { int[] array ={100, 90, 80, 70, 60, 50, 40, 30, 20, 10}; int maximum_value = maximumValue(array); System.out.print("\nThe elements of the array to be sorted are:\n "); System.out.println(Arrays.toString(array)); System.out.print("\nThe elements of the sorted array sorted using bucket sort algorithm are:\n "); System.out.println(Arrays.toString(bucketsort(array,maximum_value))); } }
输出:
在上面的程序中,我们创建了一个名为 newbucket 的空数组,它被视为存储桶数组。然后我们创建另一个名为sorted_array 的空数组来存储结果数组。然后我们遍历输入数组,将每个元素添加到存储桶数组中。然后,我们对存储桶数组中的每个元素进行排序,并将每个已排序的元素按顺序添加到原始输入数组中。然后我们定义一个函数来查找输入数组中的最大值,以便使用桶排序技术对给定数组进行排序。然后调用主函数,在其中显示结果数组。输出如上面的快照所示。
示例#2
Java 程序通过实现桶排序算法对给定数组的元素进行排序,然后将排序后的数组元素显示为屏幕上的输出:
代码:
import java.util.*; public class Main { public static int[] bucketsort(int[] array, int maximum_value) { //creating an empty array called newbucket which is considered as bucket array int[] newbucket = new int[maximum_value + 1]; //creating another empty array called sorted_array to store the result array int[] sorted_array = new int[array.length]; //traversing through the input array to add each element to the bucket array for (int a= 0; a <array.length; a++) newbucket[array[a]]++; //sorting each element in the bucket array and adding each sorted element in order to the original input array int position = 0; for (int b = 0; b < newbucket.length; b++) for (int c = 0; c < newbucket[b]; c++) sorted_array[position++] = b; return sorted_array; } //function to find the maximum value in the input array in order to sort the given array using bucket sort technique static int maximumValue(int[] array) { int maximum_value = 0; for (int d = 0; d < array.length; d++) if (array[d] > maximum_value) maximum_value = array[d]; return maximum_value; } //main function is called within which we display the resulting array public static void main(String args[]) { int[] array ={ 60, 80, 50, 90, 30, 70, 20 }; int maximum_value = maximumValue(array); System.out.print("\nThe elements of the array to be sorted are:\n "); System.out.println(Arrays.toString(array)); System.out.print("\nThe elements of the sorted array sorted using bucket sort algorithm are:\n "); System.out.println(Arrays.toString(bucketsort(array,maximum_value))); } }
输出:
在上面的程序中,我们创建了一个称为新存储桶的空数组,它被视为存储桶数组。然后我们创建另一个名为sorted_array 的空数组来存储结果数组。然后我们遍历输入数组,将每个元素添加到存储桶数组中。然后,我们对存储桶数组中的每个元素进行排序,并将每个已排序的元素按顺序添加到原始输入数组中。然后我们定义一个函数来查找输入数组中的最大值,以便使用桶排序技术对给定数组进行排序。然后调用主函数,在其中显示结果数组。输出如上面的快照所示。
以上是Java中的桶排序的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。
