您的位置:首页 > 职场人生

面试常用的4种数组排序

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

这里记录了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(" ");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: