笔记-八大排序算法-java

1.冒泡排序

public class A {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] array = {3,1,5,4,6,1,9,7,2,4};
		//before
		System.out.print("before: ");
		printArray(array);
		//sort
		bubbleSort(array);
		//after
		System.out.print(" after: ");
		printArray(array);
	}
	
	public static void bubbleSort(int[] array){
	    for(int i=0; i<array.length-1; i++){	
	        for(int j=0; j<array.length-1-i; j++){
	            if(array[j]>array[j+1]){
	                int temp = array[j];
	                array[j] = array[j+1];
	                array[j+1] = temp;
	            }
	        }
	        System.out.print(i+1 + "round: ");
	        printArray(array);
	    }
	}
	
	public static void printArray(int[] array) {
		for(int i : array) {
			System.out.print(i + " ");
		}
		System.out.println();
	}
}

排序过程和结果:


2.快速排序

public class A {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] array = {3,1,5,4,6,1,9,7,2,4};
		//before
		System.out.print("before: ");
		printArray(array);
		//sort
		quickSort(array, 0, array.length-1);
		//after
		System.out.print(" after: ");
		printArray(array);
	}
	
	public static void quickSort(int[] array, int left, int right){
		if(left>right) return;
		int i, j;
		i = left;
		j = right;
		int pivot = array[left];
		while(i<j) {
			while(i<j && pivot < array[j]) j--;
			while(i<j && pivot >= array[i]) i++;//相等的话i也要向后移动
			if(i<j) {
				int temp = array[j];
				array[j] = array[i];
				array[i] = temp;
			}
		}
		array[left] = array[i];
		array[i] = pivot;
		printArray(array);
		 
		quickSort(array, left, i-1);
		quickSort(array, i+1, right);
	}
	
	public static void printArray(int[] array) {
		for(int i : array) {
			System.out.print(i + " ");
		}
		System.out.println();
	}
}

排序过程和结果:

3. 直接插入

public class A {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] array = {3,1,5,4,6,1,9,7,2,4};
		//before
		System.out.print("before: ");
		printArray(array);
		//sort
		insertSort(array);
		//after
		System.out.print(" after: ");
		printArray(array);
	}
	
	public static void insertSort(int[] array){
		if(array.length <= 1) return;
	    for(int i=0; i<array.length-1; i++) {
	    	for(int j=i+1; j>0; j--) {
	    		if(array[j-1] <= array[j])
	    			break;
	    		int temp = array[j];
	    		array[j] = array[j-1];
	    		array[j-1] = temp;
	    	}
	    	 printArray(array);
	    }
	}
	
	public static void printArray(int[] array) {
		for(int i : array) {
			System.out.print(i + " ");
		}
		System.out.println();
	}
}

排序过程和结果:


4.shell排序

这篇文章解释地很清楚:https://www.cnblogs.com/jsgnadsj/p/3458054.html

5. 选择排序

public class A {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int [] array = {3,1,5,4,6,1,9,7,2,4};
		//before
		System.out.print("before: ");
		printArray(array);
		//sort
		selectSort(array);
		//after
		System.out.print(" after: ");
		printArray(array);
	}
	
	public static void selectSort(int[] array) {
		for(int i=0; i<array.length-1; i++) {
			int minIndex = i;
			for(int j=i+1; j<array.length; j++) {
				if(array[j] < array[minIndex]) {
					minIndex = j;
				}
			}
			if(minIndex == i) continue;
			int temp = array[minIndex];
			array[minIndex] = array[i];
			array[i] = temp;
			printArray(array);
		}
	}
	
	public static void printArray(int[] array) {
		for(int i : array) {
			System.out.print(i + " ");
		}
		System.out.println();
	}
}

排序过程和结果:


6.堆排序

https://www.cnblogs.com/chengxiao/p/6129630.html

7.归并排序

https://www.cnblogs.com/chengxiao/p/6194356.html

8.基数排序

https://www.cnblogs.com/skywang12345/p/3603669.html








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值