第七章第三十二题(列表分区)(List partition)

第七章第三十二题(列表分区)(List partition)

  • **7.32(列表分区)编写以下方法,使用第一个元素对列表进行分区,该元素称为中心点。
    public static int partition(int[] list)
    分区后,列表中的元素被重新安排,在中心点元素之前的元素都小于或者等于该元素,而之后的元素都大于该元素。方法返回中心点元素位于新列表中的下标。例如,假设列表是{5,2,9,3,8}。最多进行list.length次比较来实现该方法。该实现的动画演示参见编写一个测试程序,提示用户输入一个列表的大小以及内容,然后显示分区后的列表。下面是一个运行示例。
    Enter list1 size: 8
    Enter list contents: 10 1 5 16 61 9 11 1
    After the partition, the list is 1 9 5 1 10 16 61 11
    *7.32(List partition)List partition (list partition) write the following method to partition the list with the first element, which is called the center point.

public static int partition(int[] list)

After partitioning, the elements in the list are rearranged, the elements before the center point element are less than or equal to the element, and the elements after are all greater than the element. Method returns the subscript of the center element in the new list. For example, suppose the list is {5, 2, 9, 3, 8}. At most list.length To implement the method. Animation of the implementation [for demonstration, see]Write a test program, prompt the user to input the size and content of a list, and then display the list after partition. Here is a running example.
Enter list1 size: 8
Enter list contents: 10 1 5 16 61 9 11 1
After the partition, the list is 1 9 5 1 10 16 61 11

  • 参考代码:

    package chapter07;
    
    import java.util.Scanner;
    
    public class Code_32 {
        public static void main(String[] args){
            Scanner input = new Scanner(System.in);
            System.out.print("Enter list1 size: ");
            int num1 = input.nextInt();
            System.out.print("Enter list contents: ");
            int[] l1 = new int[num1];
            for(int i = 0;i < num1;i++)
                l1[i] = input.nextInt();
            partition(l1);
            System.out.print("After the partition, the list is ");
            for(int i = 0;i < num1;i++)
                System.out.print(l1[i] + " ");
            System.out.println();
        }
        public static int partition(int[] list){
            int len = list.length;
            int[] help = new int[2 * len];
            int pivot = list[0];
            help[len + 1] = list[0];
            int p1 = len;
            int p2 = len + 2;
            for(int i = 1;i < len;i++) {
                if(list[i] > pivot) {
                    help[p2] = list[i];
                    p2++;
                }
                else {
                    help[p1] = list[i];
                    p1--;
                }
            }
            for(int i = 0;i < len;i++)
                list[i] = help[p1 + 1 + i];
            return len - p1;
        }
    
    }
    
    
  • 结果显示:

    Enter list1 size: 8
    Enter list contents: 10 1 5 16 61 9 11 1
    After the partition, the list is 1 9 5 1 10 16 61 11 
    
    Process finished with exit code 0
    
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值