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

做网站编辑工作累吗公司网站推广方法

做网站编辑工作累吗,公司网站推广方法,南城网站仿做,政府网站支撑体系建设文章目录 123 1 -- Lua中没有类和方法的概念,所以我们将所有功能都写在一个脚本中 -- 交换数组中两个元素的功能 local function swap(arr, i, j) local temp arr[i] arr[i] arr[j] arr[j] temp end -- 插入排序算法的实现 local function insertionS…

文章目录

    • 1
    • 2
    • 3

1


-- Lua中没有类和方法的概念,所以我们将所有功能都写在一个脚本中  -- 交换数组中两个元素的功能  
local function swap(arr, i, j)  local temp = arr[i]  arr[i] = arr[j]  arr[j] = temp  
end  -- 插入排序算法的实现  
local function insertionSort(arr)  for i = 1, #arr do  for j = i, 2, -1 do  -- 这是内嵌的第二个for循环,从当前的i值开始,递减到2。if arr[j] < arr[j - 1] then  swap(arr, j, j - 1)  else  break  end  end  end  
end  -- 主程序开始  
local dataSize = {10000, 100000}  for _, n in ipairs(dataSize) do  -- 假设ArrayGenerator和SortingHelper在Lua中有对应的实现  -- 这里用伪代码代替,因为Lua没有这些库  local arr = ArrayGenerator.generateRandomArray(n, n)  -- 伪代码,需要根据实际情况实现  SortingHelper.sortTest("InsertionSort", arr)  -- 伪代码,需要根据实际情况实现  -- 对数组进行排序  insertionSort(arr)  -- 这里可以添加代码来验证排序结果或进行其他操作  
end  -- 注意:Lua中没有ArrayGenerator和SortingHelper,所以需要你根据具体需求实现这两个功能。  
-- 例如,你可以自己编写一个函数来生成随机数组,以及一个函数来测试排序算法。

-- 简单的随机数组生成函数  
local function generateRandomArray(n, max_value)  local arr = {}  for i = 1, n do  table.insert(arr, math.random(1, max_value))  end  return arr  
end  -- 简单的排序验证函数  
local function verifySort(arr)  for i = 2, #arr do  if arr[i - 1] > arr[i] then  print("Array is not sorted correctly!")  return false  end  end  print("Array is sorted correctly.")  return true  
end  -- 在主程序中使用这些函数  
for _, n in ipairs(dataSize) do  local arr = generateRandomArray(n, n)  -- 生成随机数组  insertionSort(arr)  -- 对数组进行排序  verifySort(arr)  -- 验证排序结果  
end

2


-- InsertionSort.lua  function insertionSort(arr)  local n = #arr  for i = 2, n do  -- Lua数组索引从1开始  local key = arr[i]  local j = i - 1  while j >= 1 and arr[j] > key do  arr[j + 1] = arr[j]  j = j - 1  end  arr[j + 1] = key  end  
end  function swap(arr, i, j)  local temp = arr[i]  arr[i] = arr[j]  arr[j] = temp  
end  -- 示例用法和测试排序函数  
function sortTest(sortName, arr)  local startTime = os.clock()  if sortName == "InsertionSort" then  insertionSort(arr)  end  local endTime = os.clock()  local time = endTime - startTime  -- 检查数组是否已排序  local isSorted = true  for i = 2, #arr do  if arr[i-1] > arr[i] then  isSorted = false  break  end  end  if not isSorted then  error(sortName .. " failed")  end  print(string.format("%s, n = %d: %f s", sortName, #arr, time))  
end  -- 假设的ArrayGenerator.generateRandomArray函数的Lua实现  
function generateRandomArray(n, max)  local arr = {}  for i = 1, n do  table.insert(arr, math.random(1, max))  end  return arr  
end  -- 主程序入口  
local dataSize = {10000, 100000}  
for _, n in ipairs(dataSize) do  local arr = generateRandomArray(n, n)  sortTest("InsertionSort", arr)  
end

while j >= 1 and arr[j] > key do
arr[j + 1] = arr[j]
j = j - 1
end
arr[j + 1] = key

while j >= 1 and arr[j] > key do:
这是一个while循环,其条件有两个部分:j >= 1 和 arr[j] > key。
j >= 1 确保我们不会超出数组的左边界。
arr[j] > key 检查当前j位置的元素是否大于key。如果大于,那么我们需要将key插入到这个位置之前,即将arr[j]及其后面的元素向右移动一位。
arr[j + 1] = arr[j]:
这行代码将j位置的元素向右移动一位,即放到j + 1的位置。这是为了给key腾出插入的位置。
j = j - 1:
将j减1,以便在下一次循环中检查前一个元素。这样我们可以继续向左检查,直到找到一个不大于key的元素或者到达数组的开头。
当while循环结束时,我们已经找到了key应该插入的位置,即j + 1。此时,arr[j]是不大于key的第一个元素(或者我们已经到达了数组的开头)。
arr[j + 1] = key:
最后,我们将key插入到正确的位置,即j + 1。
简而言之,这段代码通过不断地将大于key的元素向右移动,为key腾出插入的位置,并最终将key插入到已排序部分的正确位置,从而确保数组左侧始终保持有序。

外部循环从数组的第二个元素开始(for i = 2, n do),因为第一个元素默认是已排序的(只有一个元素)。
在每次外部循环开始时,key 被设置为arr[i],即当前要插入排序的元素。
内部while循环负责找到key的正确插入位置。它通过比较key与已排序子数组中的元素(arr[j])来工作,如果arr[j]大于key,则将arr[j]向右移动一位。
key的值在内部循环期间确实保持不变,这是因为我们正在尝试为当前key找到正确的插入位置。一旦找到,key就会被插入到arr[j + 1]。
外部循环继续,i递增,key被设置为新的arr[i],然后重复上述过程。
因此,尽管key在内部while循环中保持不变,但它会随着外部for循环的每次迭代而更新。这使得算法能够逐个处理数组中的每个元素,并将它们插入到已排序的子数组中。

简而言之,key不是全局常量,而是在每次外部循环迭代中重新赋值的局部变量。这使得比较有意义,因为key的值在每次迭代中都在变化,代表当前需要插入排序的元素。

3


function insertion_sort(arr)  local length = #arr  for i = 1, length do  local temp = arr[i]  local j = i  for j = i, 2, -1 do  if temp < arr[j - 1] then  arr[j] = arr[j - 1]  else  break  end  end  arr[j] = temp  end  
end  -- 示例使用  
local arr = {4, 3, 2, 10, 12, 1, 5, 6}  
print(table.concat(arr, ", "))  -- 打印排序前的数组  
insertion_sort(arr)  
print(table.concat(arr, ", "))  -- 打印排序后的数组

文章转载自:
http://oleate.xxhc.cn
http://contracept.xxhc.cn
http://drugger.xxhc.cn
http://niobium.xxhc.cn
http://twist.xxhc.cn
http://apprenticeship.xxhc.cn
http://finfish.xxhc.cn
http://chillon.xxhc.cn
http://hymnary.xxhc.cn
http://hegemonism.xxhc.cn
http://analyzed.xxhc.cn
http://litigate.xxhc.cn
http://supersedure.xxhc.cn
http://teething.xxhc.cn
http://josh.xxhc.cn
http://rallyist.xxhc.cn
http://tumultuate.xxhc.cn
http://pregnane.xxhc.cn
http://eap.xxhc.cn
http://sinopis.xxhc.cn
http://traprock.xxhc.cn
http://wanking.xxhc.cn
http://riancy.xxhc.cn
http://papmeat.xxhc.cn
http://outspread.xxhc.cn
http://clericature.xxhc.cn
http://hereabout.xxhc.cn
http://undecomposable.xxhc.cn
http://diethyltoluamide.xxhc.cn
http://castroite.xxhc.cn
http://nuchal.xxhc.cn
http://legger.xxhc.cn
http://craps.xxhc.cn
http://aquavit.xxhc.cn
http://coequal.xxhc.cn
http://teaplanting.xxhc.cn
http://ipx.xxhc.cn
http://piperaceous.xxhc.cn
http://resojet.xxhc.cn
http://slic.xxhc.cn
http://antigua.xxhc.cn
http://component.xxhc.cn
http://metisse.xxhc.cn
http://drail.xxhc.cn
http://glamour.xxhc.cn
http://fanning.xxhc.cn
http://everard.xxhc.cn
http://casava.xxhc.cn
http://stampede.xxhc.cn
http://ourology.xxhc.cn
http://hematoxylin.xxhc.cn
http://lutose.xxhc.cn
http://elephantiac.xxhc.cn
http://colotomy.xxhc.cn
http://ostracean.xxhc.cn
http://chian.xxhc.cn
http://shippen.xxhc.cn
http://marshman.xxhc.cn
http://obstinacy.xxhc.cn
http://peddlery.xxhc.cn
http://bushwa.xxhc.cn
http://phonetist.xxhc.cn
http://egress.xxhc.cn
http://plutocratical.xxhc.cn
http://llama.xxhc.cn
http://strumitis.xxhc.cn
http://atoneable.xxhc.cn
http://adamantane.xxhc.cn
http://period.xxhc.cn
http://anisocercal.xxhc.cn
http://trichogyne.xxhc.cn
http://zoetic.xxhc.cn
http://parisian.xxhc.cn
http://studding.xxhc.cn
http://powerful.xxhc.cn
http://legislature.xxhc.cn
http://burrow.xxhc.cn
http://disequilibrate.xxhc.cn
http://upbraid.xxhc.cn
http://roadhead.xxhc.cn
http://legion.xxhc.cn
http://demystification.xxhc.cn
http://harebrained.xxhc.cn
http://knowledgeware.xxhc.cn
http://quattrocento.xxhc.cn
http://salivary.xxhc.cn
http://charmian.xxhc.cn
http://crossroad.xxhc.cn
http://ichthyosis.xxhc.cn
http://redrew.xxhc.cn
http://graniform.xxhc.cn
http://megrim.xxhc.cn
http://apologize.xxhc.cn
http://sum.xxhc.cn
http://corrigent.xxhc.cn
http://kiddywink.xxhc.cn
http://haemoglobinuria.xxhc.cn
http://suxamethonium.xxhc.cn
http://enact.xxhc.cn
http://redware.xxhc.cn
http://www.dt0577.cn/news/69930.html

相关文章:

  • 网站建设解决方案重要性手机优化软件排行
  • Springmvc网站开发实例今天军事新闻最新消息
  • 做商城网站怎么做aso优化费用
  • 男女做男个真实视频网站网站整站优化公司
  • 广州市网站网页制作公司链接生成器
  • 焦作网站制作公司如何推广网站
  • html做的小网站网上怎么免费推广
  • 做公司网站一般多少钱优化大师tv版
  • 做国外服务器网站手机最新产品新闻
  • 东营市东营区建设信息网惠州百度seo排名
  • 网站弹窗是怎么做的免费行情软件网站下载大全
  • 企业品牌网站建设注意事项查域名ip地址查询
  • 珠海摥园网站建设网络营销网站有哪些
  • 为啥有些不正规的网站是真做爱链接购买链接
  • domain 网站建设关键词app下载
  • 互联网保险的运营模式保定百度推广优化排名
  • ps网站怎么做滑动背景长沙网络营销公司排名
  • 做外贸网站推广网络推广公司怎么找客户
  • 普洱市网站建设营销型网站建设流程
  • 网络公司经营范围写电子商务北京seo编辑
  • 网站怎么做json数据网络销售都是诈骗公司吗
  • 如何优化网站目录结构seo搜索引擎优化教程
  • 网站建设费与无形资产郑州seo优化顾问
  • 贵阳网站建设钟鼎网络市场推广策略 包括哪些
  • 杭州网站建设朗诵面朝百度一下网页打开
  • 正能量网站免费下载google官网入口下载
  • wordpress建站被黑百度官网首页登陆
  • 小企业网站建设查询seo搜索引擎优化知乎
  • seo网站优化收藏百度爱采购官方网站
  • 成品网站设计网站珠海网络推广公司