【学习】不同语言的算法整理

2024-08-14 15:51:15 浏览数 (1)

1.javascript

代码语言:javascript复制
function bucketSort(arr, bucketSize = 5) {
    if (!Array.isArray(arr)) {
      throw new TypeError('Input must be an array');
    }
  
    const max = Math.max(...arr);
    const min = Math.min(...arr);
    const range = Math.ceil((max - min) / bucketSize);
    const buckets = Array.from({ length: range }, () => []);
  
    for (const num of arr) {
      const index = Math.floor((num - min) / bucketSize);
      if (buckets[index] === undefined) {
        buckets[index] = [];
      }
      buckets[index].push(num);
    }
  
    return buckets.reduce((sortedArr, bucket) => {
      return sortedArr.concat(bucket.sort((a, b) => a - b));
    }, []);
  }
  
// 测试用例
const testArr1 = [3, 5, 1, 7, 8, 2, 8, 4];
console.log(bucketSort(testArr1)); // [1, 2, 3, 4, 5, 7, 8, 8]

const testArr2 = [0.1, 0.5, 0.2, 0.3, 0.6, 0.4];
console.log(bucketSort(testArr2, 0.1)); // [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]

const testArr3 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(bucketSort(testArr3)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]

const testArr4 = [9, 8, 7, 6, 5, 4, 3, 2, 1];
console.log(bucketSort(testArr4)); // [1, 2, 3, 4, 5, 6, 7, 8, 9]

2.python

代码语言:python代码运行次数:1复制
import math

def bucket_sort(arr, bucket_size=5):
    if not isinstance(arr, list):
        raise TypeError("Input must be a list")

    max_val = max(arr)
    min_val = min(arr)
    range_val = math.ceil((max_val - min_val) / bucket_size)
    buckets = [[] for _ in range(range_val)]

    for num in arr:
        index = math.floor((num - min_val) / bucket_size)
        if index >= len(buckets):
            index = len(buckets) - 1
        buckets[index].append(num)

    sorted_arr = []
    for bucket in buckets:
        sorted_arr.extend(sorted(bucket))

    return sorted_arr

# 测试用例
test_arr1 = [3, 5, 1, 7, 8, 2, 8, 4]
print(bucket_sort(test_arr1))  # [1, 2, 3, 4, 5, 7, 8, 8]

test_arr2 = [0.1, 0.5, 0.2, 0.3, 0.6, 0.4]
print(bucket_sort(test_arr2, 0.1))  # [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]

test_arr3 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print(bucket_sort(test_arr3))  # [1, 2, 3, 4, 5, 6, 7, 8, 9]

test_arr4 = [9, 8, 7, 6, 5, 4, 3, 2, 1]
print(bucket_sort(test_arr4))  # [1, 2, 3, 4, 5, 6, 7, 8, 9]

3.c

代码语言:c复制
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node *next;
} Node;

Node *create_node(int data) {
    Node *new_node = (Node *)malloc(sizeof(Node));
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

void print_list(Node *head) {
    Node *current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULLn");
}

Node *reverse_list(Node *head) {
    Node *prev = NULL;
    Node *current = head;
    Node *next = NULL;

    while (current != NULL) {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }

    return prev;
}

int main() {
    Node *head = create_node(1);
    head->next = create_node(2);
    head->next->next = create_node(3);
    head->next->next->next = create_node(4);
    head->next->next->next->next = create_node(5);

    printf("Original list: ");
    print_list(head);

    head = reverse_list(head);

    printf("Reversed list: ");
    print_list(head);

    return 0;
}

4.c

代码语言:cpp复制
#include <iostream>
using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

void preorder_traversal(TreeNode *root) {
    if (root == NULL) {
        return;
    }

    cout << root->val << " ";
    preorder_traversal(root->left);
    preorder_traversal(root->right);
}

int main() {
    // 构建测试用例
    TreeNode *root = new TreeNode(1);
    root->left = new TreeNode(2);
    root->right = new TreeNode(3);
    root->left->left = new TreeNode(4);
    root->left->right = new TreeNode(5);
    root->right->left = new TreeNode(6);
    root->right->right = new TreeNode(7);

    cout << "前序遍历结果: ";
    preorder_traversal(root);
    cout << endl;

    return 0;
}

0 人点赞