排序算法

1、直接插入排序

//直接插入排序
//如果后一位数比前一位小,则把前一位往后移,再和更前一位比,如果还小,则继续往后移
public class A3 {
	public void insertSort(int[] a){
		int temp,i,j;
		for(i=1;i<a.length;i++){
			if(a[i]<a[i-1]){
				temp=a[i];
				for(j=i-1;j>=0&&temp<a[j];j--)
					a[j+1]=a[j];
				a[j+1]=temp;
			}
		}
	}
	public static void main(String[] args) {
		A3 j=new A3();
		int[] a = {9,7,6};
		j.insertSort(a);
		for(int s:a){
			System.out.print(s+" ");
		}
	}
}

2、简单选择排序

//简单选择排序
public class A4 {
	public void selectSort(int[] a) {
		int temp,k;
		for (int i = 0; i < a.length; i++) {
			k = i;
			for (int j = i + 1; j < a.length; j++)
				if (a[k] > a[j])
					k = j;
			if (k != i) {
				temp = a[i];
				a[i] = a[k];
				a[k] = temp;
			}
		}
	}

	public static void main(String[] args) {
		A4 j = new A4();
		int[] a = { 0, 7, 6, 9, 8 };
		j.selectSort(a);
		for (int s : a) {
			System.out.print(s + " ");
		}
	}
}

3、冒泡排序

//冒泡排序
public class A5 {
	public void bubbleSort(int[] a) {
		int m = a.length - 1, flag = 1, temp;
		while (m > 0 && flag == 1) {
			flag = 0;
			for (int i = 0; i < m; i++) {
				if (a[i] > a[i + 1]) {
					flag = 1;
					temp = a[i + 1];
					a[i + 1] = a[i];
					a[i] = temp;
				}
			}
			m--;
		}
	}

	public static void main(String[] args) {
		A5 j = new A5();
		int[] a = { 1, 6, 2, 9, 7, 5, 6 };
		j.bubbleSort(a);
		for (int s : a)
			System.out.print(s + ",");
	}
}

4、快速排序

//快速排序
public class A6 {
	public int Partition(int[] a,int low,int high){
		int temp=a[low];
		while(low<high){	
			while(low<high&&a[high]>=temp)
				high--;
			a[low]=a[high];
			while(low<high&&a[low]<=temp)
				low++;
			a[high]=a[low];
		}
		a[low]=temp;
		return low;
	}
	public void QSort(int[] a,int low,int high){
		if(low<high){
			int pivotloc=Partition(a,low,high);
			QSort(a,low,pivotloc-1);
			QSort(a,pivotloc+1,high);
		}
	}
	public static void main(String[] args) {
		A6 j=new A6();
		int[] a={1,3,5,9,2,4,5,8,9,7,6};
		j.QSort(a,0,a.length-1);
		for(int b:a)
			System.out.print(b+" ");
	}
}

5、归并排序

//归并排序
public class A7 {
	public static void main(String[] args) {
		A7 a7 = new A7();
		int[] R = { 3, 1, 4, 8, 6, 2, 5, 9};
		int[] T = new int[R.length];
		for(int i=0;i<R.length;i++)
			T[i]=R[i];
		a7.Msort(R, T, 0, R.length - 1);
		for (int b : R)
			System.out.print(b + " ");
		System.out.println();
		for (int b : T)
			System.out.print(b + " ");
	}

	private void Msort(int[] R, int[] T, int low, int high) {
		// TODO Auto-generated method stub
		if (low < high) {
			int mid = (low + high) / 2;
			Msort(T, R, low, mid);
			Msort(T, R, mid + 1, high);
			Merge(R, T, low, mid, high);
		}
	}

	private void Merge(int[] R, int[] T, int low, int mid, int high) {
		// TODO Auto-generated method stub	
		int i = low;
		int j = mid + 1;
		int k = low;
		while (i <= mid && j <= high) {
			if (T[i] <= T[j])
				R[k++] = T[i++];
			else
				R[k++] = T[j++];
		}
		while (i <= mid)
			R[k++] = T[i++];
		while (j <= high)
			R[k++] = T[j++];
	}
}

6、复杂度在这里插入图片描述
7、埃式筛法

public class A2 {
	public static void main(String[] args) {
		prime(100);
	}

	public static void prime(int n) {
		boolean[] shu = new boolean[n + 1];
		for (int i = 2; i <= Math.sqrt(n); i++) {
			if (!shu[i]) {
				for (int j = i * i; j <= n; j += i) {
					shu[j] = true;
				}
			}
		}
		for (int i = 2; i <= n; i++)
			if (!shu[i])
				System.out.print(i + " ");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值