1. 程式人生 > >各種排序演算法實現及各自的特點

各種排序演算法實現及各自的特點

插入排序:

1. 直接插入排序

/*
 * 直接插入排序
 * coded by Jerome
 */
	public static void Direct_Insert(int[] a){
		int temp = 0;
		for(int i = 1;i<a.length;i++){
			if(a[i]<a[i-1]){
				temp = a[i];
				int j = i;
				while(j>=1&&temp<a[j-1]){
					a[j] = a[j-1];
					j--;
				}
				a[j] = temp;
			}
		}
	}

2. 折半插入排序

/*
 * 折半插入排序
 * coded by Jerome
 */
	public static void Binary_Insert(int[]a){
		int  temp = 0;
		int j =0;
		for(int i = 1;i<a.length;i++){
			temp = a[i];
			int low = 0;
			int high = i-1;
			while(low <= high){
				int mid = (high+low)/2;
				if(temp >= a[mid])
					low = mid + 1;
				else
					high = mid - 1;
			}
			for(j = i;j>high+1;j--)
				a[j] = a[j-1];
			a[j] = temp;
		}
	}

3. 希爾排序

/*希爾排序
 * coded by Jerome
 */
	public static void Shell_Sort(int[] a){
		int length = a.length;
		int i;
		int j;
		int h;//定義步長
		for(h=length/2;h>=1;h/=2){
			for(i=h;i<length;i++){
				if(a[i]<a[i-h]){
					int temp = a[i];
					j = i;
					while(j-h>=0&&a[j-h]>temp){
						a[j] = a[j-h];
						j = j-h;
					}
					a[j] = temp;
			}
				else
					continue;
		}
	}
}

交換排序:

1. 氣泡排序

/*
 * 氣泡排序原始 演算法
 * coded by Jerome
 */
	public static void Bubble_Sort(int[] a){
		int temp = 0;
		for(int i = a.length-1;i>0;i--){
			for(int j = 0;j<i;j++){
				if(a[j]>a[j+1]){
					temp = a[j+1];
					a[j+1] = a[j];
					a[j] = temp;
				}
				else
					continue;
				
			}
		}
	}

對於氣泡排序,若一趟排序結束,沒有資料產生交換,則不需要再繼續排序,說明已經是有序的,這時應該退出迴圈,結束排序過程,因此引出氣泡排序的改進版

2. 氣泡排序改進版

/*
 * 氣泡排序演算法的優化
 * 若在一趟排序中沒有發生元素之間的交換
 * 說明元素已經有序,就退出排序
 * coded by Jerome
 */
	public static void Bubble_Sort(int[] a){
		int temp = 0;
		for(int i = a.length-1;i>0;i--){
			boolean flag = true;
			for(int j = 0;j<i;j++){
				if(a[j]>a[j+1]){
					temp = a[j+1];
					a[j+1] = a[j];
					a[j] = temp;
					flag = false;
				}
				else
					continue;
			}
			if(flag)
				break;
			else
				continue;
		}
	}

3. 快速排序

/*
 * 快速排序
 * coded by Jerome
 */
	public static void Quick_Sort(int[] a,int low,int high){
		if(low<high){
			int pivotIndex = partion(a,low,high);
			Quick_Sort(a,low,pivotIndex-1);
			Quick_Sort(a,pivotIndex+1,high);
		}
	}
	public static int partion(int[] a,int low,int high){
		int pivot = a[low];
		int i = low;
		while(low<high){
			while(low<high&&a[high]>=pivot)
				high--;
			while(low<high&&a[low]<=pivot)
				low++;
			if(low<high){
				int temp = a[high];
				a[high] = a[low];
				a[low] = temp;
			}
		}
		a[i] = a[low];
		a[low] = pivot;
		return low;
	}

選擇排序:

1. 直接選擇排序

/*直接選擇排序
 * coded by Jerome 
 */
	public static void Select_Sort(int[] a){
		int min = 0;
		int flag = 0;
		for(int i = 0;i<a.length-1;i++){
			min = a[i];
			flag = i;
			for(int j = i+1;j<a.length;j++){
				if(a[j]<min){
					min = a[j];
					flag = j;
				}
				else
					continue;
			}
			a[flag] = a[i];
			a[i] = min;
		}
}

2. 堆排序

/*堆排序
 * coded by Jerome
 */
	public static void Heap_Sort(int[] a){
		buildHeap(a);
		for(int i = a.length-1;i>0;i--){
			swap(a,0,i);
			adjustDown(a,0,i);
		}
	}
	public static void swap(int[] a,int j ,int i){
		int temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}
	public static void buildHeap(int[] a){
		int len = a.length;
		for(int i =(len-1)/2;i>=0;i--){
			adjustDown(a, i, len);
		}
	}
	public static void adjustDown(int[] a,int i,int len){
		int temp = a[i];
		for(int j = 2*i+1;j<len;j=2*j+1){
			if(j+1<len&&a[j]<a[j+1])
				j++;
			if(a[j]<temp)
				break;
			else{
				a[i] = a[j];
				i = j;
				/*把j的值賦值給i,方便交換資料後,判斷下面的堆是否破壞,
				 * 一旦破壞,這樣設定才能進行下一輪值的交換
				 */
			}
				
		}
		a[i] = temp;
	}

歸併排序:

/*
 *歸併排序 
 *coded by Jerome
 */
	public static void merge(int[] a,int low,int mid,int high){
		int[] temp = new int[high-low+1];//定義輔助陣列
		int k = 0;
		int left = low;
		int right = mid+1;
		while(left<=mid&&right<=high){
			if(a[left]<a[right])
				temp[k++] = a[left++];
			else
				temp[k++] = a[right++];
		}
		while(left<=mid)
			temp[k++] = a[left++];
		while(right<=high)
			temp[k++] = a[right++];
		for(int i = 0;i<temp.length;i++)
			a[low+i] = temp[i];
		
	}
	public static void Merge_Sort(int[] a,int low,int high){
		
		if(low<high){
			int mid = (high+low)/2;
			Merge_Sort(a,low,mid);
			Merge_Sort(a,mid+1,high);
			merge(a,low,mid,high);
		}
		
	}

各種排序演算法之間的比較及適用的場景: