Java实现常见排序算法

转载请注明出处:http://blog.csdn.net/jevonsCSDN/article/details/55254923【Jevons’Blog】

冒泡排序:

package sortingAlgorithms;

import java.util.Arrays;

/**
* <p>Title: BubbleSorting</p>
* <p>Description:冒泡排序 </p>
* <p>Company: </p> 
* @author   jevons
* @date         2017年2月15日 下午8:26:41
*/
public class BubbleSorting {
    public static void main(String[] args) {
        int[] data = { 7, 8, 9, 2, 1, 3, 4, 5, 6, 10 };// {12,45,6,7,68,99,13};
        int temp,counter=0;
        for (int i = 0; i < data.length-1; i++) {
            for (int j = 0; j < data.length-1-i; j++) {
                if(data[j]>data[j+1]){
                    temp= data[j+1];
                    data[j+1]=data[j];
                    data[j] = temp;
                }
                counter++;//计数
            }
        }
        System.out.println(Arrays.toString(data));
        System.out.println("比较次数为:"+counter);
    }
}

快速排序

package sortingAlgorithms;

import java.util.Arrays;
/**
* <p>Title: FastSorting_SimpleVersion</p>
* <p>Description: 快速排序算法</p>
* <p>Company: </p> 
* @author   jevons
* @date         2017年2月16日 下午1:24:37
*/
public class FastSorting_SimpleVersion {

    public static void main(String[] args) {
        int[] data = { 12, 44, 7, 66, 43, 33, 1, 22 };
        sort(data, 0, data.length - 1);
        System.out.println(Arrays.toString(data));
    }

    public static void sort(int[] array, int lo, int hi) {
        if (lo >= hi) {
            return;
        }
        int initLo=lo;
        int initHi=hi;
        int key = array[lo];
        while (lo < hi) {
            while (array[hi] >= key && hi > lo) {
                hi--;
            }
            array[lo] = array[hi];
            while (array[lo] <= key && lo < hi) {
                lo++;
            }
            array[hi] = array[lo];
        }
        array[lo]=key;
        //递归
        sort(array, initLo, hi-1);
        sort(array,hi+1,initHi);
    }
}

插入排序

package sortingAlgorithms;

import java.util.Arrays;

/**
* <p>Title: InsertionSorting</p>
* <p>Description: 插入排序,效率比冒泡排序高近一倍</p>
* <p>Company: </p> 
* @author   jevons
* @date         2017年2月16日 下午12:56:23
*/
public class InsertionSorting {
    public static void main(String[] args) {
        int[] data =  { 7, 8, 9, 2, 1, 3, 4, 5, 6, 10 };//{12,44,7,66,43,33,1,22};
        insertSort(data);
        System.out.println(Arrays.toString(data));
    }
    private static int[] insertSort(int[] data) {
        if (data == null || data.length < 2) {
            return data;
        }
        int counter =0;//计数器
        for (int i = 1; i < data.length; i++) {
            int temp = data[i];
            int j=i;
            while(j>0&&data[j-1]>temp){
                data[j]=data[j-1];
                j--;
                counter++;//计数
                System.out.println(Arrays.toString(data));
            }
            data[j]=temp;
        }
        System.out.println("比较次数为:"+counter);//本例数据用冒泡算法需比较45次,而插入排序只需19次
        return data;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值