Java中各种排序算法
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
- /**
- * @author treeroot
- * @since 2006-2-2
- * @version 1.0
- */
- public class InsertSort implements SortUtil.Sort{
- /** (non-Javadoc)
- * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
- */
- public void sort(int[] data) {
- int temp;
- for(int i=1;i<data.length;i++){
- for(int j=i;(j>0)&&(data[j]<data[j-1]);j--){
- SortUtil.swap(data,j,j-1);
- }
- }
- }
- }
- //冒泡排序:
- for(var i=0; i<arr.length; i++) {
- for(var j=i+1; j<=arr.length-1; j++) {
- if(eval(arr[i]) < eval(arr[j])) {
- temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- }
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
- /**
- * @author treeroot
- * @since 2006-2-2
- * @version 1.0
- */
- public class BubbleSort implements SortUtil.Sort{
- /** (non-Javadoc)
- * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
- */
- public void sort(int[] data) {
- int temp;
- for(int i=0;i<data.length;i++){
- for(int j=data.length-1;j>i;j--){
- if(data[j]<data[j-1]){
- SortUtil.swap(data,j,j-1);
- }
- }
- }
- }
- }
- //选择排序:
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
- /**
- * @author treeroot
- * @since 2006-2-2
- * @version 1.0
- */
- public class SelectionSort implements SortUtil.Sort {
- /**
- * (non-Javadoc)
- *
- * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
- */
- public void sort(int[] data) {
- int temp;
- for (int i = 0; i < data.length; i++) {
- int lowIndex = i;
- for (int j = data.length - 1; j > i; j--) {
- if (data[j] < data[lowIndex]) {
- lowIndex = j;
- }
- }
- SortUtil.swap(data,i,lowIndex);
- }
- }
- }
- //Shell排序:
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
- /**
- * @author treeroot
- * @since 2006-2-2
- * @version 1.0
- */
- public class ShellSort implements SortUtil.Sort{
- /** (non-Javadoc)
- * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
- */
- public void sort(int[] data) {
- for(int i=data.length/2;i>2;i/=2){
- for(int j=0;j<i;j++){
- insertSort(data,j,i);
- }
- }
- insertSort(data,0,1);
- }
- /**
- * @param data
- * @param j
- * @param i
- */
- private void insertSort(int[] data, int start, int inc) {
- int temp;
- for(int i=start+inc;i<data.length;i+=inc){
- for(int j=i;(j>=inc)&&(data[j]<data[j-inc]);j-=inc){
- SortUtil.swap(data,j,j-inc);
- }
- }
- }
- }
- //快速排序:
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
- /**
- * @author treeroot
- * @since 2006-2-2
- * @version 1.0
- */
- public class QuickSort implements SortUtil.Sort{
- /** (non-Javadoc)
- * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
- */
- public void sort(int[] data) {
- quickSort(data,0,data.length-1);
- }
- private void quickSort(int[] data,int i,int j){
- int pivotIndex=(i+j)/2;
- //swap
- SortUtil.swap(data,pivotIndex,j);
- int k=partition(data,i-1,j,data[j]);
- SortUtil.swap(data,k,j);
- if((k-i)>1) quickSort(data,i,k-1);
- if((j-k)>1) quickSort(data,k+1,j);
- }
- /**
- * @param data
- * @param i
- * @param j
- * @return
- */
- private int partition(int[] data, int l, int r,int pivot) {
- do{
- while(data[++l]<pivot);
- while((r!=0)&&data[--r]>pivot);
- SortUtil.swap(data,l,r);
- }
- while(l<r);
- SortUtil.swap(data,l,r);
- return l;
- }
- }
- //改进后的快速排序:
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
- /**
- * @author treeroot
- * @since 2006-2-2
- * @version 1.0
- */
- public class ImprovedQuickSort implements SortUtil.Sort {
- private static int MAX_STACK_SIZE=4096;
- private static int THRESHOLD=10;
- /** (non-Javadoc)
- * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
- */
- public void sort(int[] data) {
- int[] stack=new int[MAX_STACK_SIZE];
- int top=-1;
- int pivot;
- int pivotIndex,l,r;
- stack[++top]=0;
- stack[++top]=data.length-1;
- while(top>0){
- int j=stack[top--];
- int i=stack[top--];
- pivotIndex=(i+j)/2;
- pivot=data[pivotIndex];
- SortUtil.swap(data,pivotIndex,j);
- //partition
- l=i-1;
- r=j;
- do{
- while(data[++l]<pivot);
- while((r!=0)&&(data[--r]>pivot));
- SortUtil.swap(data,l,r);
- }
- while(l<r);
- SortUtil.swap(data,l,r);
- SortUtil.swap(data,l,j);
- if((l-i)>THRESHOLD){
- stack[++top]=i;
- stack[++top]=l-1;
- }
- if((j-l)>THRESHOLD){
- stack[++top]=l+1;
- stack[++top]=j;
- }
- }
- //new InsertSort().sort(data);
- insertSort(data);
- }
- /**
- * @param data
- */
- private void insertSort(int[] data) {
- int temp;
- for(int i=1;i<data.length;i++){
- for(int j=i;(j>0)&&(data[j]<data[j-1]);j--){
- SortUtil.swap(data,j,j-1);
- }
- }
- }
- }
- //归并排序:
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
- /**
- * @author treeroot
- * @since 2006-2-2
- * @version 1.0
- */
- public class MergeSort implements SortUtil.Sort{
- /** (non-Javadoc)
- * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
- */
- public void sort(int[] data) {
- int[] temp=new int[data.length];
- mergeSort(data,temp,0,data.length-1);
- }
- private void mergeSort(int[] data,int[] temp,int l,int r){
- int mid=(l+r)/2;
- if(l==r) return ;
- mergeSort(data,temp,l,mid);
- mergeSort(data,temp,mid+1,r);
- for(int i=l;i<=r;i++){
- temp[i]=data[i];
- }
- int i1=l;
- int i2=mid+1;
- for(int cur=l;cur<=r;cur++){
- if(i1==mid+1)
- data[cur]=temp[i2++];
- else if(i2>r)
- data[cur]=temp[i1++];
- else if(temp[i1]<temp[i2])
- data[cur]=temp[i1++];
- else
- data[cur]=temp[i2++];
- }
- }
- }
- //改进后的归并排序:
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
- /**
- * @author treeroot
- * @since 2006-2-2
- * @version 1.0
- */
- public class ImprovedMergeSort implements SortUtil.Sort {
- private static final int THRESHOLD = 10;
- /**
- * (non-Javadoc)
- *
- * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
- */
- public void sort(int[] data) {
- int[] temp=new int[data.length];
- mergeSort(data,temp,0,data.length-1);
- }
- private void mergeSort(int[] data, int[] temp, int l, int r) {
- int i, j, k;
- int mid = (l + r) / 2;
- if (l == r)
- return;
- if ((mid - l) >= THRESHOLD)
- mergeSort(data, temp, l, mid);
- else
- insertSort(data, l, mid - l + 1);
- if ((r - mid) > THRESHOLD)
- mergeSort(data, temp, mid + 1, r);
- else
- insertSort(data, mid + 1, r - mid);
- for (i = l; i <= mid; i++) {
- temp[i] = data[i];
- }
- for (j = 1; j <= r - mid; j++) {
- temp[r - j + 1] = data[j + mid];
- }
- int a = temp[l];
- int b = temp[r];
- for (i = l, j = r, k = l; k <= r; k++) {
- if (a < b) {
- data[k] = temp[i++];
- a = temp[i];
- } else {
- data[k] = temp[j--];
- b = temp[j];
- }
- }
- }
- /**
- * @param data
- * @param l
- * @param i
- */
- private void insertSort(int[] data, int start, int len) {
- for(int i=start+1;i<start+len;i++){
- for(int j=i;(j>start) && data[j]<data[j-1];j--){
- SortUtil.swap(data,j,j-1);
- }
- }
- }
- }
- //堆排序:
- package org.rut.util.algorithm.support;
- import org.rut.util.algorithm.SortUtil;
- /**
- * @author treeroot
- * @since 2006-2-2
- * @version 1.0
- */
- public class HeapSort implements SortUtil.Sort{
- /** (non-Javadoc)
- * @see org.rut.util.algorithm.SortUtil.Sort#sort(int[])
- */
- public void sort(int[] data) {
- MaxHeap h=new MaxHeap();
- h.init(data);
- for(int i=0;i<data.length;i++)
- h.remove();
- System.arraycopy(h.queue,1,data,0,data.length);
- }
- private static class MaxHeap{
- void init(int[] data){
- this.queue=new int[data.length+1];
- for(int i=0;i<data.length;i++){
- queue[++size]=data[i];
- fixUp(size);
- }
- }
- private int size=0;
- private int[] queue;
- public int get() {
- return queue[1];
- }
- public void remove() {
- SortUtil.swap(queue,1,size--);
- fixDown(1);
- }
- //fixdown
- private void fixDown(int k) {
- int j;
- while ((j = k << 1) <= size) {
- if (j < size && queue[j]<queue[j+1])
- j++;
- if (queue[k]>queue[j]) //不用交换
- break;
- SortUtil.swap(queue,j,k);
- k = j;
- }
- }
- private void fixUp(int k) {
- while (k > 1) {
- int j = k >> 1;
- if (queue[j]>queue[k])
- break;
- SortUtil.swap(queue,j,k);
- k = j;
- }
- }
- }
- }
- //SortUtil:
- package org.rut.util.algorithm;
- import org.rut.util.algorithm.support.BubbleSort;
- import org.rut.util.algorithm.support.HeapSort;
- import org.rut.util.algorithm.support.ImprovedMergeSort;
- import org.rut.util.algorithm.support.ImprovedQuickSort;
- import org.rut.util.algorithm.support.InsertSort;
- import org.rut.util.algorithm.support.MergeSort;
- import org.rut.util.algorithm.support.QuickSort;
- import org.rut.util.algorithm.support.SelectionSort;
- import org.rut.util.algorithm.support.ShellSort;
- /**
- * @author treeroot
- * @since 2006-2-2
- * @version 1.0
- */
- public class SortUtil {
- public final static int INSERT = 1;
- public final static int BUBBLE = 2;
- public final static int SELECTION = 3;
- public final static int SHELL = 4;
- public final static int QUICK = 5;
- public final static int IMPROVED_QUICK = 6;
- public final static int MERGE = 7;
- public final static int IMPROVED_MERGE = 8;
- public final static int HEAP = 9;
- public static void sort(int[] data) {
- sort(data, IMPROVED_QUICK);
- }
- private static String[] name={
- "insert","bubble","selection","shell","quick","improved_quick","merge","improved_merge","heap"
- };
- private static Sort[] impl=new Sort[]{
- new InsertSort(),
- new BubbleSort(),
- new SelectionSort(),
- new ShellSort(),
- new QuickSort(),
- new ImprovedQuickSort(),
- new MergeSort(),
- new ImprovedMergeSort(),
- new HeapSort()
- };
- public static String toString(int algorithm){
- return name[algorithm-1];
- }
- public static void sort(int[] data, int algorithm) {
- impl[algorithm-1].sort(data);
- }
- public static interface Sort {
- public void sort(int[] data);
- }
- public static void swap(int[] data, int i, int j) {
- int temp = data[i];
- data[i] = data[j];
- data[j] = temp;
- }
- }
Java中各种排序算法的更多相关文章
- Java中的排序算法(2)
Java中的排序算法(2) * 快速排序 * 快速排序使用分治法(Divide and conquer)策略来把一个序列(list)分为两个子序列(sub-lists). * 步骤为: * 1. 从数 ...
- Java 中常见排序算法
经典的排序算法总结 冒泡排序算法 算法描述: 比较相邻的元素:如果第一个比第二个大,就交换它们两个: 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对,这样在最后的元素应该会是最大的数: 针 ...
- Java中的经典算法之选择排序(SelectionSort)
Java中的经典算法之选择排序(SelectionSort) 神话丿小王子的博客主页 a) 原理:每一趟从待排序的记录中选出最小的元素,顺序放在已排好序的序列最后,直到全部记录排序完毕.也就是:每一趟 ...
- Java中的经典算法之冒泡排序(Bubble Sort)
Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...
- Java中的查找算法之顺序查找(Sequential Search)
Java中的查找算法之顺序查找(Sequential Search) 神话丿小王子的博客主页 a) 原理:顺序查找就是按顺序从头到尾依次往下查找,找到数据,则提前结束查找,找不到便一直查找下去,直到数 ...
- Comparable与Comparator,java中的排序与比较
1:比较和排序的概念 比较:两个实体类之间按>,=,<进行比较. 排序:在集合类中,对集合类中的实体进行排序.排序基于的算法基于实体类提供的比较函数. 基本型别都提供了默认的比较算法,如s ...
- Java中的经典算法之快速排序(Quick Sort)
Java中的经典算法之快速排序(Quick Sort) 快速排序的思想 基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小, 然后再按此方法对 ...
- STL笔记(6)标准库:标准库中的排序算法
STL笔记(6)标准库:标准库中的排序算法 标准库:标准库中的排序算法The Standard Librarian: Sorting in the Standard Library Matthew A ...
- java实现各种排序算法
java实现各种排序算法 import java.util.Arrays; public class SomeSort { public static void main(String[] args) ...
随机推荐
- [Gauss]POJ1222 EXTENDED LIGHTS OUT
题意:给一个5*6的矩阵 1代表该位置的灯亮着, 0代表该位置的灯没亮 按某个位置的开关,可以同时改变 该位置 以及 该位置上方.下方.左方.右方, 共五个位置的灯的开.关(1->0, 0-&g ...
- HDOJ多校联合第四场
B题: C题:仅由'A','G','C','T',4个字母组成,给定一个字符串S,|S|<=15,给定一个整数m,以m为长度且仅含4种字母的字符串T,求LCS(S,T)为0,1,2,3....| ...
- Java二维数组
package com.test; public class Test { public static void main(String[] args) { // TODO Auto-generate ...
- MySQL 性能调优的10个方法
Mysql的优化方面,一般我们很少去考虑它,即使想到优化一般也更多是程序级别的,比如不要写过于消耗资源的SQL语句,但是除此以外,在整个系统上其实仍然有很多可以优化的地方. 1. 选择合适的存储引擎: ...
- 坑爹的libxml2 for mingw 编译 (二)
makefile 中由于大量使用了cmd /C ""样式去执行mkdir和copy操作,导致mingw最后出错,因为会从mingw切换至cmd界面.因此需要把相关代码进行修改. # ...
- poj3140Contestants Division
链接 这叫树形DP吗..?断开某条边 求剩下两颗树数权值和的差最小 dfs一遍 枚举边 查了n久 wa n次 dp数组没初始化.. 在poj上1A感觉应该挺爽 #include <iostre ...
- [Hadoop源码解读](三)MapReduce篇之Job类
下面,我们只涉及MapReduce 1,而不涉及YARN. 当我们在写MapReduce程序的时候,通常,在main函数里,我们会像下面这样做.建立一个Job对象,设置它的JobName,然后配置输入 ...
- 从头开始编写一个Orchard网上商店模块(6) - 创建购物车服务和控制器
原文地址: http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-pa ...
- C# 如何获取某个类型或类型实例对象的大小
在统计类型或类型实例对象时,出了个异常: “不能作为非托管结构进行封送处理;无法计算有意义的大小或偏移量.” 后来查了一下,原来,我们创建的struct或是class都是属于复杂类型的.(纠正一下,如 ...
- How to detect and avoid memory and resources leaks in .NET applications
By Fabrice Marguerie Despite what a lot of people believe, it's easy to introduce memory and resourc ...