Java面试宝典——选择排序+插入排序+冒泡排序+快速排序+希尔排序

package demos.order;
/** 
 * @author wyl
 * @time 2018年7月9日下午8:30:07
 * 选择排序:
 * 	对于给定的一组记录,经过第一轮比较后得到最小的记录,
 * 	然后将该记录与第一个记录的位置进行交换;
 * 	依次比较,直到进行比较的记录只有一个时为止。
 */
public class SelectOrder {

	public static void selectOrder(int[] a){
		int n=a.length;
		int tmp=0;
		int flag=0;
		for(int i=0;i<n;i++){
			tmp=a[i];
			flag=i;
			for(int j=i+1;j<n;j++){ //找到除第i个元素最小的元素
				if (a[j]<tmp) {
					tmp=a[j];
					flag=j;
				}
			}
			if (flag!=i) {
				a[flag]=a[i];
				a[i]=tmp;
			}
		}
	}
	public static void printArray(int[] a){
		for(int i=0;i<a.length;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println();
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int i=0;
		int a[]={5,4,9,8,7,6,0,1,3,2};
		selectOrder(a);
		printArray(a);
	}

}


package demos.order;
/** 
 * @author wyl
 * @time 2018年7月9日下午8:51:31
 * 插入排序:
 * 	对于给定的一组记录,初始时假设第一个记录自成一个有序序列,其余记录为无序序列。
 * 	接着从第二个记录开始,按照记录的大小依次将当前处理的记录插入到其之前的有序序列中,
 * 	直到最后一个记录插入到有序序列中为止。
 */
public class InsertSort {

	public static void insertOrder(int[] a ){
		if (a!=null) {
			for(int i=1;i<a.length;i++){
				int tmp=a[i];
				int j=i;
				if (a[j-1]>tmp) {
					while(j>=1&&a[j-1]>tmp){
						a[j]=a[j-1];
						j--;
					}
				}
				a[j]=tmp;
			}
		}
	}
	
	public static void printArray(int[] a){
		for(int i=0;i<a.length;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int[] a={7,3,19,40,4,7,1};
		insertOrder(a);
		printArray(a);
	}

}
package test.demos.order;
/** 
 * @author wyl
 * @time 2018年7月10日上午8:01:39
 */
public class InsertOrder {

	public static void insertOrder(int[] a){
		if (a!=null) {
			for(int i=0;i<a.length-1;i++){
				for(int j=i+1;j>0;j--){
					if (a[j]<a[j-1]) {
						int tmp=a[j];
						a[j]=a[j-1];
						a[j-1]=tmp;
					}
				}
			}
		}
	}
	
	public static void printArray(int[] a){
		for(int i=0;i<a.length;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] a={7,3,19,40,4,7,1};
		insertOrder(a);
		printArray(a);
	}

}
package test.demos.order;
/** 
 * @author wyl
 * @time 2018年7月10日上午8:22:45
 */
public class Select_Insert_Bubble_Quick_Shell {

	public static void selectSort(int[] a){
		int tmp=0;
		int flag=0;
		for(int i=0;i<a.length-1;i++){
			tmp=a[i];
			flag=i;
			for(int j=i+1;j<a.length;j++){
				if (a[j]<tmp) {
					tmp=a[j];
					flag=j;
				}
			}
			if (flag!=i) {
				a[flag]=a[i];
				a[i]=tmp;
			}
		}
	}
	
	public static void insertSort(int[] a){
		if (a!=null) {
			for(int i=0;i<a.length-1;i++){
				for(int j=i+1;j>0;--j ){
					if (a[j]<a[j-1]) {
						int tmp=a[j];
						a[j]=a[j-1];
						a[j-1]=tmp;
					}
				}
			}
		}
	}
	
	public static void bubbleSort(int[] a){
		for(int i=0;i<a.length-1;i++){
			for(int j=a.length-1;j>i;j--){
				if (a[j]<a[j-1]) {
					int tmp=a[j];
					a[j]=a[j-1];
					a[j-1]=tmp;
				}
			}
		}
	}
	
	public static void quickSort(int[] a,int low,int high){
		if (low>high) {
			return;
		}
		int p=partation(a,low,high);//分割点
		quickSort(a, low, p-1);
		quickSort(a, p+1, high);
	}
	
	public static void shellSort(int[] a){
		int i,j,h;
		int tmp;
		for(h=a.length/2;h>0;h/=2){
			for(i=h;i<a.length;i++){
				tmp=a[i];
				for(j=i-h;j>=0;j-=h){
					if (tmp<a[j]) {
						a[j+h]=a[j];
					}else {
						break;
					}
				}
				a[j+h]=tmp;
			}
		}
	}
	
	private static int partation(int[] a, int low, int high) {
		// TODO Auto-generated method stub
		int tmp=a[low];
		int i=low;
		int j=high+1;
		while(true){
			//从左向右扫描,查找比主元大的第一个元素a[i]
			while(a[++i]<tmp){
				if (i==high) {
					break;
				}
			}
			//从右向左扫描,查找比主元小的第一个元素a[j]
			while(a[--j]>tmp){
				if (j==low) {
					break;
				}
			}
			if (i>=j) {
				break;
			}
			//交换a[i] a[j] 
			int t=a[i];
			a[i]=a[j];
			a[j]=t;
		}
		//主元与分割点交换
		a[low]=a[j];
		a[j]=tmp;
		return j;
	}

	public static void printArray(int[] a){
		for(int i=0;i<a.length;i++){
			System.out.print(a[i]+" ");
		}
		System.out.println();
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] a={2,0,5,9,1,6,8,4,7};
		
		System.out.println("选择排序");
		selectSort(a);
		printArray(a);
		
		int[] a1={2,0,5,9,1,6,8,4,7};
		System.out.println("插入排序");
		insertSort(a1);
		printArray(a1);
		
		int[] a2={2,0,5,9,1,6,8,4,7};
		System.out.println("冒泡排序");
		bubbleSort(a2);
		printArray(a2);
		
		int[] a3={2,0,5,9,1,6,8,4,7};
		System.out.println("快速排序");
		quickSort(a3, 0, 8);
		printArray(a3);
		
		int[] a4={2,0,5,9,1,6,8,4,7};
		System.out.println("希尔排序");
		shellSort(a4);
		printArray(a4);
	}

}






猜你喜欢

转载自blog.csdn.net/u014067137/article/details/80977146