2024-08-26

二分查找算法通常用于在有序数组中查找特定元素。以下是使用Python、Java和C++实现的二分查找算法的示例代码。

Python:




def binary_search(arr, x):
    low = 0
    high = len(arr) - 1
    while low <= high:
        mid = (low + high) // 2
        if arr[mid] == x:
            return mid
        elif arr[mid] > x:
            high = mid - 1
        else:
            low = mid + 1
    return -1
 
# 示例使用
arr = [2, 3, 4, 10, 40]
x = 10
 
# 函数调用
result = binary_search(arr, x)
print(result)  # 输出元素的索引,未找到返回-1

Java:




public class BinarySearch {
    public static int binarySearch(int[] arr, int x) {
        int low = 0;
        int high = arr.length - 1;
        while (low <= high) {
            int mid = (low + high) / 2;
            if (arr[mid] == x) {
                return mid;
            } else if (arr[mid] > x) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }
 
    // 示例使用
    public static void main(String[] args) {
        int[] arr = {2, 3, 4, 10, 40};
        int x = 10;
 
        // 函数调用
        int result = binarySearch(arr, x);
        System.out.println(result);  // 输出元素的索引,未找到返回-1
    }
}

C++:




#include <iostream>
#include <vector>
 
int binarySearch(const std::vector<int>& arr, int x) {
    int low = 0;
    int high = arr.size() - 1;
    while (low <= high) {
        int mid = (low + high) / 2;
        if (arr[mid] == x) {
            return mid;
        } else if (arr[mid] > x) {
            high = mid - 1;
        } else {
            low = mid + 1;
        }
    }
    return -1;
}
 
int main() {
    std::vector<int> arr = {2, 3, 4, 10, 40};
    int x = 10;
 
    // 函数调用
    int result = binarySearch(arr, x);
    std::cout << result << std::endl;  // 输出元素的索引,未找到返回-1
    return 0;
}

以上代码实现了二分查找算法,在有序数组中查找特定元素的索引。如果找到,返回元素的索引;如果未找到,返回-1。

2024-08-26



public class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
        next = null;
    }
}
 
public class LinkedListAlgorithm {
 
    // 删除有序链表中值相同的节点
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode current = head;
        while (current.next != null) {
            if (current.val == current.next.val) {
                current.next = current.next.next;
            } else {
                current = current.next;
            }
        }
        return head;
    }
 
    // 合并两个有序链表
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        } else if (l2 == null) {
            return l1;
        }
        ListNode dummy = new ListNode(0);
        ListNode current = dummy;
        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                current.next = l1;
                l1 = l1.next;
            } else {
                current.next = l2;
                l2 = l2.next;
            }
            current = current.next;
        }
        if (l1 != null) {
            current.next = l1;
        }
        if (l2 != null) {
            current.next = l2;
        }
        return dummy.next;
    }
 
    // 合并k个有序链表
    public ListNode mergeKLists(ListNode[] lists) {
        if (lists == null || lists.length == 0) {
            return null;
        }
        ListNode dummy = new ListNode(0);
        ListNode current = dummy;
        PriorityQueue<ListNode> pq = new PriorityQueue<>(new Comparator<ListNode>() {
            @Override
            public int compare(ListNode o1, ListNode o2) {
                return o1.val - o2.val;
            }
        });
        for (ListNode head : lists) {
            if (head != null) {
                pq.add(head);
            }
        }
        while (!pq.isEmpty()) {
            ListNode
2024-08-26

以下是插入排序、希尔排序、选择排序和堆排序的Java实现示例代码:




// 插入排序
public void insertionSort(int[] arr) {
    for (int i = 1; i < arr.length; i++) {
        int key = arr[i];
        int j = i - 1;
 
        // 移动元素
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}
 
// 希尔排序
public void shellSort(int[] arr) {
    int n = arr.length;
    int h = 1;
    while (h < n / 3) {
        h = h * 3 + 1;
    }
 
    while (h >= 1) {
        for (int i = h; i < n; i++) {
            int temp = arr[i];
            int j = i;
            while (j >= h && arr[j - h] > temp) {
                arr[j] = arr[j - h];
                j = j - h;
            }
            arr[j] = temp;
        }
        h = h / 3;
    }
}
 
// 选择排序
public void selectionSort(int[] arr) {
    for (int i = 0; i < arr.length - 1; i++) {
        int minIndex = i;
        for (int j = i + 1; j < arr.length; j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }
        }
        int temp = arr[minIndex];
        arr[minIndex] = arr[i];
        arr[i] = temp;
    }
}
 
// 堆排序
public void heapSort(int[] arr) {
    int n = arr.length;
 
    // Build heap (rearrange array)
    for (int i = n / 2 - 1; i >= 0; i--) {
        heapify(arr, n, i);
    }
 
    // One by one extract elements
    for (int i = n - 1; i >= 0; i--) {
        // Move current root to end
        int temp = arr[0];
        arr[0] = arr[i];
        arr[i] = temp;
 
        // heapify the root element
        heapify(arr, i, 0);
    }
}
 
private void heapify(int[] arr, int n, int i) {
    int largest = i; // Initialize largest as root
    int l = 2 * i + 1; // left = 2*i + 1
    int r = 2 * i + 2; // right = 2*i + 2
 
    // If left child is larger than root
    if (l < n && arr[l] > arr[largest]) {
        largest = l;
    }
 
    // If right child is larger than largest so far
    if (r < n && arr[r] > arr[largest]) {
        largest = r;
    }
 
    // If largest is not root
    if (largest != i) {
        int swap = arr[i];
        arr[i] = arr[largest];
        arr[largest] = swap;
 
        // Recursively heapify the affected sub-tree
        heapify(arr, n, largest);
    }
}
2024-08-26

冒泡排序、快速排序、归并排序和堆排序是四种常用的排序算法,以下是它们的Java实现:

冒泡排序:




public void bubbleSort(int[] arr) {
    int n = arr.length;
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                // 交换 arr[j+1] 和 arr[j]
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}

快速排序:




public void quickSort(int[] arr, int low, int high) {
    if (low < high) {
        // 获取分区后的枢纽位置
        int pivotIndex = partition(arr, low, high);
 
        // 分别对枢纽左右两边进行递归排序
        quickSort(arr, low, pivotIndex - 1);
        quickSort(arr, pivotIndex + 1, high);
    }
}
 
// 分区函数
private int partition(int[] arr, int low, int high) {
    // 选择一个枢纽元素,这里选择最高位作为枢纽
    int pivot = arr[high];
    int i = (low - 1);
    
    // 遍历数组,将小于枢纽的元素放到左边,大于枢纽的元素放到右边
    for (int j = low; j < high; j++) {
        if (arr[j] < pivot) {
            i++;
 
            // 交换 arr[i] 和 arr[j]
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }
 
    // 将枢纽元素放到正确的位置
    int temp = arr[i + 1];
    arr[i + 1] = arr[high];
    arr[high] = temp;
 
    return i + 1;
}

归并排序:




public void mergeSort(int[] arr, int low, int high) {
    if (low < high) {
        // 找到中间索引
        int mid = (low + high) / 2;
 
        // 对左边进行递归排序
        mergeSort(arr, low, mid);
 
        // 对右边进行递归排序
        mergeSort(arr, mid + 1, high);
 
        // 合并两个已排序的子数组
        merge(arr, low, mid, high);
    }
}
 
// 合并两个已排序的子数组
private void merge(int[] arr, int low, int mid, int high) {
    // 创建一个临时数组
    int[] temp = new int[high - low + 1];
    int i = low;
    int j = mid + 1;
    int k = 0;
 
    // 比较两个子数组,并将较小的元素放入临时数组中
    while (i <= mid && j <= high) {
        if (arr[i] <= arr[j]) {
            temp[k++] = arr[i++];
        } else {
            temp[k++]
2024-08-26



public class SortAlgorithms {
 
    // 交换数组中的两个元素
    private static void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
 
    // 直接插入排序
    public static void insertionSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            for (int j = i; j > 0; j--) {
                if (arr[j] < arr[j - 1]) {
                    swap(arr, j, j - 1);
                }
            }
        }
    }
 
    // 冒泡排序
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    swap(arr, j, j + 1);
                }
            }
        }
    }
 
    // 选择排序
    public static void selectionSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            swap(arr, i, minIndex);
        }
    }
 
    // 快速排序
    public static void quickSort(int[] arr, int low, int high) {
        if (low < high) {
            // 获取分区后的枢纽位置
            int pivotIndex = partition(arr, low, high);
            
            // 分别对枢纽左右两边进行递归排序
            quickSort(arr, low, pivotIndex - 1);
            quickSort(arr, pivotIndex + 1, high);
        }
    }
 
    private static int partition(int[] arr, int low, int high) {
        // 选择一个枢纽元素,这里选择最高位作为枢纽
        int pivot = arr[high];
        int i = (low - 1);
        
        // 遍历数组,将小于枢纽的元素放到左边,大于枢纽的元素放到右边
        for (int j = low; j < high; j++) {
            if (arr[j] < pivot) {
                i++;
 
                // 交换 arr[i] 和 arr[j]
                swap(arr, i, j);
            }
        }
        
        // 最后将枢纽元素放到正确的位置
        swap(arr, i + 1, high);
        
        return i + 1;
    }
 
    // 归并排序
    public static void mergeSort(int[] arr) {
        int mid = arr.length / 2;
        if (arr.length >= 2) {
            // 分割数组
            int[] leftHalf = Arrays.copyOfRange(arr, 0, mid);
            int[] rightHalf = Arrays.copyOfRange(arr, mid, arr.length);
 
            // 递归分割
            mergeSort(leftHalf);
            mergeSort(rightHalf);
 
            // 合并数组
  
2024-08-26

以下是实现单链表反转的 5 种方法的示例代码:

  1. 递归反转



public ListNode reverseList(ListNode head) {
    if (head == null || head.next == null) {
        return head;
    }
    ListNode p = reverseList(head.next);
    head.next.next = head;
    head.next = null;
    return p;
}
  1. 迭代反转



public ListNode reverseList(ListNode head) {
    ListNode prev = null;
    ListNode curr = head;
    while (curr != null) {
        ListNode nextTemp = curr.next;
        curr.next = prev;
        prev = curr;
        curr = nextTemp;
    }
    return prev;
}
  1. 交换节点反转



public ListNode reverseList(ListNode head) {
    ListNode prev = null;
    ListNode curr = head;
    while (curr != null) {
        ListNode nextTemp = curr.next; // Temporarily store the next node
        curr.next = prev; // Reverse the link
        prev = curr; // Move forward on the list
        curr = nextTemp; // Move forward on the list
    }
    return prev;
}
  1. 使用栈



public ListNode reverseList(ListNode head) {
    Deque<ListNode> stack = new LinkedList<>();
    ListNode current = head;
    while (current != null) {
        stack.push(current);
        current = current.next;
    }
    current = stack.poll();
    ListNode newHead = current;
    while (!stack.isEmpty()) {
        current.next = stack.poll();
        current = current.next;
    }
    current.next = null;
    return newHead;
}
  1. 使用头插法



public ListNode reverseList(ListNode head) {
    ListNode newHead = null;
    while (head != null) {
        ListNode next = head.next; // Temporarily store the next node
        head.next = newHead; // Reverse the link
        newHead = head; // Move forward on the list
        head = next; // Move forward on the list
    }
    return newHead;
}

以上每种方法都是单链表反转的有效方式,选择合适的方法取决于特定的应用场景和性能要求。

2024-08-26



public class BinarySearch {
 
    public static int binarySearch(int[] arr, int target) {
        int left = 0;
        int right = arr.length - 1; // 定义右边界
 
        while (left <= right) { // 当左边界小于等于右边界时执行循环
            int mid = left + (right - left) / 2; // 计算中间索引,防止溢出
 
            if (arr[mid] == target) { // 如果中间值等于目标值
                return mid; // 返回中间索引
            } else if (arr[mid] < target) { // 如果中间值小于目标值
                left = mid + 1; // 将左边界设置为中间索引的下一个位置
            } else { // 如果中间值大于目标值
                right = mid - 1; // 将右边界设置为中间索引的前一个位置
            }
        }
        return -1; // 如果没有找到目标值,返回-1
    }
 
    public static void main(String[] args) {
        int[] arr = {1, 3, 5, 7, 9};
        int target = 7;
        int index = binarySearch(arr, target);
        if (index != -1) {
            System.out.println("找到目标值,索引为:" + index);
        } else {
            System.out.println("未找到目标值");
        }
    }
}

这段代码实现了二分查找算法,在一个有序数组中查找特定的元素,并返回其索引。如果找不到,则返回-1。这是一个常用的算法,对于学习数据结构和算法有重要的意义。

2024-08-26



<template>
  <div class="selection-sort-animation">
    <div class="animation-container">
      <div
        v-for="(item, index) in items"
        :key="index"
        class="animation-bar"
        :style="{ height: `${item}px`, backgroundColor: getColor(index) }"
      ></div>
    </div>
    <button @click="startAnimation">排序</button>
  </div>
</template>
 
<script>
export default {
  data() {
    return {
      items: [...Array(10)].map(() => Math.random() * 100), // 初始化10个高度随机的方块
      sortedItems: [], // 用于存放排序后的方块数组
      sorting: false, // 是否正在进行排序
    };
  },
  methods: {
    getColor(index) {
      return this.sortedItems.includes(index) ? 'green' : 'blue';
    },
    startAnimation() {
      if (this.sorting) return; // 如果已经在排序,则不再执行
      this.sorting = true;
      this.sortedItems = []; // 重置排序记录数组
      const sort = () => {
        if (this.items.length <= 1) {
          this.sorting = false;
          return;
        }
        const index = this.findSmallest(this.items);
        const smallest = this.items.splice(index, 1)[0];
        this.sortedItems.push(index);
        setTimeout(() => {
          this.items.unshift(smallest);
          sort();
        }, 1000);
      };
      sort();
    },
    findSmallest(arr) {
      let smallest = arr[0];
      let index = 0;
      for (let i = 1; i < arr.length; i++) {
        if (arr[i] < smallest) {
          smallest = arr[i];
          index = i;
        }
      }
      return index;
    },
  },
};
</script>
 
<style scoped>
.animation-container {
  display: flex;
}
.animation-bar {
  margin: 5px;
  transition: all 0.5s;
}
</style>

这段代码实现了选择排序动画的初始化和触发。它首先在data中初始化了一个包含随机高度的方块数组,并定义了一个空数组来记录已排序的方块。在methods中定义了getColor方法来根据方块是否已排序改变颜色,以及startAnimation方法来开始排序动画过程。startAnimation方法中定义了选择排序的逻辑,并通过setTimeout模拟动画效果。这个例子展示了如何在Vue中结合JavaScript实现动画效果,并且是排序算法可视化教学的一个很好的起点。




// 以下是Brandes算法的核心函数,用于计算无向图中节点最短路径长度之和。
// 假设图以邻接矩阵的形式给出,其中distTo[v]存储从源节点到v的最短路径长度,
// 而edgeTo[v]记录了从源节点到v的路径上的前一个节点。
 
public void shortestPathLengths(boolean[] s, int V) {
    IndexMinPQ<Double> pq = new IndexMinPQ<>(V);
    for (int v = 0; v < V; v++) {
        if (s[v]) {
            distTo[v] = 0.0;
            pq.insert(v, 0.0);
        } else {
            distTo[v] = Double.POSITIVE_INFINITY;
        }
        edgeTo[v] = -1;
    }
 
    while (!pq.isEmpty()) {
        int v = pq.delMin();
        for (Edge e : G.adj(v)) {
            int w = e.other(v);
            if (Double.POSITIVE_INFINITY == distTo[w]) {
                distTo[w] = distTo[v] + e.weight();
                edgeTo[w] = v;
                pq.insert(w, distTo[w]);
            }
        }
    }
}

这段代码实现了Brandes算法的核心部分,用于计算给定源节点的最短路径长度。它使用了一个索引最小优先队列来有效地维护当前最短路径长度的节点。在实际应用中,需要先初始化distToedgeTo数组,并根据需要调整IndexMinPQ的实现。

在Elasticsearch中,ik分词器是一个非常流行的中文分词器,它提供了多种分词算法,并且容易进行扩展。然而,在使用ik分词器的过程中,可能会遇到各种问题,如内存泄露、性能问题等。

解决ik分词器可能遇到的问题,需要从以下几个方面入手:

  1. 监控和分析GC(垃圾回收)日志,确保Elasticsearch的堆内存分配是合理的,避免频繁的FGC和OOM。
  2. 调整JVM堆的大小和分配,确保Elasticsearch有足够的堆内存来支持ik分词器的运行。
  3. 优化ik分词器的配置,包括词典、停用词等,减少内存的使用。
  4. 使用ik分词器的最新版本,这些版本可能修复了内存泄露的问题,或者提供了新的优化。
  5. 如果问题仍然存在,可以考虑使用其他分词器,或者自定义分词器插件,以解决特定问题。

下面是一个简单的示例,演示如何调整Elasticsearch的JVM参数来优化ik分词器的性能和内存使用:




# 设置Elasticsearch的最大堆内存和初始堆内存
export ES_HEAP_SIZE=16g
export ES_MAX_MEM=16g
 
# 启动Elasticsearch
./bin/elasticsearch

在生产环境中,监控工具如Elasticsearch自带的Monitoring功能,或第三方监控工具(如ElasticHQ、Grafana),可以帮助你实时监控Elasticsearch的性能和资源使用情况,及时发现问题。

综上所述,要精细地玩转ik分词器,需要对JVM内存管理、分词器配置、Elasticsearch监控等有深入的理解和实践经验。在实际操作中,还需要结合具体的Elasticsearch版本和部署环境进行调整和优化。