basicsorttool.java

来自「<算法导论>第二版大部分算法实现. 1. 各类排序和顺序统计学相关」· Java 代码 · 共 96 行

JAVA
96
字号
/* * Copyright (C) 2000-2007 Wang Pengcheng <wpc0000@gmail.com> * Licensed to the Wang Pengcheng under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The LGPL licenses this file to You under the GNU Lesser General Public * Licence, Version 2.0  (the "License"); you may not use this file except in * compliance with the License.  You may obtain a copy of the License at * *     http://www.gnu.org/licenses/lgpl.txt * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *///15 Oct 2007package cn.edu.whu.iss.algorithm.basictools;import java.util.Random;public final class BasicSortTool {		/**	 * For random a integer	 */	private static Random rand = new Random();		/**	 * Compare two objects.	 * @param o1	 * @param o2	 * @return a integer <0(o1<o2) =0(o1=o2) >0(o1>o2) 	 * 	 */	public static int compare(Object o1,Object o2){		if(o1==null&&o2!=null){			return -1;		}else if(o1!=null&&o2==null){			return 1;		}else{			return ((Comparable)o1).compareTo(o2);		}	}		/**	 * Get a integer between l and r	 * @param l	 * @param r	 * @return	 */	public static int randomInt(int l,int r){		if(r<l){			int t=r;			r=l;			l=t;		}		if(r==l){			return l;		}else{			return rand.nextInt(r-l)+l;		}	}		/**	 * Swap two elements in the array.And the elements is the i and j.	 * @param o element array	 * @param i one element	 * @param j other one 	 */	public static void swap(Object[] o,int i,int j){		Object temp = o[i];		o[i] = o[j];		o[j] = temp;	}		/**	 * Get the max value in the elements array.	 * @param element array	 * @return the max value	 */	public static Object max(Object[] element){		if( (element==null)||(element.length==0)){			return null;		}		int k=0;		for(int i=1;i<element.length;i++){			if(compare(element[i], element[k])>0){				k=i;			}		}		return element[k];	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?