首页 >Java >java教程 >Java中的桶排序

Java中的桶排序

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原创
2024-08-30 15:31:56930浏览

将给定数组的元素分配到多个桶中,使用不同的排序算法或递归地使用桶排序算法对每个桶进行排序的排序技术在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 int position="0;" for b="0;" newbucket.length c="0;" sorted_array return>
<p>其中 array 是要使用桶排序算法排序的输入数组,maximum_value 是给定数组中存在的 maximum_value,sorted_array 是由排序元素组成的结果数组。</p>
<h3>Java 中桶排序算法的工作原理</h3>
<p>Java中桶排序算法的工作原理如下:</p>
<ul>
<li>桶排序算法的第一步是创建一个空数组,该数组被视为桶。</li>
<li>第二步是遍历整个要排序的输入数组,并将每个元素添加到桶中。</li>
<li>第三步是对桶中的每个元素进行排序。</li>
<li>第四步,遍历桶中的所有元素,并将每个元素按排序顺序添加到原始输入数组中。</li>
</ul>
<h3>Java 中桶排序的示例</h3>
<p>以下是示例:</p>
<h4>示例#1</h4>
<p>Java 程序通过实现桶排序算法对给定数组的元素进行排序,然后将排序后的数组元素显示为屏幕上的输出:</p>
<p><strong> 代码:</strong></p>


<pre class="brush:php;toolbar:false">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 each element in the bucket array and adding sorted order to original input int position="0;" for b="0;" newbucket.length c="0;" sorted_array return find maximum value sort given using technique static maximumvalue maximum_value="0;" d="0;" array.length if> 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)));
}
}</array.length>

输出:

Java中的桶排序

在上面的程序中,我们创建了一个名为 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 each element in the bucket array and adding sorted order to original input int position="0;" for b="0;" newbucket.length c="0;" sorted_array return find maximum value sort given using technique static maximumvalue maximum_value="0;" d="0;" array.length if> 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)));
}
}</array.length>

输出:

Java中的桶排序

在上面的程序中,我们创建了一个称为新存储桶的空数组,它被视为存储桶数组。然后我们创建另一个名为sorted_array 的空数组来存储结果数组。然后我们遍历输入数组,将每个元素添加到存储桶数组中。然后,我们对存储桶数组中的每个元素进行排序,并将每个已排序的元素按顺序添加到原始输入数组中。然后我们定义一个函数来查找输入数组中的最大值,以便使用桶排序技术对给定数组进行排序。然后调用主函数,在其中显示结果数组。输出如上面的快照所示。

以上是Java中的桶排序的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Heap Sort In Java下一篇:Quick Sort in Java