java排序算法总结

排序引用方法: 写道
 
public static void swap(int[] b,int i,int j){
		int t;
		t=b[i];
		b[i]=b[j];
		b[j]=t;
	}

 

1.冒泡排序
 
public int[] BubbleSort(int[] array) {
		for (int i = 0; i < array.length; i++) {
			for (int j = 0; j < array.length - i - 1; j++) {
				if (array[j] > array[j + 1]) {
					swap(array,j,j+1);
				}
			}
		}
		return array;
	}

 

2.快速排序算法
 
private static int[]quickSort(int[] array, int low, int high) {
        if (low < high) {
            int p = partition(array, low, high);
            quickSort(array, low, p - 1);
            quickSort(array, p + 1, high);
        }
        return array;
    }

    private static int partition(int[] array, int low, int high) {
        int s = array[high];
        int i = low - 1;
        for (int j = low; j < high; j++) {
            if (array[j] < s) {
                i++;
                swap(array, i, j);
            }
        }
        swap(array, ++i, high);
        return i;
    }
 

 

 

 

 

 

你可能感兴趣的:(java,算法,J#)