冒泡,快速,选择,插入排序

1,冒泡排序

/**
 * @Description:TODO
 * @project:project
 * @class:BubbleSort.java
 * @author:
 * @time:2015-7-14 上午11:08:44
 */
package sort;

/**
 * @deprecated: 冒泡排序
 * @author: 
 */
public class BubbleSort {
 public static void main(String[] args) {
  int score[] = { 67, 69, 75, 87, 89, 90, 99, 100 };
  for (int i = 0; i < score.length - 1; i++) { 
   for (int j = 0; j < score.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] + "  ");
  }
 }

}

2,插入排序

/**
 * @Description:TODO
 * @project:project
 * @class:InsertSort.java
 * @author:
 * @time:2015-7-14 下午02:56:32
 */
package sort;

/**
 * @deprecated: 插入排序
 * @author: 
 */
public class InsertSort {

 public static void main(String[] args) {
  int array[] = { 4, 12, 3, 1, 9 };
  insertSort(array);
  for (int i = 0; i < array.length; i++) {
   System.out.print(array[i]);
   if (i != array.length - 1) {
    System.out.print(",");
   }
  }
 }

 public static void insertSort(int[] array) {

  for (int i = 1; i < array.length; i++) {

   if (array[i] < array[i - 1]) {
    // 保存第i位的值
    int temp = array[i];
    int k = i - 1;

    for (int j = k; j >= 0 && temp < array[j]; j--) {
     array[j + 1] = array[j];
     k--;
    }

    array[k + 1] = temp;
   }
  }
 }

}

3,快速排序

/**
 * @Description:TODO
 * @project:project
 * @class:QuickSort.java
 * @author:
 * @time:2015-7-14 上午11:24:55
 */
package sort;

/**
 * @deprecated: 快速排序
 * @author: 
 */
public class QuickSort {

 public static void main(String[] args) {
  int score[] = { 100,69 , 67 , 75, 89, 87, 90, 99 };
  quick_sort(score,0,score.length-1);
  for (int a = 0; a < score.length; a++) {
   System.out.print(score[a] + "  ");
  }
 }
 
 // 快速排序
 public static void quick_sort(int s[], int l, int r) {
  if (l < r) {
   int i = l;
   int j = r;
   int x = s[l];
   while (i < j) {
    while (i < j && s[j] >= x)
     j--;
    if (i < j)
     s[i++] = s[j];

    while (i < j && s[i] < x)
     i++;
    if (i < j)
     s[j--] = s[i];
   }
   s[i] = x;
   quick_sort(s, l, i - 1); // 递归调用
   quick_sort(s, i + 1, r);
  }
 }
}

4,选择排序

/**
 * @Description:TODO
 * @project:project
 * @class:SelectionSort.java
 * @author:wangK
 * @time:2015-7-14 下午02:31:18
 */
package sort;

/**
 * @deprecated: 选择排序
 * @author: wangK
 */
public class SelectionSort {

 public static void main(String[] args) {
  int a[] = { 2, 13, 4, 1, 9 };
  sort(a);
  for (int i = 0; i < a.length; i++) {
   System.out.print(a[i] + "  ");
  }
 }

 public static void sort(int array[]) {
  int n = array.length;
  for (int i = 0; i < n - 1; i++) {
   int k = i;
   for (int j = i + 1; j < n; j++) {
    if (array[j] < array[k]) {
     k = j;
    }
   }
   int t = array[k];
   array[k] = array[i];
   array[i] = t;
  }
 }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值