首页 > 学院 > 开发设计 > 正文

排序之快速排序

2019-11-14 08:58:13
字体:
来源:转载
供稿:网友
package com.test.sort;/**  * <Description> <br>  *   * @author liu.mingqing<br> * @version 1.0<br> * @taskId <br> * @CreateDate 2017年2月4日 <br> * @since V7.3<br> * @see com.test.sort <br> */public class QuickSort {	/**	 * Description: <br> 	 *  	 * @author liu.mingqing<br>	 * @taskId <br>	 * @param args <br>	 */	public static void main(String[] args) {		Integer[] test = {305,65,7,90,120,110,8};		quickSort(test,0,6);		System.out.PRintln(test.toString());	}		public static <T extends Comparable<T>> void  quickSort(T[] data, int min, int max) {		if (min < max) {			int indexOfpartion = partion(data, min, max);			quickSort(data,min,indexOfpartion);			quickSort(data, indexOfpartion+1,max);		}	}	/**	 * 	 * Description: <br> 	 *  	 * @author liu.mingqing<br>	 * @taskId <br>	 * @param data	 * @param min	 * @param max <br>	 */	public static <T extends Comparable<T>> int partion(T[] data, int min, int max) {		int mid = (min+max)/2;		T partionElement = data[mid];		int first = min,last = max;		swap(data,min,mid);				while(first < last) {			while (first < last && data[first].compareTo(partionElement) <= 0) {				first++;			}			while (data[last].compareTo(partionElement) > 0) {				last--;			}			if (first < last) 				swap(data,first,last);		}				swap(data,min,last);		return last;	}		public static <T extends Comparable<T>> void swap(T[] data, int m, int n ) {		if (m >= data.length || n >= data.length)			return;		T temp = data[m];		data[m] = data[n];		data[n] = temp;	}}
上一篇:1041. 考试座位号(15)

下一篇:Spring

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表