面试常用的4种数组排序
2014-04-15 15:17
197 查看
最近要离职了,正好项目做完,没什么事做就看看算法,数组的排序每次看完就忘记,这次记下来留着以后备用。
这里记录了4种排序:快速排序、冒泡排序、选择排序、插入排序,用java写的。
1、快速排序
写了2个小时才写出来,我了个去,又重新梳理了一下,大致的方法如下:
快速排序过程如下:
2、冒泡排序
写成了一个方法,代码如下:
3、选择排序
直接上代码吧:
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(" "); } }
相关文章推荐
- 面试常用的4种数组排序
- 剑指offer面试题目:数字在排序数组中出现的次数
- 数组常用的三种排序
- Java基础学习应用_数组常用方法与排序(二)
- 笔试面试算法经典-未排序正整数数组中累加和为给定值的子数组
- 黑马程序员——Java语言基础——02.java语言基础组成(4)数组及常用排序方法
- js数组去重并且升序排序常用方法总结
- 【LeetCode-面试算法经典-Java实现】【004-Median of Two Sorted Arrays(两个排序数组的中位数)】
- PHP数组排序常用函数的区别一谈
- 数组常用的排序
- 黑马程序员_Java基础_函数,数组,常用排序
- Objective-C中数组对象排序的方法(OC中NSString的常用API的基础应用2)
- java 运用数组常用的几种排序方法
- 数组常见常用的几种排序→→→冒泡→→选择→→插入
- 常用算法_数组排序Test1
- Java常用类库——Arrays类(用于普通数组操作)、比较器(Comparable、Comparator 用于对象排序)的使用
- Java数组冒泡,选择,插入三种常用排序法及自带的快速排序法
- ArrayTool是一个定义了操作数组常用的工具类,比如求最大值,排序,查找
- PHP删除数组中指定值的元素常用方法实例分析【4种方法】
- C/C++面试之算法系列--1~n无序数组时间复杂度为O(n)排序