面试常用的4种数组排序

最近要离职了,正好项目做完,没什么事做就看看算法,数组的排序每次看完就忘记,这次记下来留着以后备用。

这里记录了4种排序:快速排序、冒泡排序、选择排序、插入排序,用java写的。

1、快速排序

写了2个小时才写出来,我了个去,又重新梳理了一下,大致的方法如下:

	/**
	 * 快速排序
	 * @param array
	 * @param low
	 * @param high
	 */
	public static void quickSort(int[] array,int low,int high){
		//数组为空时,不进行排序操作
		if(array == null || array.length == 0){
			return;
		}
		//如果low >= high,数据有误,不进行操作
		if(low >= high){
			return;
		}
		//取中值
		int middle = low + (high - low)/2;
		int prmt = array[middle];
		
		//排序使得数组left<prmt 同时,right>prmt;
		int i = low,j = high;
		while(i <= j){
			while(array[i] < prmt){
				i++;
			}
			while(array[j] > prmt){
				j--;
			}
			
			if(i <= j){
				int temp = array[i];
				array[i] = array[j];
				array[j] = temp;
				i++;
				j--;
			}
			System.out.print("排序中:");
			printArray(array);
		}
		//对左边部分进行处理
		if(low < j){
			quickSort(array,low,j);
		}
		//对右边的部分进行处理
		if(high > i){
			quickSort(array,i,high);
		}
	}

快速排序过程如下:

排序前:9 2 10 7 3 7 4  
排序中:4 2 10 7 3 7 9  
排序中:4 2 7 7 3 10 9  
排序中:4 2 7 3 7 10 9  
排序中:2 4 7 3 7 10 9  
排序中:2 4 3 7 7 10 9  
排序中:2 3 4 7 7 10 9  
排序中:2 3 4 7 7 9 10  
排序中:2 3 4 7 7 9 10  
排序后:2 3 4 7 7 9 10

2、冒泡排序

写成了一个方法,代码如下:

	/**
	 * 冒泡排序
	 * @param array
	 */
	public static void bubbleSort(int[] array){
		for(int i = 1;i<array.length; i++){
			System.out.println("排序序号" + i);
			for(int j = 0;j<array.length-i;j++){
				if(array[j]>array[j+1]){
					int temp = array[j];
					array[j] = array[j+1];
					array[j+1] = temp;
				}
				System.out.print("排序中:");
				printArray(array);
			}
		}
	}
冒泡排序过程如下:

排序前:9 2 10 7 3 7 4  
排序中:2 9 10 7 3 7 4  
排序中:2 9 10 7 3 7 4  
排序中:2 9 7 10 3 7 4  
排序中:2 9 7 3 10 7 4  
排序中:2 9 7 3 7 10 4  
排序中:2 9 7 3 7 4 10  
排序中:2 9 7 3 7 4 10  
排序中:2 7 9 3 7 4 10  
排序中:2 7 3 9 7 4 10  
排序中:2 7 3 7 9 4 10  
排序中:2 7 3 7 4 9 10  
排序中:2 7 3 7 4 9 10  
排序中:2 3 7 7 4 9 10  
排序中:2 3 7 7 4 9 10  
排序中:2 3 7 4 7 9 10  
排序中:2 3 7 4 7 9 10  
排序中:2 3 7 4 7 9 10  
排序中:2 3 4 7 7 9 10  
排序中:2 3 4 7 7 9 10  
排序中:2 3 4 7 7 9 10  
排序中:2 3 4 7 7 9 10  
排序后:2 3 4 7 7 9 10 



3、选择排序

直接上代码吧:

	/**
	 * 选择排序
	 * @param array
	 */
	public static void selectSort(int[] array){
		int min_index;
		for(int i = 0; i<array.length-1; i++){
			min_index = i;
			for(int j = i+1;j<array.length; j++){
				if(array[j] < array[min_index]){
					int temp = array[j];
					array[j] = array[min_index];
					array[min_index] = temp;
				}
				System.out.print("排序中:");
				printArray(array);
			}
		}
	}
选择排序过程如下:

排序前:9 2 10 7 3 7 4  
排序中:2 9 10 7 3 7 4  
排序中:2 9 10 7 3 7 4  
排序中:2 9 10 7 3 7 4  
排序中:2 9 10 7 3 7 4  
排序中:2 9 10 7 3 7 4  
排序中:2 9 10 7 3 7 4  
排序中:2 9 10 7 3 7 4  
排序中:2 7 10 9 3 7 4  
排序中:2 3 10 9 7 7 4  
排序中:2 3 10 9 7 7 4  
排序中:2 3 10 9 7 7 4  
排序中:2 3 9 10 7 7 4  
排序中:2 3 7 10 9 7 4  
排序中:2 3 7 10 9 7 4  
排序中:2 3 4 10 9 7 7  
排序中:2 3 4 9 10 7 7  
排序中:2 3 4 7 10 9 7  
排序中:2 3 4 7 10 9 7  
排序中:2 3 4 7 9 10 7  
排序中:2 3 4 7 7 10 9  
排序中:2 3 4 7 7 9 10  
排序后:2 3 4 7 7 9 10 

4、插入排序

继续上代码:

	/**
	 * 插入排序
	 * @param array
	 */
	public static void insertSort(int[] array){
		for(int i = 1;i<array.length; i++){
			int temp = array[i];
			int j = i-1;
			
			while(j >= 0 && array[j] > temp){
				array[j+1] = array[j];
				j--;
				System.out.print("排序中:");
				printArray(array);
			}
			array[j+1] = temp;
			
		}
	}
插入排序过程如下:

排序前:9 2 10 7 3 7 4  
排序中:9 9 10 7 3 7 4  
排序中:2 9 10 10 3 7 4  
排序中:2 9 9 10 3 7 4  
排序中:2 7 9 10 10 7 4  
排序中:2 7 9 9 10 7 4  
排序中:2 7 7 9 10 7 4  
排序中:2 3 7 9 10 10 4  
排序中:2 3 7 9 9 10 4  
排序中:2 3 7 7 9 10 10  
排序中:2 3 7 7 9 9 10  
排序中:2 3 7 7 7 9 10  
排序中:2 3 7 7 7 9 10  
排序后:2 3 4 7 7 9 10 
最后包含四种排序方法的代码也贴下吧:

public class SortUtil {

	public static void main(String[] args) {
		int[] x= {9,2,10,7,3,7,4};
		System.out.print("排序前:");
		printArray(x);
		
		int low = 0;
		int high = x.length-1;
		//快速排序
		quickSort(x,low,high);
		//冒泡排序
//		bubbleSort(x);
		//选择排序
//		selectSort(x);
		//插入排序
//		insertSort(x);
		
		System.out.print("排序后:");
		printArray(x);
	}
	
	/**
	 * 插入排序
	 * @param array
	 */
	public static void insertSort(int[] array){
		for(int i = 1;i<array.length; i++){
			int temp = array[i];
			int j = i-1;
			
			while(j >= 0 && array[j] > temp){
				array[j+1] = array[j];
				j--;
				System.out.print("排序中:");
				printArray(array);
			}
			array[j+1] = temp;
			
		}
	}
	
	/**
	 * 选择排序
	 * @param array
	 */
	public static void selectSort(int[] array){
		int min_index;
		for(int i = 0; i<array.length-1; i++){
			min_index = i;
			for(int j = i+1;j<array.length; j++){
				if(array[j] < array[min_index]){
					int temp = array[j];
					array[j] = array[min_index];
					array[min_index] = temp;
				}
				System.out.print("排序中:");
				printArray(array);
			}
		}
	}
	
	/**
	 * 冒泡排序
	 * @param array
	 */
	public static void bubbleSort(int[] array){
		for(int i = 1;i<array.length; i++){
			for(int j = 0;j<array.length-i;j++){
				if(array[j]>array[j+1]){
					int temp = array[j];
					array[j] = array[j+1];
					array[j+1] = temp;
				}
				System.out.print("排序中:");
				printArray(array);
			}
		}
	}
	
	/**
	 * 快速排序
	 * @param array
	 * @param low
	 * @param high
	 */
	public static void quickSort(int[] array,int low,int high){
		//数组为空时,不进行排序操作
		if(array == null || array.length == 0){
			return;
		}
		//如果low >= high,数据有误,不进行操作
		if(low >= high){
			return;
		}
		//取中值
		int middle = low + (high - low)/2;
		int prmt = array[middle];
		
		//排序使得数组left<prmt 同时,right>prmt;
		int i = low,j = high;
		while(i <= j){
			while(array[i] < prmt){
				i++;
			}
			while(array[j] > prmt){
				j--;
			}
			
			if(i <= j){
				int temp = array[i];
				array[i] = array[j];
				array[j] = temp;
				i++;
				j--;
			}
			System.out.print("排序中:");
			printArray(array);
		}
		//对左边部分进行处理
		if(low < j){
			quickSort(array,low,j);
		}
		//对右边的部分进行处理
		if(high > i){
			quickSort(array,i,high);
		}
	}
	
	
	
	/**
	 * 打印数组
	 * @param x 参数数组
	 */
	public static void printArray(int[] x){
		for(int a : x){
			System.out.print(a + " ");
		}
		System.out.println(" ");
	}

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值