冒泡、插入、选择、快速排序算法

冒泡排序:原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换,这样一趟过去后,最大或最小的数字被交换到了最后一位,然后再从头开始进行两两比较交换,直到倒数第二位时结束。

插入排序:它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入;假设第一个元素排好,之后的元素对排好的部分从后向前比较并逐一移动。

选择排序:从未排好的部分的第一个作为最小(最大)的,然后依次和剩余的比较,如果有更小(更大)的,记下下标,以此作为新的最小(最大)值,继续比较,一趟结束后,然后和第一个进行交换。

快速排序:快速排序采用了分而治之的思想,利用递归方法把大量数据划分成多分小量数据进行排序;而涉及到的基准关键字是关键。

基准关键字的选取,基准关键字的选取是决定快速排序算法的关键,常用的基准关键字的选取方式如下: 
第一种:三者取中。将序列首、尾和中间位置上的记录进行比较,选择三者中值作为基准关键字。 
第二种:取left和right之间的一个随机数这里写图片描述,用n[m]作为基准关键字。采用这种方法得到的快速排序一般称为随机的快速排序。

package com.kingcom.test;

/**
* @author CQling
* @version 2017年10月17日
*
*/
public class MianSort {

	public static int[] bubbleSort(int[] sort) {
		int len=sort.length;
		int[] temp=sort;
		for (int i = 0; i < len; i++) {
			for (int j = i+1; j < len; j++) {
				if (temp[i]<temp[j]) {
					int t = sort[i];
					temp[i] = temp[j];
					temp[j] = t;
				}
			}
		}

		return temp;
	}

	public static int[] insertSort(int[] sort) {
		int len=sort.length;
		int i,j,t;
		for (i = 1; i < len; i++) {
			t = sort[i];
			for (j = i-1; j >=0&&t > sort[j]; j--) {
				sort[j+1] = sort[j];
			}
			sort[j+1] = t;
		}

		return sort;
	}

	public static int[] selectSort(int[] sort) {
		int i,j,k,t;
		for (i = 0; i < sort.length; i++) {
			k=i;
			for (j = i+1; j < sort.length; j++) {
				if (sort[k]<sort[j]) {
					k=j;
				}
			}
			if (k!=i) {
				t=sort[i];
				sort[i]=sort[k];
				sort[k]=t;
			}
		}
		return sort;
	}

	public static int[] quickSort(int[] sort) {
		int low=0;
		int hight=sort.length-1;
		quickSort(low, hight, sort);
		return sort;
	}

	private static void quickSort(int low,int hight,int[] sort) {
		if (low>hight) {
			return;
		}
		int i=low,j=hight;
		int index = sort[i];
		while (i<j) {
			while (i<j&&index>=sort[j]) {
				j--;
			}
			if (i<j) {
				sort[i++]=sort[j];
			}
			while (i<j&&index<=sort[i]) {
				i++;
			}
			if (i<j) {
				sort[j--]=sort[i];
			}
		}
		sort[i]=index;
		quickSort(low, i-1, sort);
		quickSort(i+1, hight, sort);
	}

	public static void main(String[] args) {
		int[] s = {2,1,4,5,3};
		System.out.print("Sort Before:");
		for (int i = 0; i < s.length; i++) {
			System.out.print(s[i]+" ");
		}
		System.out.println("");
		System.out.print("InsertSort:");
		int[] t = insertSort(s);
		for (int i = 0; i < t.length; i++) {
			System.out.print(t[i]+" ");
		}
		System.out.println("");
		System.out.print("BubbleSort:");
		int[] t1 = bubbleSort(s);
		for (int i = 0; i < t1.length; i++) {
			System.out.print(t1[i]+" ");
		}
		System.out.println("");
		System.out.print("SelectSort:");
		int[] t2 = selectSort(s);
		for (int i = 0; i < t2.length; i++) {
			System.out.print(t2[i]+" ");
		}
		System.out.println("");
		System.out.print("QuickSort :");
		int[] t3 = quickSort(s);
		for (int i = 0; i < t3.length; i++) {
			System.out.print(t3[i]+" ");
		}

	}
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值