当前位置: 首页 > news >正文

Java实现排序算法(一)

  1. /**
  2.  * @(#)SortTest.java
  3.  * 排序算法
  4.  *
  5.  * @author 
  6.  * @version 1.00 2008/8/2
  7.  */
  8. public class SortTest {
  9.     /**
  10.      *选择排序
  11.      *在找到全局第i小的时候记下该元素位置,最后跟第i个元素交换,从而保证数组最终的有序
  12.     */
  13.    void selectSort(int[] sortIn){
  14.         int i,j,temp,min=0;
  15.         for(i=0;i<sortIn.length;i++){
  16.             min=i;
  17.             for(j=i;j<sortIn.length;j++){//每一趟都选择一个最小的
  18.                 if(sortIn[j]<sortIn[min]) min=j;
  19.             }
  20.             //交换
  21.             swap(sortIn,i,min);
  22.         }
  23.     }
  24.     
  25.     /**
  26.      *冒泡排序
  27.      *算法思想是每次从数组末端开始比较相邻两元素,把第i小的冒泡到数组的第i个位置
  28.      */
  29.     void bubbleSort(int[] sortIn){
  30.         for(int i=0;i<sortIn.length;i++){
  31.             for(int j=sortIn.length-1;j>i;j--){
  32.                 if(sortIn[j]<sortIn[j-1])
  33.                     swap(sortIn,j-1,j);         
  34.             }
  35.         }
  36.     }
  37.     
  38.     /**
  39.      *插入排序
  40.      */
  41.     void insertSort(int[] sortIn){
  42.         for(int i=1;i<sortIn.length;i++){
  43.             int temp=sortIn[i];
  44.             int j=i-1;
  45.             while(j>=0&&temp<sortIn[j]){
  46.                 sortIn[j+1]=sortIn[j];//向后移动元素
  47.                 j--;
  48.             }
  49.             sortIn[j+1]=temp;
  50.         }
  51.     }
  52.     
  53.     /**
  54.      *希尔排序
  55.      *Shell排序每次把数据分成若个小块,来使用插入排序,而且之后在这若个小块
  56.      *排好序的情况下把它们合成大一点的小块,继续使用插入排序,不停的合并小块,
  57.      *知道最后成一个块,并使用插入排序。
  58.      *这里每次分成若干小块是通过“增量” 来控制的,开始时增量交大,接近N/2,
  59.      *从而使得分割出来接近N/2个小块,逐渐的减小“增量“最终到减小到1。
  60.      */
  61.     void sheelSort(int[] sortIn){
  62.         int d=sortIn.length;
  63.         while(d>1){
  64.             d=(d+1)/2;
  65.             for(int i=0;i<sortIn.length-d;i++){
  66.                 if(sortIn[i+d]<sortIn[i]){
  67.                     swap(sortIn,i+d,i);
  68.                 }
  69.             }
  70.         }
  71.     }
  72.     /**
  73.      *快速排序
  74.      *(1)分解(2)求解(3)组合
  75.      */
  76.     void quickSort(int[] sortIn,int low,int high){
  77.         int pivotpos;//划分后的基准记录的位置
  78.         if(low<high){//仅当区间长度大于1时才需排序
  79.             pivotpos=partition(sortIn,low,high);//做划分
  80.             quickSort(sortIn,low,pivotpos-1);//对左区间进行递归排序
  81.             quickSort(sortIn,pivotpos+1,high);//对右区间进行递归排序
  82.         }
  83.     }
  84.     
  85.     private int partition(int[] sortIn,int i,int j){
  86.         int pivot=sortIn[i];
  87.         while(i<j){
  88.             //从右向左扫描,查找第一个关键字小于pivot的记录
  89.             while(i<j&&sortIn[j]>=pivot) j--;
  90.             if(i<j){
  91.                 swap(sortIn,i,j);//交换sortIn[i]和sortIn[j]
  92.                 i++;
  93.             }
  94.             
  95.             //从左向右扫描,查找第一个关键字大于pivot的记录
  96.             while(i<j&&sortIn[i]<=pivot) i++;
  97.             if(i<j){
  98.                 swap(sortIn,i,j);
  99.                 j--;
  100.             }
  101.         }
  102.         sortIn[i]=pivot;//基准位置已被最终定位
  103.         return i;
  104.     } 
  105.         
  106.     /**
  107.      *交换位置
  108.      */
  109.     private void swap(int a[],int i,int j){
  110.         int temp=a[i];
  111.         a[i]=a[j];
  112.         a[j]=temp;
  113.     }
  114.     
  115.     public static void main(String[] args){
  116.         int[] sortIn={49,38,56,97,76,13,27,99,55,3};
  117.         SortTest ss=new SortTest();
  118.         
  119.         //ss.selectSort(sortIn);
  120.         //ss.bubbleSort(sortIn);
  121.         //ss.insertSort(sortIn);
  122.         //ss.sheelSort(sortIn);
  123.         ss.quickSort(sortIn,0,sortIn.length-1);
  124.         for(int num:sortIn){
  125.             System.out.print(" "+num);
  126.         }
  127.     }
  128. }

相关文章:

  • JMS 之 Active MQ 的spring整合
  • Java实现排序算法(二)
  • vue项目实战爬坑小记001
  • Java实现排序算法(三)
  • Java通信编程之Socket入门
  • 回车提交表单
  • 数据库的查询优化技术
  • 微信jssdk分享功能,jssdk成功调用,分享内容自定义失败
  • 用VS2005制造WEB安装程序
  • Java对象实例化顺序
  • 非阻塞模式下,虽然connect出错,但是getsockopt取得的错误却是0的问题
  • Java面试之判断对错
  • PL/SQL
  • 数据库系统优化--业务逻辑设计优化
  • 【WPF】DispatcherFrame 是个啥玩意儿
  • 分享一款快速APP功能测试工具
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • 【407天】跃迁之路——程序员高效学习方法论探索系列(实验阶段164-2018.03.19)...
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • 03Go 类型总结
  • CentOS学习笔记 - 12. Nginx搭建Centos7.5远程repo
  • Docker入门(二) - Dockerfile
  • Effective Java 笔记(一)
  • JavaScript工作原理(五):深入了解WebSockets,HTTP/2和SSE,以及如何选择
  • JavaScript新鲜事·第5期
  • Java-详解HashMap
  • Sequelize 中文文档 v4 - Getting started - 入门
  • spark本地环境的搭建到运行第一个spark程序
  • Spring核心 Bean的高级装配
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • Vim Clutch | 面向脚踏板编程……
  • vuex 笔记整理
  • Vultr 教程目录
  • Yeoman_Bower_Grunt
  • 高度不固定时垂直居中
  • 世界上最简单的无等待算法(getAndIncrement)
  • 手写双向链表LinkedList的几个常用功能
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • 在 Chrome DevTools 中调试 JavaScript 入门
  • 带你开发类似Pokemon Go的AR游戏
  • 继 XDL 之后,阿里妈妈开源大规模分布式图表征学习框架 Euler ...
  • ​​​​​​​Installing ROS on the Raspberry Pi
  • #if和#ifdef区别
  • (2/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (a /b)*c的值
  • (c语言)strcpy函数用法
  • (四)TensorRT | 基于 GPU 端的 Python 推理
  • (转)使用VMware vSphere标准交换机设置网络连接
  • .[backups@airmail.cc].faust勒索病毒的最新威胁:如何恢复您的数据?
  • .h头文件 .lib动态链接库文件 .dll 动态链接库
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .NET core 自定义过滤器 Filter 实现webapi RestFul 统一接口数据返回格式
  • .NET Core、DNX、DNU、DNVM、MVC6学习资料
  • .NET构架之我见
  • .net经典笔试题