数组二分查找法(Java)

数组二分查找法(Java)
public class CSearch {

    /**
     * Use binary search method to find specific element from an array.
     *
     * @param key specific element
     * @param arr array
     * @return if found, return the index of specific element, if not, return -1
     */
    public static int binarySearch(int key, int[] arr) {
        return binarySearch(key, arr, 0, arr.length - 1);
    }

    /**
     * Using binary search method to find specific element from a specific range of indexes in an array
     *
     * @param key specific element
     * @param arr array
     * @param lo begin index
     * @param hi end index
     * @return if found, return the index of specific element, if not, return -1
     */
    public static int binarySearch(int key, int[] arr, int lo, int hi) {
        if (lo > hi)
            return -1;
        int mid = lo + (hi - lo) / 2;
        if (key < arr[mid])
            return binarySearch(key, arr, lo, mid - 1);
        if (key > arr[mid])
            return binarySearch(key, arr, mid + 1, hi);
        else
            return mid;
    }

    /**
     * Use binary search method to find specific element from an array.
     *
     * @param key specific element
     * @param arr array
     * @return if found, return the index of specific element, if not, return -1
     */
    public static int binarySearch(double key, double[] arr) {
        return binarySearch(key, arr, 0, arr.length - 1);
    }

    /**
     * Using binary search method to find specific element from a specific range of indexes in an array
     *
     * @param key specific element
     * @param arr array
     * @param lo begin index
     * @param hi end index
     * @return if found, return the index of specific element, if not, return -1
     */
    public static int binarySearch(double key, double[] arr, int lo, int hi) {
        if (lo > hi)
            return -1;
        int mid = lo + (hi - lo) / 2;
        if (key < arr[mid])
            return binarySearch(key, arr, lo, mid - 1);
        if (key > arr[mid])
            return binarySearch(key, arr, mid + 1, hi);
        else
            return mid;
    }
}
Linux系统中,使用`nohup`命令运行程序时,默认会生成一个名为`nohup.out`的日志文件来记录程序的标准输出和标准错误输出。当用户尝试清空该日志文件时,有时会发现除了原来的`nohup.out`外,还会出现另一个同名文件或者分割后的文件,这可能是由于以下原因造成的: ### 原因分析 1. **程序仍在运行并持续写入日志** 如果在清空`nohup.out`时,相关的进程仍在运行,并且继续向该文件写入数据,那么即使你删除或清空了`nohup.out`,操作系统并会立即释放磁盘空间,因为文件句柄仍被进程占用。此时,若你创建了一个新的`nohup.out`文件,可能会造成两个“相同”文件的存在(实际上是一个文件的两个链接)[^3]。 2. **脚本操作当导致新文件生成** 在一些清理脚本中,如引用[2]中的`ClearNohup.sh`脚本,在执行过程中会对`nohup.out`进行分割和归档操作。例如使用`split`命令将大文件切分为多个小文件,并以特定命名格式保存到`logs`目录中。这种情况下,`nohup-${current_date}`格式的文件会被创建出来,看起来像是生成了多个`nohup`文件[^2]。 3. **日志轮转机制触发新文件生成** 有些系统或应用程序配置了日志轮转(log rotation)机制,比如通过`logrotate`工具定期压缩、备份或删除旧日志文件。如果`nohup.out`被纳入此类机制中,也可能导致新文件的生成[^4]。 --- ### 解决方法 #### 方法一:安全清空正在写入的 `nohup.out` 如果你希望清空一个当前正在被写入的`nohup.out`文件影响运行中的进程,可以使用以下方式: ```bash cat /dev/null > nohup.out ``` 这种方式会删除文件本身,而是清空其内容,保留文件句柄变,因此会中断正在写入的进程。 #### 方法二:避免重复创建文件 确保在清空或处理`nohup.out`之前,先停止相关进程,再进行文件操作: ```bash pkill -f logstash # 示例:终止与logstash相关的进程 rm -f nohup.out # 删除文件 touch nohup.out # 可选:重新创建空文件 ``` 这样可以防止在操作过程中产生多个版本的日志文件。 #### 方法三:使用日志分割工具替代直接清空 可以采用更专业的日志管理工具(如`logrotate`)来自动管理日志文件的大小和生命周期,避免手动干预带来的问题。例如,创建一个`logrotate`配置文件: ```bash sudo vi /etc/logrotate.d/nohup ``` 内容如下: ```conf /path/to/nohup.out { daily missingok rotate 7 compress delaycompress notifempty create 644 user group } ``` 此配置每天检查一次`nohup.out`,最多保留7个历史版本,并对其进行压缩管理。 #### 方法四:优化定时任务脚本逻辑 参考引用[2]中的脚本,在编写清理脚本时应确保逻辑严谨,例如在执行分割前检查文件是否存在,分割后及时清理文件,并避免并发执行导致冲突。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值