package cn.by.Collecion.home0820;

import java.util.Comparator;
import java.util.List;

/**
 *
 *排序类
 */
public class Sort {
 
 
 /**
  * 根据元素的自然顺序 对指定列表按升序进行排序。列表中的所有元素都必须实现 Comparable 接口.
  *
  * @param list  要排序的列表
  */
 public static <T extends Comparable<? super T>> void sort(List<T> list) {
  /**
   *主要用于提前结束比较,即如果一趟中没有一个元素交换, 则后面还没有进行的趟也不用进行了。
   */
  int exchange;
  // 临时交换变量
  T temp;

  int length = list.size();

  for (int i = 0; i < length - 1; i++) {
   exchange = 0; // 初始值为0
   for (int j = length - 1; j > i; j--) {

    T after = list.get(j);
    T front = list.get(j - 1);

    if (after.compareTo(front) < 0) {

     temp = list.get(j);
     list.set(j, front);
     list.set(j - 1, temp);
     exchange = 1; // 如有交换则更改为1
    }
   }
   // 提前结束
   if (exchange == 0)
    return;
  }

 }
 
 /**
  * 根据指定比较器产生的顺序对指定列表进行排序。
  *
  * @param list  要排序的列表
  * @param c     确定列表顺序的比较器。null 值指示应该使用元素的自然顺序。
  */
 public static <T> void sort(List<T> list, Comparator<? super T> c) {  

  /**
   *主要用于提前结束比较,即如果一趟中没有一个元素交换, 则后面还没有进行的趟也不用进行了。
   */
  int exchange;

  // 临时交换变量
  T temp;
  if (c != null) {//比较器不为空
   int result;
   for (int i = 0; i < list.size() - 1; i++) {
    exchange = 0; // 初始值为0
    for (int j = list.size() - 1; j > i; j--) {
     T after = list.get(j);
     T front = list.get(j - 1);
     result = c.compare(after, front);
     // 比较
     if (result < 0) {
      temp = list.get(j);
      list.set(j, front);
      list.set(j - 1, temp);
      exchange = 1; // 如有交换则更改为1
     }

    }
    // 提前结束
    if (exchange == 0)
     return;
   }
  }
  
  
  
  //比较器为空时,应该使用元素的自然顺序
  int length = list.size();
  for (int i = 0; i < length - 1; i++) {
   exchange = 0; // 初始值为0
   for (int j = length - 1; j > i; j--) {

    T after = list.get(j);
    T front = list.get(j - 1);

    Comparable com = (Comparable) after;

    if (com.compareTo(front) < 0) {

     temp = list.get(j);
     list.set(j, front);
     list.set(j - 1, temp);
     exchange = 1; // 如有交换则更改为1
    }
   }
   // 提前结束
   if (exchange == 0)
    return;
  }

 }
 
 
 
 

}

 

 

 

 

 

 

 

 

package cn.by.Collecion.home0820;

import java.util.Comparator;
import java.util.List;

public class BinarySearch {
 
 
 
 /**
  * 使用二分搜索法搜索指定列表,以获得指定对象。
  * @param list   要搜索的列表
  * @param key    要搜索的键
  * @return       如果搜索键包含在列表中,则返回搜索键的索引,否则
  * 返回 (-(插入点) - 1)。插入点 被定义为将  * 键插入列表的那一点:
  * 即第一个大于此键的元素索引;如果列表中的所有元素 都小于指定的键,
  * 则为 list.size()。注意,这保证了当且仅当此键被找到时,返回的值将 >= 0。
  */
 public static <T> int binarySearch(List<? extends Comparable<? super T>> list, T key) {
  int low = 1;
  int high = list.size();
  int mid;
  int result;
  while (low <= high) {
   mid = (low + high) / 2;           
   //第一个比较元素为list.get(mid - 1),注意if判断时的符号
   result = list.get(mid - 1).compareTo(key);
   
   if (result == 0) {
    return mid;
   } else if (result > 0) {
    high = mid - 1;
   } else {
    low = mid + 1;
   }

  }
  // 没找到
  return -low;

 }
 
 
 
 
 /**
  * 使用二分搜索法搜索指定列表,以获得指定对象。
  * @param list  要搜索的列表
  * @param key   要搜索的键
  * @param c     排序列表的比较器。null 值指示应该使用元素的自然顺序。
  * @return      如果搜索键包含在列表中,则返回搜索键的索引,否则返回
  * (-(插入点) - 1)。插入点 被定义为将 键插入列表的那一点:即第一个大于
  * 此键的元素索引;如果列表中的所有元素 都小于指定的键,则为 list.size()。
  * 注意,这保证了当且仅当此键被找到时, 返回的值将 >= 0。
  */ 
 public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) {
  int low = 1;
  int high = list.size();
  int mid;
  int result;
  
        if( c != null) {//比较器不为空
  while (low <= high) {

   mid = (low + high) / 2;
   // System.out.println("mid="+mid);
   //第一个元素为key,注意if判断时的符号
   result = c.compare(key, list.get(mid - 1));
   if (result == 0)
    return mid;
   else if (result < 0) {
    high = mid - 1;
    // System.out.println("high="+high);
   } else {
    low = mid + 1;
    // System.out.println("low="+low);
   }

  }
  // 没找到
  return -low;
        }
       
        //比较器为空时,使用元素的自然顺序比较
        while (low <= high) {
   mid = (low + high) / 2;           
   //第一个比较元素为list.get(mid - 1)
   Comparable com = (Comparable)list.get(mid - 1);
   
   result = com.compareTo(key);
   
   if (result == 0) {
    return mid;
   } else if (result > 0) {
    high = mid - 1;
   } else {
    low = mid + 1;
   }

  }
  // 没找到
  return -low;
       
 }
 

}