排序算法 面试 JAVA

**1.内部排序算法:

**选择排序(直接选择排序,堆排序)

交换排序(冒泡排序,快速排序)

插入排序(直接插入排序,折半插入排序,Shell排序)

归并排序

桶式排序

基数排序**

2.外部排序**

最常用的是多路归并排序,即将原文件分解称多个能够一次性装入内存的部分,分别把每一部分调入内存完成排序,接下来再对多个有序的子文件进行**归并排序**



**冒泡排序**

/*
 * 冒泡排序
 */
public class BubbleSort {
    
	public void bubble(Integer[] data){
		for(int i=0;i<data.length;i++){
			for(int j=0;j<data.length-1-i;j++){
				if(data[j]>data[j+1]){   //如果后一个数小于前一个数交换
					int tmp=data[j];
					data[j]=data[j+1];
					data[j+1]=tmp;
				}
			}
		}
	}
}


**插入排序**

	public static void insertSort(int[] array) {
		if (array == null || array.length < 2) {
			return;
		}

		for (int i = 1; i < array.length; i++) {
			int tmp = array[i];
			int position = i;
			for (int j = i - 1; j >= 0; j--) {
				if (array[j] > tmp) {
					array[j + 1] = array[j];
					position--;
				} else {
					break;
				}
			}
			array[position] =tmp;
		}
	}


**选择排序**

	public static void selectSort(Integer[] a) {
		if (a == null || a.length <= 0) {
			return;
		}
		for (int i = 0; i < a.length; i++) {
			int min = i; /* 将当前下标定义为最小值下标 */

			for (int j = i + 1; j < a.length; j++) {
				if (a[min] > a[j]) {   /* 如果有小于当前最小值的关键字 */
					min = j;       /* 将此关键字的下标赋值给min */
				}
			}
			if (i != min) {               /* 若min不等于i,说明找到最小值,交换 */
				int tmp = a[min];
				a[min] = a[i];
				a[i] = tmp;
			}
		}
	}


**快速排序**

    public static void quickSort(int[]n ,int left,int right){  
        int pivot;  
        if (left < right) {  
            //pivot作为枢轴,较之小的元素在左,较之大的元素在右  
            pivot = partition(n, left, right);  
            //对左右数组递归调用快速排序,直到顺序完全正确  
            quickSort(n, left, pivot - 1);  
            quickSort(n, pivot + 1, right);  
        }  
    }  
      
    public static int partition(int[]n ,int left,int right){  
        int pivotkey = n[left];  
        //枢轴选定后永远不变,最终在中间,前小后大  
        while (left < right) {  
            while (left < right && n[right] >= pivotkey) --right;  
            //将比枢轴小的元素移到低端,此时right位相当于空,等待低位比pivotkey大的数补上  
            n[left] = n[right];  
            while (left < right && n[left] <= pivotkey) ++left;  
            //将比枢轴大的元素移到高端,此时left位相当于空,等待高位比pivotkey小的数补上  
            n[right] = n[left];  
        }  
        //当left == right,完成一趟快速排序,此时left位相当于空,等待pivotkey补上  
        n[left] = pivotkey;  
        return left;  
    }  

**堆排序**

public class HeapSort {  
        public static void main(String[] args) {  
            int[] array = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3 };  
            heapSort(array);  
        }  
  
        public static void heapSort(int[] array) {  
            if (array == null || array.length <= 1) {  
                return;  
            }  
  
            buildMaxHeap(array);  //构建
  
            for (int i = array.length - 1; i >= 1; i--) {  //
                ArrayUtils.exchangeElements(array, 0, i);  //最大交换到最后
                maxHeap(array, i, 0);  //调整
            }  
        }  
//构建
        private static void buildMaxHeap(int[] array) {  
            if (array == null || array.length <= 1) {  
                return;  
            }  
  
            int half = array.length / 2;  
            for (int i = half; i >= 0; i--) {  
                maxHeap(array, array.length, i);  
            }  
        }  
//调整
        private static void maxHeap(int[] array, int heapSize, int index) {  
            int left = index * 2 + 1;  
            int right = index * 2 + 2;  
  
            int largest = index;  
            if (left < heapSize && array[left] > array[index]) {  
                largest = left;  
            }  
  
            if (right < heapSize && array[right] > array[largest]) {  
                largest = right;  
            }  
  
            if (index != largest) {  
                ArrayUtils.exchangeElements(array, index, largest);  
                maxHeap(array, heapSize, largest);  
            }  
        }  
    }  





**归并排序**

public class MergeSort {
	/**
	 * 归并排序
	 * 简介:将两个(或两个以上)有序表合并成一个新的有序表 即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列
	 * 时间复杂度为O(nlogn)
	 * 稳定排序方式
	 * @param nums 待排序数组
	 * @return 输出有序数组
	 */
	public static int[] sort(int[] nums, int low, int high) {
		int mid = (low + high) / 2;
		if (low < high) {
			      // 左边
			sort(nums, low, mid);
			      // 右边
			sort(nums, mid + 1, high);
			      // 左右归并
			merge(nums, low, mid, high);
		}
		return nums;
	}

	public static void merge(int[] nums, int low, int mid, int high) {
		int[] temp = new int[high - low + 1];
		int i = low;    // 左指针
		int j = mid + 1;// 右指针
		int k = 0;

		// 把较小的数先移到新数组中
		while (i <= mid && j <= high) {
			if (nums[i] < nums[j]) {
				temp[k++] = nums[i++];
			} else {
				temp[k++] = nums[j++];
			}
		}
		// 把左边剩余的数移入数组
		while (i <= mid) {
			temp[k++] = nums[i++];
		}
		// 把右边边剩余的数移入数组
		while (j <= high) {
			temp[k++] = nums[j++];
		}
		// 把新数组中的数覆盖nums数组
		for (int k2 = 0; k2 < temp.length; k2++) {
			nums[k2 + low] = temp[k2];
		}
	}	
	// 归并排序的实现
	public static void main(String[] args) {
		int[] nums = { 2, 7, 8, 3, 1, 6, 9, 0, 5, 4 };
		MergeSort.sort(nums, 0, nums.length-1);
		System.out.println(Arrays.toString(nums));
	}
}


**基数排序**


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值