Java常用的排序查找算法

public static void main(String[] args) {
      // bubbleSort();
  // int[] a = {20,2,10,8,12,17,4,25,11,6,33,13,24};
  // int start = 0;
  // int end = a.length-1;
  // sort(a,start,end);
  // for(int i = 0; i<a.length; i++){
  // System.out.print(a[i]+",");
  // }
  // choseSort();
  // nsertSort();
  // sequential();
  binarySearch();
}
/**冒泡排序*/
public static void bubbleSort(){
  int[] score = {20,2,10,8,12,17,4,25,11,6,33,13,24};
  //冒泡排序
  for (int i = 0; i < score.length -1; i++){ //最多做n-1趟排序
    for(int j = 0 ;j < score.length - i - 1; j++){
    //对当前无序区间score[0......length-i-1]进行排序
    //(j的范围很关键,这个范围是在逐步缩小的)
      if(score[j] > score[j + 1]){ //把小的值交换到后面
        int temp = score[j];
        score[j] = score[j + 1];
        score[j + 1] = temp;
      }
    }
  }
  System.out.print("最终排序结果:");
  for(int a = 0; a < score.length; a++){
    System.out.print(score[a] + ",");
  }
}
/**快速排序*/
public static void sort(int[] a,int low,int high){
  int start = low;
  int end = high;
  int key = a[low];

  while(end>start){
  //从后往前比较
    while(end>start&&a[end]>=key) //如果没有比关键值小的,比较下一个,直到有比关键值小的交换位置,然后又从前往后比较
      end--;
      if(a[end]<=key){
        int temp = a[end];
        a[end] = a[start];
        a[start] = temp;
      }
      //从前往后比较
      while(end>start&&a[start]<=key)//如果没有比关键值大的,比较下一个,直到有比关键值大的交换位置
        start++;
        if(a[start]>=key){
        int temp = a[start];
        a[start] = a[end];
        a[end] = temp;
      }
    //此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用
    }
    //递归
    if(start>low) sort(a,low,start-1);//左边序列。第一个索引位置到关键值索引-1
    if(end<high) sort(a,end+1,high);//右边序列。从关键值索引+1到最后一个
}
/**选择排序*/
public static void choseSort(){
  int[] toBeSorted = {20,2,10,8,12,17,4,25,11,6,33,13,24};

  for(int i = 0; i < toBeSorted.length; i++){
    for(int j = i+1; j < toBeSorted.length; j++){
      if(toBeSorted[i] > toBeSorted[j]){
        int temp = toBeSorted[i];
        toBeSorted[i] = toBeSorted[j];
        toBeSorted[j] = temp;
      }
    }
  }

  for(int i = 0; i <toBeSorted.length; i++){
  System.out.print(toBeSorted[i]+",");
  }

}
/**插入排序*/
public static void nsertSort(){
  int[] arr = {20,2,10,8,12,17,4,25,11,6,33,13,24};
  int i, j;
  int n = arr.length;
  int target;
  //假定第一个元素被放到了正确的位置上
  //这样,仅需遍历1 - n-1
  for (i = 1; i < n; i++){
    j = i;
    target = arr[i];
    while (j > 0 && target < arr[j - 1]){
      arr[j] = arr[j - 1];
      j--;
    }
    arr[j] = target;
  }
  for(int a = 0; a < arr.length; a++){
    System.out.print(arr[a] + ",");
  }
}

/**顺序查找*/
public static void sequential(){
  int[] arr = {20,2,10,8,12,17,4,25,11,6,33,13,24};
  int key = 6;
  for(int i=0;i<arr.length;i++){
    if(key == arr[i]){
      System.out.print("第"+(i+1)+"个");
    }
  }
}
/**折半查找*/
public static void binarySearch(){
  int[] arr = {20,2,10,8,12,17,4,25,11,6,33,13,24};
  int low = 0;
  int high = arr.length - 1;
  int key = 6;

  while ((low <= high) && (low <= arr.length - 1)
      && (high <= arr.length - 1)) {
    int middle = (high + low) >> 1;
    if (key == arr[middle]) {
      System.out.print("第"+(middle+1)+"个");
      break;
    } else if (key < arr[middle]) {
      high = middle - 1;
    } else {
      low = middle + 1;
    }
  }
}

posted @ 2017-02-08 18:48  我又不会乱来  阅读(1379)  评论(0编辑  收藏  举报