java排序学习笔记
前面写了js的排序实现,总得玩玩java的哈。
同样,冒泡、选择、快速(这三个之前实现过也写过文章)、堆排序,然后做比较。
主要遇到的难点:
- -||想轻松点写个封装计时的逻辑,不想每调用一个排序就要写一个计时代码。想想,还是javascript写起来方便;
java的话,我想到的方法是写一个抽象类:抽象出排序方法,实现一个排序计时方法(该方法调用了抽象排序,但在先后排序时加入计时代码[感觉像是aop操作]);
接着所有排序类都继承这个抽象类,并实现排序方法,调用的时候直接调用继承的排序计时方法,这样就不用写多余的代码了。(不过这还搞继承,有点。。。不知道有啥高招呢?)
下面是展示各排序代码:
冒泡:
1 public class BubbleSort extends SortAbstract{ 2 3 public static void main(String[] args) { 4 int[] a = {1,8,5,6,3,7,5,4,8,9,12,2}; 5 new BubbleSort().sort(a); 6 for(int c : a){ 7 System.out.println(c); 8 } 9 } 10 public void sort(int[] array){ 11 if(array.length == 0)return ; 12 for(int i=0;i<array.length-1;i++){//i是计数标记 13 for(int j=0;j<array.length-i-1;j++){//注意终止条件的判断,冒泡的亮点在于从头到尾一对一对比较 14 if(array[j]>array[j+1]){ 15 swap(array, j, j+1); 16 } 17 } 18 } 19 } 20 public static void swap(int[] array,int i,int j){ 21 int temp = array[i]; 22 array[i] = array[j]; 23 array[j] = temp; 24 } 25 }
选择:
1 public class ChooseSort extends SortAbstract{ 2 public static void main(String[] args) { 3 int[] a = {1,8,5,6,3,7,5,4,8,9,12,2}; 4 new ChooseSort().sort(a); 5 for(int c : a){ 6 System.out.println(c); 7 } 8 } 9 10 public void sort(int[] array){ 11 if(array.length == 0)return ; 12 for(int i=0;i<array.length-1;i++){ 13 for(int j=i+1;j<array.length;j++){ 14 if(array[i]>array[j]){ 15 swap(array, i, j); 16 } 17 } 18 } 19 } 20 21 public static void swap(int[] array,int i,int j){ 22 int temp = array[i]; 23 array[i] = array[j]; 24 array[j] = temp; 25 } 26 }
View Code
快速:
1 public class QuickSort extends SortAbstract{ 2 public static void qSort(int[] num,int low,int high){ 3 if(low<high){ 4 int pivotloc = partition(num,low,high); 5 qSort(num, low, pivotloc-1); 6 qSort(num, pivotloc+1, high); 7 } 8 } 9 public void sort(int[] array){ 10 qSort(array, 0, array.length-1); 11 } 12 public static int partition(int[] num,int low,int high){ 13 int mid = num[low]; 14 int pivotkey = num[low]; 15 while(low<high){ 16 while(low<high&&num[high]>=pivotkey){ 17 --high; 18 } 19 num[low] = num[high]; 20 while(low<high&&num[low]<=pivotkey){ 21 ++low; 22 } 23 num[high]=num[low]; 24 } 25 num[low] = mid; 26 return low; 27 } 28 /** 29 * @param args 30 */ 31 public static void main(String[] args) { 32 // TODO Auto-generated method stub 33 int[] num = {9,18,-8,-6,-57,5,62,0}; 34 qSort(num, 0, num.length-1); 35 for(int i=0;i<num.length;i++){ 36 System.out.println(num[i]); 37 } 38 } 39 40 }
View Code
堆:
1 public class HeapSort extends SortAbstract { 2 public void sort(int[] array){ 3 if(array.length<=1){ 4 return; 5 } 6 int len = array.length; 7 for(int i=len/2+1;i>=0;i--){//初始最大堆 无序数组,由最右一个非子节点开始。这里len/2 +1 没想明白 8 maxHeapify(array, i, len); 9 } 10 for(int i = len-1; i>0; i-- ){ 11 swap(array, 0, i); //每次将堆根节点与尾部交换,然后逻辑上数组放弃掉尾部,实际有点像尾部冒泡 12 maxHeapify(array, 0, --len); //从顶部开始顶堆调整 13 } 14 } 15 public static int getLeft(int index){ 16 return index*2+1; 17 } 18 public static int getRight(int index){ 19 return (index+1)*2; 20 } 21 public static void swap(int[] array, int i, int j){ 22 int temp = array[i]; 23 array[i] = array[j]; 24 array[j] = temp; 25 } 26 public static void maxHeapify(int[] array, int index, int len){ 27 if(array.length==0 || len<=1){ 28 return; 29 } 30 int largest; 31 int left = getLeft(index); 32 int right = getRight(index); 33 if(left<len&&array[index]<array[left]){ 34 largest = left; 35 }else{ 36 largest = index; 37 } 38 if(right<len && array[right]>array[largest]){ 39 largest = right; 40 } 41 if(largest!=index){ 42 swap(array, largest, index); //交换两个位置的元素,并递归调用调整交换的孩子节点 43 maxHeapify(array, largest, len); 44 } 45 } 46 public static void main(String[] args){ 47 int[] a = {1,8,5,6,3,7,5,4,8,9,12,2}; 48 new HeapSort().sort(a); 49 for(int c : a){ 50 System.out.println(c); 51 } 52 } 53 }
View Code
计时抽象类:
1 public abstract class SortAbstract { 2 public abstract void sort(int[] array); 3 public void runWithTimer(int[] array){ 4 Date start = new Date(); 5 this.sort(array); 6 Date end = new Date(); 7 System.out.println("排序时间:(ms)"+(end.getTime()-start.getTime())); 8 } 9 }
测试用例:
1 public class SortTestMain { 2 public static void main(String[] args){ 3 int[] small = {6,44,33,2,3,5,2,1,7,9,8,14}; 4 BubbleSort bubbleSort = new BubbleSort(); 5 ChooseSort chooseSort = new ChooseSort(); 6 QuickSort quickSort = new QuickSort(); 7 HeapSort heapSort = new HeapSort(); 8 System.out.println("冒泡排序:"); 9 //int[] smallUse = small.clone(); 10 bubbleSort.runWithTimer(small.clone()); 11 System.out.println("选择排序:"); 12 chooseSort.runWithTimer(small.clone()); 13 System.out.println("快速排序:"); 14 quickSort.runWithTimer(small.clone()); 15 System.out.println("堆排序:"); 16 heapSort.runWithTimer(small.clone()); 17 18 System.out.println("对a[10000]排序:"); 19 int[] big = new int[10000]; 20 for(int i=0; i<10000; i++){ 21 big[i] = (int)Math.floor(Math.random()*100001); 22 } 23 System.out.println("冒泡排序:"); 24 //int[] smallUse = small.clone(); 25 bubbleSort.runWithTimer(big.clone()); 26 System.out.println("选择排序:"); 27 chooseSort.runWithTimer(big.clone()); 28 System.out.println("快速排序:"); 29 quickSort.runWithTimer(big.clone()); 30 System.out.println("堆排序:"); 31 heapSort.runWithTimer(big.clone()); 32 } 33 }
测试结果:
结论:
几个元素的排序,基本很快。只有当数据开始多时,堆和快速开始展现出优势。
转载于:https://www.cnblogs.com/dont27/p/3801709.html
java排序学习笔记相关推荐
- Java中大数据数组,Java基础学习笔记之数组详解
摘要:这篇Java开发技术栏目下的"Java基础学习笔记之数组详解",介绍的技术点是"java基础学习笔记.基础学习笔记.Java基础.数组详解.学习笔记.Java&qu ...
- java多线程学习笔记。
java多线程学习笔记 线程的优缺点: 多线程的好处: 充分利用多处理核心,提高资源的利用率和吞吐量. 提高接口的响应效率,异步系统工作. 线程的风险: 安全危险(竞争条件):什么坏事都没有发生.在没 ...
- 黑马程序员Java教程学习笔记(五)
学习视频:https://www.bilibili.com/video/BV1Cv411372m 如侵权,请私信联系本人删除 文章目录 黑马程序员Java教程学习笔记(五) 日期时间:Date.Sim ...
- 【Java】学习笔记2——从小白到入门(技术提升篇)
写在前面 [Java]学习笔记1--从小小白到小白 (基础知识篇)里记录了Java中最最基础的知识,在对基础知识有了基本了解之后,就可以开始着手技术提升了.本篇博客也将延续第一篇,继续记录我的Java ...
- Java NIO 学习笔记(三)----Selector
目录: Java NIO 学习笔记(一)----概述,Channel/Buffer Java NIO 学习笔记(二)----聚集和分散,通道到通道 Java NIO 学习笔记(三)----Select ...
- 可能是最全面的 Java G1学习笔记
转载自 可能是最全面的 Java G1学习笔记 引子 最近遇到很多朋友过来咨询G1调优的问题,我自己去年有专门学过一次G1,但是当时只是看了个皮毛,因此自己也有不少问题.总体来讲,对于G1我有几个疑惑 ...
- 深入浅出 Java CMS 学习笔记
转载自 深入浅出 Java CMS 学习笔记 引子 带着问题去学习一个东西,才会有目标感,我先把一直以来自己对CMS的一些疑惑罗列了下,希望这篇学习笔记能解决掉这些疑惑,希望也能对你有所帮助. 1. ...
- Java NIO学习笔记之图解ByteBuffer
转载自 Java NIO学习笔记之图解ByteBuffer ByteBuffer前前后后看过好几次了,实际使用也用了一些,总觉得条理不够清晰. <程序员的思维修炼>一本书讲过,主动学习,要 ...
- 转载:mongoDB java驱动学习笔记
http://www.blogjava.net/watchzerg/archive/2012/09/22/388346.html mongoDB java驱动学习笔记 指定新mongo实例: Mong ...
最新文章
- SQL批量修改字段值
- linux 内核钩子 khook 项目介绍
- 二十一、Python爬取百度文库word文档内容
- 第5章 Python 数字图像处理(DIP) - 图像复原与重建1 - 高斯噪声
- .Net Core集成Office Web Apps(一)
- ArrayList 有序集合 c#
- #窗体整人小程序_Excel VBA和文件夹-1.8通过对话框灵活选定文件的小技巧
- grafana官方使用文档_5. Centos7 下部署使用 nmon2influxdb
- RecognizerIntent(语音识别)
- XUtils BitmapUtils 改造以加入drawable支持
- 您如何性能测试JavaScript代码?
- 利用膨胀和腐蚀获取图像边缘 matlab实现
- 论坛指定portal.php,去除Discuz论坛域名后面的/forum.php或portal.php
- ubuntu 下的开发板的环境配置
- mysql gay锁_MySQL事务(二) - osc_gay6i4ve的个人空间 - OSCHINA - 中文开源技术交流社区...
- Matlab实现常见的插值算法
- 点云配准1-ICP算法 原理代码实现
- hdu 4500 小Q系列故事——屌丝的逆袭
- 使用GraalVM实现java调用python脚本
- 关于线程中断thread interrupt
热门文章
- Golang实践录:使用gin框架实现转发功能:上传文件并转
- python scheduler 定时执行_python使用apscheduler做定时任务的管理
- 【maven】idea左侧External Libraries里,没有Maven的依赖包 代码飘红
- 【ElasticSearch】Es 源码之 IndicesModule 源码解读
- 【guava】GuavaCache缓存失效的时候做一些操作 RemovalListener
- 【MySQL】mysql查看库 表占用存储空间大小
- 【yarn】INFO ipc.Client Retrying connect to server xxx 8032 Already tried 0 time(s)
- SparkStreaming之mapWithState
- Spring : Transaction源码解析
- flink报错;IllegalArgumentException: requirement failed The class xx$3 is an instance class, mean