【无标题】

算法实验作业

实验一内容

实验一要求实现一个简单的二分查找功能:

设计二分查找的函数,然后再Test1中去调用这个函数,在主函数中调用这个Test即可

#include 
#include 

int Demo1_binary_search(int arr[], int size, int target) {
    int left = 0;
    int right = size - 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;
}

void Test1()
{
    int arr[] = { 1, 3, 5, 7, 9, 11, 13, 15 };
    int size = sizeof(arr) / sizeof(arr[0]);
    int target = 7;

    int result = Demo1_binary_search(arr, size, target);

    if (result != -1) {
        std::cout << "Element found at index: " << result << std::endl;
    }
    else {
        std::cout << "Element not found in the array" << std::endl;
    }
}



int main() {
  
    Test1();
    return 0;
}

实验二内容

实验内容:

对于环形链表,编写函数求出其中环的长度。

掌握链表节点的定义和链表的创建方法。

使用双指针方法解决某些算法问题。

其实这个在Leetcode上面有专门的题目,这个是力扣题目的链接:

142. 环形链表 II - 力扣(LeetCode)

class Solution {
public:
	ListNode* detectCycle(ListNode* head)
	{
		if (head == nullptr) return nullptr;
		//先判断是否存在环
		ListNode* Fast = head;
		ListNode* Slow = head;
		while (Fast != nullptr && Fast->next!=nullptr)
		{
            Fast = Fast->next->next;
			Slow = Slow->next;
			
			if (Fast == Slow)
			{
				ListNode* IndexMet = Fast;
				ListNode* IndexHead = head;
				while (IndexMet != IndexHead)
				{
					IndexMet = IndexMet->next;
					IndexHead = IndexHead->next;
				}
				return IndexMet;
			}
		}
		return nullptr;
	}
};

输出的结果如下:
【无标题】_第1张图片

实验三内容

用递归来实现二分查找

    int Demo3_binary_search(int arr[], int size, int target) {
        if (size == 0) {
            return -1;
        }
        if (arr[size / 2] == target) {
            return size / 2;
        }
        else if (arr[size / 2] < target) {
            return Demo3_binary_search(arr, size / 2, target);
        }
        else {
            return Demo3_binary_search(arr + size / 2 + 1, size / 2, target);
        }
    }

    int Test3() {
        int arr[] = { 1, 3, 5, 7, 9, 11, 13, 15 };
        int size = sizeof(arr) / sizeof(arr[0]);
        int target = 7;

        int result = Demo3_binary_search(arr, size, target);
        if (result != -1) {
            cout << "Element found at index: " << result << endl;
        }
        else {
            cout << "Element not found in the array" << endl;
        }

        return 0;
    }

实验四内容

插入排序的代码

void Demo4_insertionSort(int arr[], int n) {
    for (int i = 1; i < n; i++) {
        int key = arr[i];
        int j = i - 1;

        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    }
}

int Test4 (){
    int arr[] = {5, 2, 8, 1, 3, 7, 6, 4};
    int n = sizeof(arr) / sizeof(arr[0]);

    Demo4_insertionSort(arr, n);

    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }

    return 0;
}

选择排序的代码

void Demo4_selectionSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        int minIndex = i;
        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }
        }
        if (minIndex != i) {
            std::swap(arr[i], arr[minIndex]);
        }
    }
}

int Test4_selectionSort() {
    int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
    int n = sizeof(arr) / sizeof(arr[0]);

    Demo4_selectionSort(arr, n);

    std::cout << "Sorted array: ";
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;
    return 0;
}

实验五内容

快速排序的代码

用递归来实现

int quick_sort(int arr[], int low, int high) {
    if (low < high) {
        int pivot = Demo5_partition(arr, low, high);
        quick_sort(arr, low, pivot - 1);
        quick_sort(arr, pivot + 1, high);
    }
    return 0;
}

int Demo5_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++;
            Demo5_swap(arr[i], arr[j]);
        }
    }
    Demo5_swap(arr[i + 1], arr[high]);
    return i + 1;
}

void Demo5_swap(int& a, int& b) {
    int temp = a;
    a = b;
    b = temp;
}

int Test5() {
    int arr[] = { 10, 7, 8, 9, 1, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);

    quick_sort(arr, 0, n - 1);

    cout << "Sorted array: ";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;

    return 0;
}

实验六内容

void Demo6_swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

void Demo6_heapify(int arr[], int n, int i) {
    int largest = i;
    int left = 2 * i + 1;
    int right = 2 * i + 2;

    if (left < n && arr[left] > arr[largest]) {
        largest = left;
    }

    if (right < n && arr[right] > arr[largest]) {
        largest = right;
    }

    if (largest != i) {
        Demo6_swap(&arr[i], &arr[largest]);
        Demo6_heapify(arr, n, largest);
    }
}

void Demo6_heapSort(int arr[], int n) {
    for (int i = n / 2 - 1; i >= 0; i--) {
        Demo6_heapify(arr, n, i);
    }

    for (int i = n - 1; i > 0; i--) {
        Demo6_swap(&arr[0], &arr[i]);
        Demo6_heapify(arr, i, 0);
    }
}

int Test6() {
    int arr[] = { 12, 11, 13, 5, 6, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);

    Demo6_heapSort(arr, n);

    std::cout << "Sorted array is: ";
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;

    return 0;
}

实验七的内容

计算树的高度

把函数封装在这个类中,使用的时候直接调用类中的函数Test就可以了

class Demo7 
{
public:
    struct TreeNode {
        int val;
        TreeNode* left;
        TreeNode* right;

        TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    };

    int getTreeHeight(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }
        int leftHeight = getTreeHeight(root->left);
        int rightHeight = getTreeHeight(root->right);
        return max(leftHeight, rightHeight) + 1;
    }

    int Test7() {
        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);

        cout << "Tree height: " << getTreeHeight(root) << endl;

        return 0;
    }
};
  

实验八的内容

 
#include 
class Demo8
{
public:
    struct TreeNode {
        int val;
        TreeNode* left;
        TreeNode* right;

        TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    };
    int findTreeHeight(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }

        queue q;
        q.push(root);
        int height = 0;

        while (!q.empty()) {
            height++;
            int n = q.size();
            for (int i = 0; i < n; i++) {
                TreeNode* node = q.front();
                q.pop();
                if (node->left != nullptr) {
                    q.push(node->left);
                }
                if (node->right != nullptr) {
                    q.push(node->right);
                }
            }
        }

        return height;
    }

    int Test8() {
        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->right = new TreeNode(6);

        cout << "Tree height: " << findTreeHeight(root) << endl;

        return 0;
    }
};

你可能感兴趣的:(算法,数据结构)