将较大数字向右移动,将较小数字向左移动给定值

我有一个家庭作业问题,给了我这个数组:

[3, 22, 1, 5, 6, 10, 4]

我需要将所有大于最后一个值 4 的数字移动到值的右侧,并将所有较小的值移动到左侧。

数字不一定需要按顺序排列。因此,该程序的输出将是:

[3, 1, 4, 22, 5, 6, 10]

出于某种原因,我真的很难想到一种算法可以允许这种情况发生。我尝试过创建一个循环,将最后一个值与较大的数字交换,但是如果数组中的某个奇数混合了最小的值,它将位于值的右侧,这是不正确的。

任何人都可以帮我吗?


隔江千里
浏览 109回答 2
2回答

撒科打诨

我不会帮你完成家务。但是我会引导你思考你的这个例子将走向何方。这是快速排序的第一步 - 对数组进行分区。public class QuickSortImpl {&nbsp; &nbsp; private static void swap(int[] array, int l, int h) {&nbsp; &nbsp; &nbsp; &nbsp; int temp = array[h];&nbsp; &nbsp; &nbsp; &nbsp; array[h] = array[l];&nbsp; &nbsp; &nbsp; &nbsp; array[l] = temp;&nbsp; &nbsp; }&nbsp; &nbsp; public static int partition(int[] array, int low, int high) {&nbsp; &nbsp; &nbsp; &nbsp; int pivot = high;&nbsp; &nbsp; &nbsp; &nbsp; int firsthigh = low;&nbsp; &nbsp; &nbsp; &nbsp; int x,y;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = low; i < high; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = array[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y = array[pivot];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array[i] < array[pivot]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swap(array, i, firsthigh);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firsthigh++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; swap(array, pivot, firsthigh);&nbsp; &nbsp; &nbsp; &nbsp; return firsthigh;&nbsp; &nbsp; }&nbsp; &nbsp; private static void printArray(int[] arr ) {&nbsp; &nbsp; &nbsp; &nbsp; for ( int i =0; i < arr.length; i++ ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(" " + arr[i]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; }&nbsp; &nbsp; public static void quickSort(int[] array, int low, int high) {&nbsp; &nbsp; &nbsp; &nbsp; if ( low < high ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int pivot = partition(array, low, high);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quickSort(array, low, pivot - 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; quickSort(array, pivot + 1, high);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; int[] arr = { 3, 22, 1, 5, 6, 10, 4};&nbsp; &nbsp; &nbsp; &nbsp; quickSort(arr, 0, arr.length -1 );&nbsp; &nbsp; &nbsp; &nbsp; printArray(arr);&nbsp; &nbsp; }}

哆啦的时光机

看看这个简单的例子:public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Integer[] listOfNumbers = {1,4,5,6,74,2,7,8,5,2,6,989,3};&nbsp; &nbsp; &nbsp; &nbsp; //sort by number 6&nbsp; &nbsp; &nbsp; &nbsp; FirstCustomComparator comparator = new FirstCustomComparator(6);&nbsp; &nbsp; &nbsp; &nbsp; Arrays.sort(listOfNumbers, 0, listOfNumbers.length, comparator);&nbsp; &nbsp; &nbsp; &nbsp; for (int number : listOfNumbers) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(number+" ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }第一定制比较器类:public class FirstCustomComparator implements Comparator<Integer> {&nbsp; &nbsp; private final int exception;&nbsp; &nbsp; public FirstCustomComparator(int i)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; exception = i;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int compare(Integer a, Integer b)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (a < exception)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return a.compareTo(b);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java