Android面试常见的面试题

1 快速排序算法

private static void quickSort() {
		int[] arr = new int[] { 6, 1, 2, 7, 9, 3, 4, 5, 10, 8 };
		qSort(arr, 0, arr.length - 1);
		System.out.println(Arrays.toString(arr));
	}
     
private static void qSort(int[] arr, int low, int high) {
		if (low < high) {
			int piovt = partition(arr, low, high);
			System.out.println("pivot :"+piovt);
			qSort(arr, low, piovt - 1);
			qSort(arr, piovt + 1, high);
		}
	
	}

	private static int partition(int[] arr, int low, int high) {
		int pivot  = arr[low];
		while (low < high) {
			while (low < high && arr[high] >= pivot ) {
				high--;
			}
			arr[low] = arr[high];
			
			while (low < high && arr[low] <= pivot ) {
				low++;
			}
			arr[high] = arr[low];
		}
		arr[low] = pivot;
		return low;
	}

2 冒泡排序

	private static void bubbleSort() {
		int[] arr = new int[] { 5,2,1,4,3,0};
		boolean sortStatus = true;
		int count = 0; //排序次数
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr.length - i - 1; j++) {
				if (arr[j] > arr[j + 1]) {
					int temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = temp;
					sortStatus = false;
					count++;
				}
			}
			if (sortStatus) { //说明已经是有序了
				break;
			}
		}
		System.out.println("count = "+count);
		System.out.println(Arrays.toString(arr));
	}

 

你可能感兴趣的:(安卓)