排序 算法

一,冒泡排序算法

1.1:算法思路
第一步: 比较相邻的两个元素:如果第一个比第二个大,就交换它们;
第二步: 对每两个相邻元素进行比较,从开始的两个到最后的两个;

2.2:复杂度

O(n) ~O(n^2);

例题:输入10个数据,放进一个数组,进行冒泡排序后输出。

    public static void main(String[] args) 
        {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int[] num = new int[5];
        System.out.println("请输入5个数:");
        for( int i = 0 ; i < num.length ; i++)
        {
            System.out.print("请输入第"+(i+1)+"个数:");
            num[i] = in.nextInt();
        }
        for(int i = 0; i < num.length - 1 ; i++)
        {
            for(int j = 0; j < num.length - 1 - i; j++)
            {
                if(num[j] > num[j+1]);
                int temp;
                temp = num[j+1];
                num[j+1] = num[j];
                num[j] = temp;
            }
        } 
        for(int x:num)
        System.out.print(x+"\t");

2,选择排序算法

2.1:每次循环选出一个未排序中的最小的进行排序

2.2:复杂度 

始终为O(n^2);

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] num = {6,5,4,3,2,1};
		for(int i = 0 ; i < num.length -1 ; i++)
		{
			boolean V = false;
			for(int j = i+1 ; j < num.length ; j++)
			{
				if(num[i] > num[j])
				{
					V = true;
				}
				if(V)
				{
					int temp;
					temp = num[j];
					num[j] = num[i];
					num[i] = temp;
				}
			}
		}
		for(int k : num)
		System.out.print(k+" ");

3,插入排序算法

3.1:每次选定一个元素插入到前边已排序好的元素中

3.2:复杂度

O(n)~O(n^2);

public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] num = {6,5,4,3,2,1};
		int j , i;
		for( i = 1 ; i < num.length  ; i++)
		{
			int temp = num[i];
			for( j = i - 1 ; j >= 0 ; j--)
			{
				if(temp < num[j])
				{
					num[j + 1] = num [j];
				}
				else
				{
					break;
				}	
			}
			num[j+1] = temp;
		}
		for(int k : num)
		System.out.print(k+" ");
	}

快速排序:每次选一个中间值 一次递归排序;

	public static void quickly(int [] arr,int beginIndex , int endIndex) {
		int i = beginIndex;
		int j = endIndex;
		int temp = arr[i];
		if(i >= j) return;
		while(i != j) {
			while(arr[j] >= temp && i < j) {
				j--;
			}
			while(arr[i] <= temp && i < j) {
				i++;
			}
			int num = arr[i];
			arr[i] = arr[j];
			arr[j] = num;
		}
		arr[beginIndex] = arr[i];
		arr[i] = temp;
		quickly(arr, beginIndex, i-1);
		quickly(arr, i+1, endIndex);
	}

二分查找法:查找某个数在已排序好的一个数组中的索引

	public static int two(int [] arr , int number) {
		int min = 0 ; 
		int max = arr.length - 1;
		int mid = ( min + max ) / 2;
//		int count = 1;
		while(true){
			if( number  < arr[mid]) {
				max = mid - 1;
				mid = ( min + max ) / 2;
//				count++;
				continue;
			}
			else if(number > arr[mid]) {
				min = mid + 1;
				mid = ( min + max ) / 2;
//				count++;
				continue;				
			}
			else {
				break;
			}
		}
		return mid;
	}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值