快排+java实现
import java.util.Arrays;
public class QuickSort {
//三数取中法。取出不大不小的那个位置
public static int getPivotPos(int[] a,int low,int high) {
int mid=(low+high)/2;
int pos=low;
if(a[mid]<a[low]) {
int temp=a[low];
a[low]=a[mid];
a[mid]=temp;
}
if(a[high]<a[low]) {
int temp=a[high];
a[high]=a[low];
a[low]=temp;
}
if(a[high]<a[mid]) {
int temp=a[high];
a[high]=a[mid];
a[mid]=temp;
}
pos=mid;
return pos;
}
//划分,取出枢纽位置
public static int partition(int[] a,int low,int high) {
int pivotpos=getPivotPos(a,low,high);
int pivot=a[pivotpos];
int temp=pivot;
a[pivotpos]=a[low];
a[low]=temp;
while(low<high) {
while(low<high&&a[high]>=pivot) high--;
a[low]=a[high];
while(low<high&&a[low]<=pivot) low++;
a[high]=a[low];
}
a[low]=pivot;
return low;
}
//快排
public static void quickSort(int[] a,int low,int high) {
if(low<high) {
int pivotpos=partition(a,low,high);
quickSort(a,low,pivotpos-1);
quickSort(a,pivotpos+1,high);
}
}
//重载快排
public static void quickSort(int[] a) {
if(a.length==0)
return;
int low=0;
int high=a.length-1;
quickSort(a,low,high);
}
public static void main(String[] args) {
int[] a= {12,32,24,99,54,76,48};
quickSort(a);
System.out.println(Arrays.toString(a));
}
}
快排+java实现的更多相关文章
- 快速排序及三向切分快排——java实现
快速排序也是一种分治算法.主要思想是选取一个切分点,将大于切分点的元素都放置到数组右侧,小于切分点的元素都放置到数组左侧:然后递归,再对切分点左侧和右侧分别排序. 归并排序时递归在前,归并在后,快速排 ...
- 快排java实现
package sort; public class QuickSort { public static final int cutoff = 3; /** * insertion sort * * ...
- 快排java代码
定一个基准位,递归左右两边排序. public void fun(){ int arr[] = {2,3,4,5,6,7,822,3,4,5,8,6,5,4,2,1}; //System.out.pr ...
- Java常见的几种排序算法-插入、选择、冒泡、快排、堆排等
本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...
- 折半、快排、插入排序的Java实现
插入排序 import java.util.Arrays; public class InsertionSort { /** * 对数组里面进行插入排序 * 参数1 数组 * 参数2 数组大小 */ ...
- Java实现的各种排序算法(包括冒泡,快排等)
//堆排序 不稳定 import java.util.Arrays; public class HeapSort { public static void main(String[] args) { ...
- Java 排序(快排,归并)
Java 排序有Java.util.Arrays的sort方法,具体查看JDK API(一般都是用快排实现的,有的是用归并) package yxy; import java.util.Arrays; ...
- 快排的java实现方式,用java代码来实现快排
1. 快排的思想 通过一趟排序将要排序的数据分割成独立的两部分,前一部分的所有数据都要小于后一部分的所有数据,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据的 ...
- 快排算法Java版-每次以最左边的值为基准值手写QuickSort
如题 手写一份快排算法. 注意, 两边双向找值的时候, 先从最右边起找严格小于基准值的值,再从最左边查找严格大于基准base的值; 并且先右后左的顺序不能反!!这个bug改了好久,233~ https ...
随机推荐
- python opencv 检测特定颜色
import cv2 import numpy as np cap = cv2.VideoCapture(0) # set blue thresh 设置HSV中蓝色.天蓝色范围 lower_blue ...
- 把你的Centos设置成代理ip服务器
前言:最近在公司做爬虫相关的工作,做过数据抓取的都知道,写程序抓取数据的过程并不像平常我们用浏览器打开网页那么简单!大多数的网站为了自己站点的性能和数据安全都设置了各种反爬策略.最常见的就是添加验证码 ...
- Shell批量启动、关闭tomcat
批量启动tomcat脚本,配置NUM可控制启动数量 #!/bin/bash #identifier CLUSTER_HOME=/opt/cluster-tomcat TNAME=tomcat-- TP ...
- 微信小程序 windos server 2008 iis 7 tls1.0 升级 tls1.2
执行下面注册表:重启服务器 下载:tls 1.2.reg 1.代码如下 Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\ ...
- 09:vuex组件间通信
1.1 vuex简介 官网:https://vuex.vuejs.org/zh/guide/ 参考博客:https://www.cnblogs.com/first-time/p/6815036.htm ...
- TCP 的那些事儿(上)(转)
原文地址:http://kb.cnblogs.com/page/209100/ TCP是一个巨复杂的协议,因为他要解决很多问题,而这些问题又带出了很多子问题和阴暗面.所以学习TCP本身是个比较痛苦的过 ...
- Magnum Kubernetes源码分析(二)
Kubernetes Master Stack kubernetes master的stack的resources主要分为三个部分. master wait handle wait handle主要用 ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- git download error processing
git clone git@github.com:happyfish100/fastdfs.git 提示下列信息: Warning: Permanently added 'github.com,192 ...
- RedHat7安装Docker
RedHat 启动 docker报错: 错误:Error starting daemon: SELinux is not supported with the overlay2 graph drive ...