java排序算法集

java中排序算法

java排序算法包括了很多种,包括了插入排序、选择排序、快速排序归并排序、桶排序、堆排序等等一系列的。


一、选择排序的递归与非递归实现

首先是非递归实现,代码如下。

	/**
	 * TODO:非递归选择排序算法(每次找出列表中最小元素或者最大元素放到当前列表的开始位置)
	 * @param noSortList 带排序的列表
	 * @return Integer[] 排好序的列表
	 * @author 邪恶小先生
	 * @time 2017年11月5日
	 */
	public static Integer [] selectionSort(Integer [] noSortList){
		for(int i = 0; i < noSortList.length - 1; i ++){
			//初始化最开始为最小
			int minElementIndex = i;
			for(int j = i + 1; j < noSortList.length; j ++){
				if(noSortList[j] < noSortList[minElementIndex]){
					minElementIndex = j;
				}
			}
			//交换元素
			if(!(minElementIndex == i)){
				int temp = noSortList[i];
				noSortList[i] = noSortList[minElementIndex];
				noSortList[minElementIndex] = temp;
			}
		}
		return noSortList;
	}
选择排序递归实现,代码如下。

	/**
	 * TODO:递归实现选择选择排序算法,每一次递归都将最小的移动到当前队列第一个元素的位置
	 * @return Integer[]
	 * @author 邪恶小先生
	 * @time 2017年11月9日
	 */
	public static Integer [] selectionSortWithRecursion(int nowStartIndex,Integer [] noSortList){
		if(nowStartIndex >= noSortList.length){
			return noSortList;
		}
		//初始化最小元素为nowStartIndex的地方的元素
		int minElementIndex = nowStartIndex;
		int i;
		//寻找最小元素下标
		for(i = nowStartIndex; i < noSortList.length; i ++){
			if(noSortList[i] < noSortList[minElementIndex]){
				minElementIndex = i;
			}
		}
		//如果下标有变动就执行交换代码块
		if(minElementIndex!=nowStartIndex){
			int temp = noSortList[nowStartIndex];
			noSortList[nowStartIndex] = noSortList[minElementIndex];
			noSortList[minElementIndex] = temp;
		}
		//递归进行
		return selectionSortWithRecursion(nowStartIndex+1,noSortList);
	}

二、插入排序

插入排序递归实现,代码如下。

	/**
	 * 
	 * TODO:非递归插入排序算法
	 * @return Integer[]
	 * @author 邪恶小先生
	 * @time 2017年11月9日
	 */
	public  static Integer [] insertSort(Integer [] noSortedList){
		for(int i = 1; i < noSortedList.length; i ++){
			int j = 0;
			//找插入的位置
			while(j < i){
				if(noSortedList[i] > noSortedList[j]){
					j++;
				}
				else{
					break;
				}
			}
			//当前被插入的元素所在的位置就正好是要插入,不需要变动
			if(!(j == i)){
				//将当前被插入的元素记录下来
				int temp = noSortedList[i];
				//移动后面元素的位置
				for(int m = i; m > j; m -- ){
					noSortedList[m] = noSortedList[m-1];
				}
				//进行插入
				noSortedList[j] = temp;
			}
		}
		return noSortedList;
	}

三、冒泡排序

	/**
	 * TODO:非递归方式的冒泡排序
	 * @param noSortList 带排序的列表
	 * @return Integer[] 排好序的列表
	 * @author 邪恶小先生
	 * @time 2017年11月5日
	 */
	public static Integer [] bubbleSort(Integer [] noSortList){
		//蛮力,just do it
		//外层循环用于控制已经排好序的元素不在进行排序
		for(int i = 0; i < noSortList.length; i ++){
			//标识当前列表是否已经有序,如果已经有序,就不用在进行排序
			boolean isInOrder = true;
			//后面排好序的就不用排序了,所以是noSortList.length - i
			for(int j = 1; j < noSortList.length - i - 1; j ++){
				if(noSortList[j] > noSortList[j+1]){
					//表明列表是无序的
					isInOrder = false;
					//swap两个元素
					int temp = noSortList[j];
					noSortList[j] = noSortList[j + 1];
					noSortList[j + 1] = temp;
				}
			}
			//表明列表是有序的,直接退出循环,避免下一次排序
			if(isInOrder){
				break;
			}
		}
		return noSortList;
	}


四、快速排序算法

package q.experience.exp2;

/**
 * @author 邪恶小先生
 */
public class QuickSort {
	/**
	 * TODO:主调算法
	 * @return void
	 * @author 邪恶小先生
	 * @time 2017年11月21日
	 */
	public static void quickSort(Integer [] noSortedList){
		quickSort(noSortedList,0,noSortedList.length - 1);
	}
	
	/**
	 * TODO:重载的方法
	 * @return void
	 * @author 邪恶小先生
	 * @time 2017年11月21日
	 */
	private static void quickSort(Integer [] partList,int first,int last){
		if(last > first){
			int pivotIndex = partition(partList, first, last);
			//分别对前后部分进行快速排序
			quickSort(partList,first,pivotIndex - 1);
			quickSort(partList,pivotIndex + 1, last);
		}
	}
	
	/**
	 * 
	 * TODO:主要的操作函数,进行元素的移动,将比主元小的元素往左边移动,比主元大的元素向右移动,返回主元正确的下标
	 * @return void
	 * @author 邪恶小先生
	 * @time 2017年11月21日
	 */
	public static int partition(Integer [] partList,int first, int last){
		//列表第一个元素作主元
		int pivot = partList[first];
		//从左向右扫描的下标开始处
		int low = first + 1;
		//从右向左扫描的下标开始处
		int high = last;
		
		//前后扫描整个列表,将比主元大的和小的分别隔开到两端
		while(high > low){
			//从左向右扫描第一个大于主元的元素,退出循环的时候要么是越过了界限,要么就是找到了第一个比主元大的元素
			while(low<=high && partList[low] <= pivot){
				//向右移动
				low ++;
			}
			
			//从右向左扫描第一个小于主元的元素,退出循环的时候要么是越过了界限,要么就是找到了第一个比主元大的元素
			while(low <= high && partList[high] > pivot){
				//向左移动
				high --;
			}
			
			//经过上面两个步骤,便找到了第一个比主元大的,和第一个比主元小的元素下标,或者越界
			//如果没有发生越界,交换两个元素
			if(high > low){
				int temp = partList[high];
				partList[high] = partList[low];
				partList[low] = temp;
			}
		}
		
		//现在确定主元pivot的位置
		while(high > first && partList[high] >= pivot){
			high --;
		}
		
		//插入主元,返回主元所在的位置
		if(pivot > partList[high]){
			partList[first] = partList[high];
			partList[high] = pivot;
			return high;
		}else{
			return first;
		}
	}
	
	public static void main(String [] args){
		Integer [] list = {2,3,2,5,6,1,-2,3,14,12};
		quickSort(list);
		System.out.println("排序完成");
		for(int i = 0; i < list.length; i ++){
			System.out.println(list[i]);
		}
	}
}


http://www.niftyadmin.cn/n/861753.html

相关文章

java扫描文件夹下面的所有文件(递归与非递归实现)

java中扫描指定文件夹下面的所有文件 扫描一个文件夹下面的所有文件&#xff0c;因为文件夹的层数没有限制可能多达几十层几百层&#xff0c;通常会采用两种方式来遍历指定文件夹下面的所有文件。递归方式非递归方式&#xff08;采用队列或者栈实现&#xff09; 下面我就给出两…

SpringMVC+Spring+Mybatis 整合教程(入手及用)

原文链接 http://www.jianshu.com/p/afc5446df498

web文件上传下载原理浅析

一、web文件上传浅析 现在有很多Web程序都有上传功能&#xff0c;实现上传功能的组件或框架也很多&#xff0c;如基于java的Commons FileUpload、还有Struts1.x和Struts2中带的上传文件功能&#xff08;实际上&#xff0c;Struts2在底层也使用了Commons FileUpload&#xff09;…

前后台交互中文乱码解决方式之一

设置tomcat的编码 在server.xml中寻找<Connector connectionTimeout"20000" port"8081" protocol"HTTP/1.1" redirectPort"8443"/>在后面加入 URIEncoding"utf-8"<Connector connectionTimeout"20000" p…

Ubuntu服务器搭建MySQL+Tomcat+JDK

话不多说&#xff0c;感谢博主&#xff0c;原文连接如下&#xff1a;Ubantu服务器搭建MySQL Tomcat JDK 注&#xff1a;博主里面很多命令没有使用sudo命令&#xff0c;比如解压tomcat&#xff0c;直接使用tar命令可能遇到权限不够的情况&#xff0c;这个时候需要在前面加上s…

浏览器的cookie,localStorage,sessionStorage区别

浏览器的cookie&#xff0c;localStorage,sessionStorage区别 localStorage,sessionStorage,cookies都是客户端存储的解决方案 1、localStorage和sessionStorage的声明周期 localStorage和sessionStorage都是用来存储客户端临时信息对象&#xff0c;他们只能存储字符串类型的…

javascript事件循环机制

文章出处https://zhuanlan.zhihu.com/p/26229293 函数调用栈与任务队列 Javascript有一个main thread 主进程和call-stack&#xff08;一个调用堆栈&#xff09;&#xff0c;在对一个调用堆栈中的task处理的时候&#xff0c;其他的都要等着。当在执行过程中遇到一些类似于setTi…

git命令集锦

1.安装篇 &#xff08;1&#xff09;windows上面使用git直接到官网下载git安装程序&#xff0c;完成后开始菜单中git->git bash如果能弹出&#xff0c;安装成功。 &#xff08;2&#xff09;自报家门&#xff0c;打开git bash&#xff0c;然后配置用户名和邮箱&#xff0…