Java利用Strategy模式实现堆排序
作者:代码骑士 时间:2022-07-16 08:01:04
将通用算法放入具体类(HeapSorter),并将通用算法必须调用的方法定义在接口(HeapSorterHandle)中,从这个接口派生出DoubleHeapSorter并传给HeapSorter,之后就可以委托这个接口实现具体工作了。
1、图示
2、项目资产
3、源代码
接口类:HeapSortHandle
public interface HeapSorterHandle {
public void swap(int index);
public int length();
public void setArray(Object array);
public void adjustHeap(int i, int length);
}
接口派生类: DoubleHeapSorter
public class DoubleHeapSorter implements HeapSorterHandle{
private double[] array = null;
public void swap(int index)
{
double temp = array[index];
array[index] = array[0];
array[0] = temp;
}
public int length()
{
return array.length;
}
public void setArray(Object array)
{
this.array = (double[])array;
}
public void adjustHeap(int i, int length)
{
double temp = array[i];
for(int k=i*2+1;k<length;k=k*2+1)
{
if(k+1<length && array[k]<array[k+1])
{
k++;
}
if(array[k] >temp)
{
array[i] = array[k];
i = k;
}
else
{
break;
}
}
array[i] = temp;
}
}
具体算法类 HeapSorter :
public class HeapSorter {
private int length = 0;
private HeapSorterHandle itsSortHandle = null;
public HeapSorter(HeapSorterHandle handle)
{
itsSortHandle = handle;
}
public void Sort(Object array)
{
itsSortHandle.setArray(array);
length = itsSortHandle.length();
if (length <= 1) return;
for(int i=length/2-1;i>=0;i--)
{
itsSortHandle.adjustHeap(i,length);
}
for(int j=length-1;j>0;j--)
{
itsSortHandle.swap(j);
itsSortHandle.adjustHeap(0,j);
}
return;
}
}
主类Main:
import java.util.Arrays;
public class Main {
public static void main(String[] args)
{
HeapSorter HS = new HeapSorter(new DoubleHeapSorter());
double []array = {1,5,2,3,4,6,7};
System.out.println("堆排序前:"+Arrays.toString(array));
HS.Sort(array);
System.out.println("堆排序后:"+Arrays.toString(array));
}
}
输出:
注:javac -encoding UTF-8 Main.java(防止乱码)
来源:https://blog.csdn.net/qq_51701007/article/details/127079544
标签:Java,Strategy,堆排序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
maven scope provided和runtime的例子说明
2023-04-28 02:15:00
![](https://img.aspxhome.com/file/2023/5/88075_0s.jpg)
Android防止按钮过快点击造成多次事件的解决方法
2022-03-30 08:35:58
java实现简易的学籍管理系统
2023-07-21 07:47:26
![](https://img.aspxhome.com/file/2023/4/71024_0s.jpg)
SpringBoot框架RESTful接口设置跨域允许
2021-12-31 13:40:19
unity绘制一条流动的弧线(贝塞尔线)
2022-09-03 18:15:00
![](https://img.aspxhome.com/file/2023/6/129676_0s.jpg)
mybatis-plus 扩展批量新增的实现
2023-07-14 14:24:13
Springboot框架实现自动装配详解
2023-03-03 21:11:14
![](https://img.aspxhome.com/file/2023/5/81235_0s.png)
Flutter Widgets粘合剂CustomScrollView NestedScrollView滚动控件
2023-07-06 01:24:29
![](https://img.aspxhome.com/file/2023/9/120089_0s.webp)
关于Spring MVC在Controller层中注入request的坑详解
2023-08-24 11:15:43
![](https://img.aspxhome.com/file/2023/4/58414_0s.png)
详解IDEA中SpringBoot整合Servlet三大组件的过程
2023-05-06 15:23:13
java异常处理的简单练习
2023-12-01 02:20:48
WPF实现半圆形导航菜单
2023-06-09 07:02:56
![](https://img.aspxhome.com/file/2023/6/129646_0s.gif)
java io文件操作删除文件或文件夹的7种方法
2022-08-09 13:29:21
![](https://img.aspxhome.com/file/2023/8/99848_0s.jpg)
C# WPF 自定义按钮的方法
2021-08-30 23:42:11
![](https://img.aspxhome.com/file/2023/0/69240_0s.gif)
SpringBoot基于Actuator远程关闭服务
2022-06-24 21:36:37
浅析Java中的GC垃圾回收器的意义及与GC的交互
2022-11-10 13:07:05
Spring Boot2.x如何自定义Endpoint
2023-03-05 18:04:12
C#之set与get方法的用法案例
2021-08-09 01:17:18
![](https://img.aspxhome.com/file/2023/9/88399_0s.jpg)
详解Java中的Lambda表达式
2021-08-17 00:06:00
![](https://img.aspxhome.com/file/2023/9/105919_0s.png)
Gradle:修改默认的Build配置文件名方式
2023-11-28 20:18:01
![](https://img.aspxhome.com/file/2023/7/60617_0s.png)