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]);
		}
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章