`

序_排

 
阅读更多
/**
* 冒泡
*/
public static void get() {
  int[] index = new int[] { 1, 9, 2, 8, 3, 6, 7, 4, 5, 5 };
  int a = 0;
  for (int i = 0; i < index.length; i++) {
    for (int j = 0; j < index.length - i - 1; j++) {
      if (index[j] > index[j + 1]) {
        a = index[j + 1];
        index[j + 1] = index[j];
        index[j] = a;
       }
    }
  }
}

/**
* 插入
*/
public static void get() {
  int[] index = new int[] { 29, 52, 3, 4, 8, 6, 7, 1, 78 };
  int a = 0;
  for (int i = 1; i < index.length; i++) {
    for (int j = 0; j < i; j++) {
      if (index[j] > index[i]) {
      a = index[j];
      index[j] = index[i];
      index[i] = a;
      }
    }
  }
}

/**
* 选择
*/
public static void get() {
  int[] index = new int[] { 29, 52, 3, 4, 8, 6, 7, 1, 78 };
  int a;
  for (int i = 1; i < index.length; i++) {
    a = 0;
    for (int j = 1; j < index.length - i; j++) {
       if (index[j] > index[a]) {
         a = j;
       }
    }
    int b = index.length - i;
    int c = index[b];
    index[b] = index[a];
    index[a] = c;
  }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics