关闭

Java排序算法和二分查找

标签: 算法排序二分查找
343人阅读 评论(0) 收藏 举报
分类:
package Bean;

public class Sort {

	static int nElement = 0;
	int[] arr;

	public Sort(int max) {
		arr = new int[max];
	}

	public void insert(int a) {
		arr[nElement] = a;
		nElement++;
	}

	// 冒泡排序
	public void bubbleSort() {
		int out, i;
		for (out = nElement - 1; out > 1; out--) {
			for (i = 0; i < out; i++) {
				if (arr[i] > arr[i + 1]) {
					swap(i, i + 1);
				}
			}

		}
	}

	// 选择排序
	public void selectSort() {
		int in, out;
		for (out = 0; out < nElement - 1; out++) {
			for (in = out + 1; in < nElement; in++) {
				if (arr[in] < arr[out]) {
					swap(in, out);
				}
			}
		}
	}

	// 插入排序
	public void insertSort() {
		for (int i = 1; i < nElement; i++) {
			int imterval = arr[i];
			int index = i - 1;

			while (index >= 0 && arr[index] > imterval) {
				arr[index + 1] = arr[index];
				index--;
			}
			arr[index + 1] = imterval;
		}
	}

	// 快速排序
	public void quickSort() {
		recQuickSort(0, nElement - 1);

	}

	public void recQuickSort(int left, int right) {
		int size = right - left +1;
		if (size <= 3) {
			manualSort(left, right);
		} else {
			int median = medianGet(left, right);
			int partition = partitionIt(left, right, median);
			recQuickSort(0, partition - 1);
			recQuickSort(partition, right);
		}
		
	}

	public int partitionIt(int left, int right, int median) {
		int leftPtr = left;
		int rightPtr = right - 1;
		while (true) {
			while (arr[++leftPtr] < median)
				;
			while (arr[--rightPtr] > median)
				;
			if (leftPtr >= rightPtr) {
				break;
			} else {
				swap(leftPtr, rightPtr);
			}
		}
		return leftPtr;
	}

	public void manualSort(int left, int right) {
		int size = right - left +1;
		if (size == 1)
			return;
		if (size == 2) {
			if (arr[left] > arr[right]) {
				swap(left, right);
			}
			return;
		} else {
			if (arr[left] > arr[right - 1]) {
				swap(left, right - 1);
			}
			if (arr[left] > arr[right]) {
				swap(left, right);
			}
			if (arr[right - 1] > arr[right]) {
				swap(right - 1, right);
			}
		}
	}

	public int medianGet(int left, int right) {
		int center = (left + right) / 2;
		if (arr[left] > arr[center]) {
			swap(left, center);
		}
		if (arr[left] > arr[right]) {
			swap(left, right);
		}
		if (arr[center] > arr[right]) {
			swap(center, right);
		}
		swap(center, right);
		return arr[right];
	}

	// 希尔排序
	public void shellSort(){
		int h = 1;
		int in, out;
		while(h <= nElement/3){
			h = h*3 + 1;
		}
		while(h > 0){
			for (out = h; out < nElement; out++){
				int temp = arr[out];
				in = out;
				while(in > h - 1 && arr[in - h] > temp){
					arr[in] = arr[in - h];
					in = in - h;
				}
				arr[in] = temp;
			}
			h = (h - 1)/3;
		}
	}
	
	// 二分查找
	public int binSearch(int start,int end, int key){
		int mid = (end - start) / 2 + start;
		if(arr[mid] == key)return mid;
		if(arr[mid] > key){
			return binSearch(start, mid, key);
		} else if(arr[mid] < key){
			return binSearch(mid, end, key);
		} 
		return 0;
	}
	
	
	// 汉诺塔
	public static void doTower(int nDisk,char from, char inter, char to){
		if(nDisk == 1){
			System.out.println("Disk 1 from " + from  + " to: " + to);
		}else {
			doTower(nDisk - 1, from, to, inter);
			System.out.println("Disk " + nDisk + " from " + from  + " to: " + to);
			doTower(nDisk - 1, inter, from, to);
		}
	}
	
	// 归并排序
	public void mergeSort(){
		int[] workArr = new int[nElement];
		recMergeSort(workArr,0, nElement - 1);
	}
	
	public void recMergeSort(int[] workArr, int low, int high){
		if(low == high){
			return;
		} else {
			int mid = (high + low)/2;
			recMergeSort(workArr, low, mid);
			recMergeSort(workArr, mid + 1 , high);
			merge(workArr, low, mid + 1, high);
		}
	}
	
	public void merge(int[] workArr, int low, int high, int upper){
		int mid = high - 1;
		int j = 0;
		int lowBound = low;
		int n = upper - lowBound + 1;
		 while(low <= mid && high <= upper){
			 if(arr[low] < arr[high]){
				 workArr[j++] = arr[low++];
			 } else{
				 workArr[j++] = arr[high++];
			 }
		 }
		 while(low <= mid){
			 workArr[j++] = arr[low++];
		 }
		 while(high <= upper){
			 workArr[j++] = arr[high++];
		 }
		 for(j = 0; j < n; j++){
			 arr[lowBound+j] = workArr[j];
		 }
	}
	
	public void swap(int i, int j) {
		int temp = arr[i];
		arr[i] = arr[j];
		arr[j] = temp;
	}

	public void displace() {
		for (int i = 0; i < nElement; i++) {
			System.out.print(arr[i] + " ");
		}
	}

	public static void main(String args[]) {
		Sort sort = new Sort(7);
		sort.insert(24);
		sort.insert(3);
		sort.insert(18);
		sort.insert(11);
		sort.insert(65);
		sort.insert(5);
		sort.insert(29);

		sort.mergeSort();
//		int src = sort.binSearch(0, nElement, 11);
//		System.out.println(src);
		sort.displace();
//		doTower(3, 'A', 'B', 'C');
	}
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:21318次
    • 积分:500
    • 等级:
    • 排名:千里之外
    • 原创:28篇
    • 转载:13篇
    • 译文:0篇
    • 评论:0条
    文章分类