Java排序算法和二分查找
2013-12-03 21:14
204 查看
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'); } }
相关文章推荐
- Light oj Trailing Zeroes (III) (二分查找)
- 搜索算法-顺序查找和二分查找
- php二分查找的两种实现方法
- 二分查找、二叉搜索
- 基于数组二分查找算法的实现
- 二分查找
- 一个可能存在Bug的二分查找方法的实现,大家一起看看有什么问题没
- 二分排序与二分查找
- 二分查找详解
- 二分查找思想寻找有序数组中查找最小值
- 二分查找代码
- [二分查找]Babelfish uva 10282
- 二分查找 hdu1969 pie
- Java数组与二分查找
- 二分查找 基于有序数组 返回下标
- 二分查找的改进 差值查找(low<=high,这与快排low<high)
- 顺序查找与二分查找
- 简单二分查找
- 二分查找(迭代和递归)
- 五大常用算法(一) 分治算法(1) 二分查找