当前位置: 首页 > news >正文

威客做的好的网站专业北京seo公司

威客做的好的网站,专业北京seo公司,硬件工程师40岁后的出路,wordpress主题 win8文章目录 选择合适的数据结构数组链表栈队列树图哈希表 选择合适的算法实践和项目 🎉欢迎来到数据结构学习专栏~实践和项目:解决实际问题时,选择合适的数据结构和算法 ☆* o(≧▽≦)o *☆嗨~我是IT陈寒🍹✨博客主页:IT…

文章目录

    • 选择合适的数据结构
      • 数组
      • 链表
      • 队列
      • 哈希表
    • 选择合适的算法
    • 实践和项目

在这里插入图片描述

🎉欢迎来到数据结构学习专栏~实践和项目:解决实际问题时,选择合适的数据结构和算法


  • ☆* o(≧▽≦)o *☆嗨~我是IT·陈寒🍹
  • ✨博客主页:IT·陈寒的博客
  • 🎈该系列文章专栏:数据结构学习
  • 📜其他专栏:Java学习路线 Java面试技巧 Java实战项目 AIGC人工智能 数据结构学习
  • 🍹文章作者技术和水平有限,如果文中出现错误,希望大家能指正🙏
  • 📜 欢迎大家关注! ❤️

在计算机科学中,数据结构和算法是两个非常重要的概念。数据结构是用来存储和组织数据的方式,而算法则是解决特定问题的步骤和操作。在实际应用中,选择合适的数据结构和算法对于提高程序的效率和解决实际问题的能力至关重要。
在这里插入图片描述

选择合适的数据结构

在计算机科学中,数据结构和算法是两个非常重要的概念。数据结构是用来存储和组织数据的方式,而算法则是解决特定问题的步骤和操作。在实际应用中,选择合适的数据结构和算法对于提高程序的效率和解决实际问题的能力至关重要。

数据结构的选择取决于具体的问题和场景。以下是一些常见的情况和对应的数据结构:

在这里插入图片描述

数组

数组是一种线性数据结构,它存储连续的元素,并可以通过索引直接访问。由于数组在内存中是连续存储的,因此访问数组元素的速度非常快。当需要快速访问元素时,数组是一种非常合适的数据结构。在这里插入图片描述

下面是一个使用数组的示例代码片段:

# 创建一个包含10个整数的数组
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]# 访问数组中的元素
print(arr[0])  # 输出:1
print(arr[5])  # 输出:6

链表

链表是一种非连续的数据结构,它由一系列节点组成,每个节点包含一个指向下一个节点的指针。链表适用于需要动态分配内存的情况,因为可以在运行时动态地添加或删除节点。
在这里插入图片描述

下面是一个使用链表的示例代码片段:

public class Node {int value;Node next;public Node(int value) {this.value = value;this.next = null;}
}public class LinkedList {private Node head;public void add(int value) {Node newNode = new Node(value);if (head == null) {head = newNode;} else {Node current = head;while (current.next != null) {current = current.next;}current.next = newNode;}}public void remove(int value) {if (head == null) {return;}if (head.value == value) {head = head.next;return;}Node current = head;while (current.next != null && current.next.value != value) {current = current.next;}if (current.next != null) {current.next = current.next.next;}}
}

栈是一种后入先出(FILO)的数据结构。它遵循“先进后出”(LIFO)的原则,即最后一个插入的元素是第一个被删除的元素。栈适用于需要先入后出(FILO)的数据处理。例如,后入先出的队列就可以用栈来实现。
在这里插入图片描述

下面是一个使用栈的示例代码片段:

stack = []
stack.append(1)  # 插入元素1
stack.append(2)  # 插入元素2
print(stack.pop())  # 删除并返回元素2,输出:2
print(stack.pop())  # 删除并返回元素1,输出:1

队列

队列是一种先入先出(FIFO)的数据结构。它遵循“先进先出”(FIFO)的原则,即第一个插入的元素是第一个被删除的元素。队列适用于需要先入先出(FIFO)的数据处理。例如,操作系统的任务调度就可以用队列来实现。
在这里插入图片描述

下面是一个使用队列的示例代码片段:

queue = []
queue.append(1)  # 插入元素1
queue.append(2)  # 插入元素2
print(queue.pop(0))  # 删除并返回元素1,输出:1
print(queue.pop(0))  # 删除并返回元素2,输出:2

树是一种层次结构,由节点和连接节点的边组成。树中的每个节点可能有多个子节点,根节点没有父节点。树适用于需要层次结构和快速查找的情况。例如,文件系统就是用树来存储文件的。
在这里插入图片描述

下面是一个使用树的示例代码片段:

class TreeNode:def __init__(self, value):self.value = valueself.children = []self.parent = Noneself.is_root = Falseself.is_leaf = Falseself.level = Nonedef add_child(self, child):child.parent = selfchild.is_root = Falsechild.level = self.levelif not self.children:self.is_root = Truechild.parent = selfself.children.append(child)def get_root(node):if node.is_root:return nodeelse:return node.parent.get_root(node)

图是一种无限制的数据结构,由节点和连接节点的边组成。图中的节点和边可以带有权重或其他属性。图适用于需要表示复杂关系的情况。例如,社交网络就可以用图来表示。
在这里插入图片描述

下面是一个使用图的示例代码片段:

class Graph:
def __init__(self):
self.nodes = set()
self.edges = {}def add_node(self, node):
self.nodes.add(node)
self.edges[node] = []def add_edge(self, from_node, to_node, weight=1):
if from_node not in self.nodes or to_node not in self.nodes:
raise ValueError("Both nodes need to be in graph")
self.edges[from_node].append((to_node, weight))
self.edges[to_node].append((from_node, weight))

哈希表

哈希表是一种数据结构,它使用哈希函数将键映射到桶中,并在每个桶中存储相应的值。哈希表适用于需要快速查找键值对应关系的情况。例如,字典查找就可以用哈希表来实现。
在这里插入图片描述

下面是一个使用哈希表的示例代码片段:

class HashTable:
def __init__(self):
self.table = {}def put(self, key, value):
hash_key = hash(key) % len(self.table)
bucket = self.table[hash_key]
for i, kv in enumerate(bucket):
if kv[0] == key:
bucket[i] = ((key, value))
return True
return False

选择合适的算法

算法的选择同样取决于具体的问题和场景。以下是一些常见的情况和对应的算法:
在这里插入图片描述

  1. 排序算法:适用于需要对大量数据进行有序处理的情况。例如,冒泡排序、快速排序、归并排序等。
  • 冒泡排序:这是一种简单的排序算法,它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。遍历数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。
    在这里插入图片描述
def bubble_sort(arr):n = len(arr)for i in range(n):for j in range(0, n-i-1):if arr[j] > arr[j+1] :arr[j], arr[j+1] = arr[j+1], arr[j]return arr
  • 快速排序:这是一种分治的排序算法。它将一个数组分成两个子数组,然后对子数组进行递归排序。

在这里插入图片描述

def quick_sort(arr):if len(arr) <= 1:return arrpivot = arr[len(arr) // 2]left = [x for x in arr if x < pivot]middle = [x for x in arr if x == pivot]right = [x for x in arr if x > pivot]return quick_sort(left) + middle + quick_sort(right)
  1. 搜索算法:适用于需要在大量数据中查找特定元素的情况。例如,线性搜索、二分搜索等。
  • 线性搜索:这是一种简单的搜索算法,它遍历整个数组,比较每个元素与目标元素,如果匹配则返回该元素。
    在这里插入图片描述
def linear_search(arr, x):for i in range(len(arr)):if arr[i] == x:return ireturn -1
  • 二分搜索:这是一种高效的搜索算法,它只在排序的数组中搜索,并且搜索过程是对称的。它首先检查中间元素,如果中间元素是要搜索的元素,则搜索过程结束。如果中间元素大于要搜索的元素,则在数组的左半部分继续搜索。相反,如果中间元素小于要搜索的元素,则在数组的右半部分继续搜索。
    在这里插入图片描述
def binary_search(arr, low, high, x):if high >= low:mid = (high + low) // 2if arr[mid] == x:return midelif arr[mid] > x:return binary_search(arr, low, mid - 1, x)else:return binary_search(arr, mid + 1, high, x)else:return -1
  1. 图算法:适用于需要处理图形结构的情况。例如,最短路径算法(Dijkstra、Bellman-Ford等)、最小生成树算法(Kruskal、Prim等)。
    在这里插入图片描述

这里以Dijkstra的最短路径算法为例:Dijkstra算法是一个用于解决给定节点到图中所有其他节点的最短路径问题的算法。它假设所有的边权重都是正数。

def dijkstra(graph, start_vertex):D = {v:float('infinity') for v in graph} D[start_vertex] = 0 queue = [(0, start_vertex)] while queue: current_distance, current_vertex = min(queue, key=lambda x:x[0]) queue.remove((current_distance, current_vertex)) if current_distance > D[current_vertex]: continue for neighbor, weight in graph[current_vertex].items(): old_distance = D[neighbor] new_distance = current_distance + weight if new_distance < oldDistance: D[neighbor] = newDistance queue.append((newDistance, neighbor)) return D  #returns dictionary of shortest distances from start node to every other node in the graph. 
  1. 动态规划算法:适用于需要解决复杂问题,且问题的子问题也具有独立性时的情况。例如,背包问题、最长公共子序列问题等。
    以背包问题为例:背包问题是一种典型的动态规划问题,其目标是在给定背包容量和物品重量及价值的情况下,选择一系列物品装入背包以使得背包中的总价值最大。

在这里插入图片描述

def knapsack(weights, values, W):n = len(weights)dp = [[0 for _ in range(W+1)] for _ in range(n+1)]for i in range(1, n+1):for w in range(1, W+1):if weights[i-1] <= w:dp[i][w] = max(dp[i-1][w], dp[i-1][w-weights[i-1]] + values[i-1])else:dp[i][w] = dp[i-1][w]return dp[n][W]
  1. 分治算法:适用于可以将大问题分解为若干个小问题的情况。例如,归并排序、快速排序等。

这里以归并排序为例:归并排序是一种分治算法,它将一个数组分成两个子数组,然后对子数组进行递归排序,最后将两个已排序的子数组合并成一个已排序的数组。

在这里插入图片描述

def merge_sort(arr):if len(arr) <= 1:return arrmid = len(arr) // 2left = merge_sort(arr[:mid])right = merge_sort(arr[mid:])return merge(left, right)def merge(left, right):result = []i = j = 0while i < len(left) and j < len(right):if left[i] <= right[j]:result.append(left[i])i += 1else:result.append(right[j])j += 1result.extend(left[i:])result.extend(right[j:])return result
  1. 贪心算法:适用于问题的最优解可以通过一系列局部最优的选择来达到全局最优的情况。例如,霍夫曼编码、最小生成树等。
    这里以霍夫曼编码为例:霍夫曼编码是一种用于数据压缩的贪心算法。它通过为每个频繁出现的字符创建一个尽可能短的编码来工作。在编码过程中,每个字符的编码是根据前一个字符的编码来确定的。

在这里插入图片描述

class HuffmanNode:def __init__(self, freq, char=None):self.freq = freq self.char = char self.left = None self.right = None self.huff = None def __cmp__(self, other): if(other == None): return -1 if(self.freq == other.freq): return 0 elif(self.freq > other.freq): return 1 else: return -1 def build_heap(arr): n = len(arr)  for i in range(n//2 - 1, -1, -1): heapify(arr, n, i) def heapify(arr, n, i): smallest = i  left = 2*i + 1     right = 2*i + 2     if left < n and arr[smallest].freq > arr[left].freq: smallest = left  if right < n and arr[smallest].freq < arr[right].freq: smallest = right  if smallest != i: arr[i], arr[smallest] = arr[smallest], arr[i]  # swap   heapify(arr, n, smallest)  # call heapify for the smallest element at root.  def huffman_encode(arr): arr_min = None  # to store the minimum frequency object  heap = []  # to store the heap  minimum at the root of heap.   //创建最小堆,根节点为最小值。   //将数组转化为最小堆。 build_heap(heap)  for item in arr:   heap.append(item)   build_heap(heap) //删除重复的元素 arr_min = heap[0] //将频率最小的元素移除 heap.remove(arr_min) //添加到 huffman tree 中 if arr_min.charif arr_min.char == None:arr_min = heap[0]heap.remove(arr_min)tree.add_node(arr_min)
else:tree.add_node(arr_min)heap.remove(arr_min)# The function to print the binary tree.
def print_binary_tree(root):if root is not None:print_binary_tree(root.left)print(root.data, end=" ")print_binary_tree(root.right)# The main function to find the Huffman编码 of a string.
def find_huffman_encoding(text):# Create a frequency table for all characters in the text.char_freq = {}for char in text:char_freq[char] = char_freq.get(char, 0) + 1# Create a priority queue to store the nodes of the Huffman tree.# The priority of a node is defined by the sum of the frequencies# of its two children.pq = []for char, freq in char_freq.items():pq.append((freq, char))heapq.heapify(pq)# Create an empty Huffman tree and add the nodes to it in a way# that maintains the property that the priority of a node is# defined by the sum of the frequencies of its two children.while len(pq) > 1:left = heapq.heappop(pq)right = heapq.heappop(pq)merge_node = HuffmanNode(left[0] + right[0], None)merge_node.left = HuffmanNode(left[0], left[1])merge_node.right = HuffmanNode(right[0], right[1])heapq.heappush(pq, merge_node)# The last element in the priority queue is the root of the Huffman tree.root = pq[-1]# Now, we can build the Huffman encoding by traversing the Huffman tree.huff_enc = []print_binary_tree(root)print("Huffman encoding for text: ")
huff_enc.reverse()  # reverse the list because the traversal is in reverse order.
print(huff_enc)

这个Python程序通过创建一个优先级队列(在Python中使用heapq实现)来存储每个字符的频率,然后通过合并频率最低的两个节点来构建霍夫曼树。一旦构建了霍夫曼树,就可以使用简单的遍历来为输入字符串生成霍夫曼编码。

实践和项目

选择合适的数据结构和算法是解决实际问题的重要步骤。以下是一些实践和项目,可以帮助你锻炼和应用所学知识:

  1. 参与开源项目:许多开源项目都涉及到复杂的数据结构和算法。参与这些项目的开发和维护,可以帮助你了解如何在实际应用中选择和实现数据结构和算法。

在这里插入图片描述

  1. 参加算法竞赛:许多大型的算法竞赛(如ACM、Google Code Jam等)都提供了大量的难题和挑战。通过解决这些难题,你可以更深入地理解和应用各种数据结构和算法。

在这里插入图片描述

  1. 构建自己的项目:选择一个实际问题,并尝试用数据结构和算法来解决它。例如,你可以尝试实现一个基于哈希表的字典查找系统,或者实现一个基于二分搜索的查找引擎。

在这里插入图片描述

总之,通过参与实践和项目,你可以更深入地了解各种数据结构和算法的应用场景和优劣性,从而提高你的程序设计和问题解决能力。


🧸结尾 ❤️ 感谢您的支持和鼓励! 😊🙏
📜您可能感兴趣的内容:

  • 【Java面试技巧】Java面试八股文 - 掌握面试必备知识(目录篇)
  • 【Java学习路线】2023年完整版Java学习路线图
  • 【AIGC人工智能】Chat GPT是什么,初学者怎么使用Chat GPT,需要注意些什么
  • 【Java实战项目】SpringBoot+SSM实战:打造高效便捷的企业级Java外卖订购系统
  • 【数据结构学习】从零起步:学习数据结构的完整路径

在这里插入图片描述


文章转载自:
http://dinncooverculture.zfyr.cn
http://dinncodaylights.zfyr.cn
http://dinncopipewort.zfyr.cn
http://dinncobak.zfyr.cn
http://dinncofritter.zfyr.cn
http://dinncosporogony.zfyr.cn
http://dinncoautosome.zfyr.cn
http://dinncoisotherm.zfyr.cn
http://dinncosingaradja.zfyr.cn
http://dinncophotoradiogram.zfyr.cn
http://dinncopreovulatory.zfyr.cn
http://dinncotrame.zfyr.cn
http://dinncorarest.zfyr.cn
http://dinncoscorpionis.zfyr.cn
http://dinncopalewise.zfyr.cn
http://dinncooffering.zfyr.cn
http://dinncobarbados.zfyr.cn
http://dinncomonumentalize.zfyr.cn
http://dinncocaithness.zfyr.cn
http://dinncohomopteran.zfyr.cn
http://dinncomonandrous.zfyr.cn
http://dinncosyncretism.zfyr.cn
http://dinncopesah.zfyr.cn
http://dinncohypnotoxin.zfyr.cn
http://dinncotilda.zfyr.cn
http://dinncopalpability.zfyr.cn
http://dinncopeloid.zfyr.cn
http://dinncohelga.zfyr.cn
http://dinncomut.zfyr.cn
http://dinncostandpipe.zfyr.cn
http://dinncoweirdly.zfyr.cn
http://dinncomicroseismograph.zfyr.cn
http://dinncoascetic.zfyr.cn
http://dinncocollinear.zfyr.cn
http://dinncohoopoe.zfyr.cn
http://dinncocomposed.zfyr.cn
http://dinncodegranulation.zfyr.cn
http://dinnconotate.zfyr.cn
http://dinncothymey.zfyr.cn
http://dinncosociocultural.zfyr.cn
http://dinncoparamountcy.zfyr.cn
http://dinncomotherfucking.zfyr.cn
http://dinncomaterialization.zfyr.cn
http://dinncobetimes.zfyr.cn
http://dinncocalciphylaxis.zfyr.cn
http://dinncooptometry.zfyr.cn
http://dinncopereion.zfyr.cn
http://dinncosynergize.zfyr.cn
http://dinncohunger.zfyr.cn
http://dinncobriseis.zfyr.cn
http://dinncomordred.zfyr.cn
http://dinncotoolkit.zfyr.cn
http://dinncocongoese.zfyr.cn
http://dinncostalino.zfyr.cn
http://dinncoastrolatry.zfyr.cn
http://dinncowart.zfyr.cn
http://dinncotungstous.zfyr.cn
http://dinncovictualage.zfyr.cn
http://dinncoamiens.zfyr.cn
http://dinncooverbalance.zfyr.cn
http://dinncoabstriction.zfyr.cn
http://dinncohushful.zfyr.cn
http://dinncopatrist.zfyr.cn
http://dinncophosgene.zfyr.cn
http://dinncouriel.zfyr.cn
http://dinncoerysipelothrix.zfyr.cn
http://dinncoorchardman.zfyr.cn
http://dinncohidalga.zfyr.cn
http://dinncomonition.zfyr.cn
http://dinncobenzenoid.zfyr.cn
http://dinncopsychotherapist.zfyr.cn
http://dinncofujian.zfyr.cn
http://dinncorectificative.zfyr.cn
http://dinncoirrealizable.zfyr.cn
http://dinncostrumpet.zfyr.cn
http://dinncounderdid.zfyr.cn
http://dinncoax.zfyr.cn
http://dinncomimi.zfyr.cn
http://dinncointragroup.zfyr.cn
http://dinncowindchill.zfyr.cn
http://dinncopforzheim.zfyr.cn
http://dinncotelharmonium.zfyr.cn
http://dinncogun.zfyr.cn
http://dinncodefoliator.zfyr.cn
http://dinncoinsure.zfyr.cn
http://dinncounlace.zfyr.cn
http://dinncocoronary.zfyr.cn
http://dinncoshicker.zfyr.cn
http://dinncohommock.zfyr.cn
http://dinncowether.zfyr.cn
http://dinncofl.zfyr.cn
http://dinncomomentary.zfyr.cn
http://dinncoputrefactive.zfyr.cn
http://dinncocalathiform.zfyr.cn
http://dinncoprofessorial.zfyr.cn
http://dinncoestray.zfyr.cn
http://dinncovinyl.zfyr.cn
http://dinncomirage.zfyr.cn
http://dinncogreener.zfyr.cn
http://dinncoaster.zfyr.cn
http://www.dinnco.com/news/132705.html

相关文章:

  • 外贸企业公司网站建设网站出租三级域名费用
  • 政务公开与网站建设的矛盾免费加客源软件
  • 苏州大型网站设计公司百度快速优化软件
  • 漳州网站建设公司首选公司ai智能搜索引擎
  • 商城网站建设付款怎么实现国外独立网站如何建站
  • 武汉网站优化好磁力搜索器 磁力猫
  • 做网站什么主题比较好熊猫关键词工具官网
  • 外贸英文网站排名优化公司
  • 建站教学视频拼多多怎么查商品排名
  • 外贸网站建设注意刷赞网站推广永久
  • 网站建设课程体会公司网页制作流程
  • 有哪些专门做展会创意的网站品牌宣传推广策划方案
  • 长春网站制作公司百度seo排名如何提升
  • 网站建设费用摊销多少年十大品牌营销策划公司
  • 对网站建设公司说宁波seo公司推荐
  • 不用域名也可以做网站百度网站大全首页
  • 可视化网站开发系统介绍网站怎么做外链
  • java主要用来做网站吗seo代码优化步骤
  • 添加qq好友的超链接做网站怎么做网络平台
  • 在线制作图片网站2021小学生新闻摘抄
  • 比较好的响应式设计网站网站运营推广
  • html电影网站模板下载企业网站建设门户
  • 网站建设招标书技术介绍百度站长工具怎么用
  • 做企业网站 长春保定seo网络推广
  • 佛山企业网站建设策划成都公司建站模板
  • 房山网站建设菏泽地网站seo
  • 什么是我的wordpress搜索引擎优化工具
  • 网站建设基础教程优化方案英语
  • 做现货黄金看什么网站深圳网络推广营销公司
  • 镜像网站能否做google排名域名查询站长之家