使用Python对数组进行波形排序

使用python对数组进行波形排序

在本文中,我们将学习一个Python程序,用于对数组进行波形排序

假设我们有一个未排序的输入数组。我们现在将以波形的方式对输入数组进行排序。如果数组 ‘arr [0..n-1]’ 满足 arr [0] >= arr [1] = arr [3] = …..,则该数组被排序为波形。

Methods Used

以下是用于完成此任务的各种方法 &miinus;

使用内置的sort()函数

立即学习“Python免费学习笔记(深入)”;

Without Using Built-in functions

Method 1: Using the Built-in sort() function

算法(步骤)

Following are the Algorithms/steps to be followed to perform the desired task. −

创建一个函数来按照波形对输入数组进行排序,接受输入数组和数组长度作为参数。

Use the sort() function(sorts the list in ascending/descending order ) to sort the input array in ascending order.

Use the for loop to traverse till the array length alternatively(step=2)

Swap the adjacent elements i.e, current and its next using the ‘,’ operator.

Create a variable to store the input array.

使用 len() 函数(返回对象中的项目数)来获取输入数组的长度。

Call the above-defined sortingInWaveform() function by passing the input array, and length of the array as arguments

使用for循环遍历数组的所有元素

Print the current element of an array.

Example

The following program sorts the input array in waveform using the python Built-in sort() function −

# creating a function to sort the array in waveform by accepting# the input array, array length as argumentsdef sortingInWaveform(inputArray, arrayLength):   # sorting the input array in ascending order using the sort() function   inputArray.sort()   # travsersing till the array length alternatively(step=2)   for k in range(0, arrayLength-1, 2):         # swapping the adjacent elements i.e, current and it's next         inputArray[k], inputArray[k+1] = inputArray[k+1], inputArray[k]# input arrayinputArray = [12, 45, 15, 4, 6, 70, 68, 3, 25]# getting the length of the input arrayarrayLength = len(inputArray)# printing the given array/listprint("The Given list is:", inputArray)# calling the above defined sortingInWaveform() function by# passing input array, length of the array as argumentssortingInWaveform(inputArray, arrayLength)print("The Result Array after sorting in wave form is:")# traversing through all the elements of the arrayfor k in range(0, arrayLength):   # printing the current element of the array/list      print(inputArray[k], end=" ")

Output

On execution, the above program will generate the following output &miinus;

The Given list is: [12, 45, 15, 4, 6, 70, 68, 3, 25]The Result Array after sorting in wave form is:4 3 12 6 25 15 68 45 70 

Time complexity − O(nLogn).

Here, the array that was given was sorted using the sort function, which typically has an O(NlogN) time complexity.

如果应用O(nLogn)的排序算法,如Merge Sort,Heap Sort等,上述给出的方法的时间复杂度为O(nLogn)。

方法2:只使用一个循环

算法(步骤)

Following are the Algorithms/steps to be followed to perform the desired task. −

Use the for loop to traverse through all the even index elements by passing 0, array length, and step value as arguments

使用if条件语句来检查当前偶数索引元素是否小于前一个元素。

Swap the elements if the condition is true.

使用 if 条件语句 来检查当前偶数索引元素是否小于下一个元素。

Swap the elements if the condition is true.

Call the above-defined sortingInWaveform() function by passing the input array, and length of the array as arguments

使用for循环遍历数组的元素。

Print the corresponding element of the array/list.

Example

The following program sorts the input array in wave form using only one for loop and without Built-in functions −

# creating a function to sort the array in waveform by accepting# the input array, array length as argumentsdef sortingInWaveform(inputArray, arrayLength):   # traversing through all the even index elements   for p in range(0, arrayLength, 2):      # checking whether the current even index element      # is smaller than the previous      if (p > 0 and inputArray[p] < inputArray[p-1]):         # swapping the elements if the condition is true            inputArray[p], inputArray[p-1] = inputArray[p-1], inputArray[p]            # checking whether the current even index element            # is smaller than the next element      if (p < arrayLength-1 and inputArray[p] < inputArray[p+1]):         # swapping the elements if the condition is true            inputArray[p], inputArray[p+1] = inputArray[p+1], inputArray[p]# input arrayinputArray = [12, 45, 15, 4, 6, 70, 68, 3, 25]# getting the length of the input arrayarrayLength = len(inputArray)print("The Given list is:", inputArray)# calling the above defined sortingInWaveform() function by# passing input array, length of the array as argumentssortingInWaveform(inputArray, arrayLength)print("The Result Array after sorting in wave form is:")# traversing through all the elements of the arrayfor k in range(0, arrayLength):   # printing the current element   print(inputArray[k], end=" ")

Output

执行上述程序后,将生成以下输出 –

The Given list is: [12, 45, 15, 4, 6, 70, 68, 3, 25]The Result Array after sorting in wave form is:45 12 15 4 70 6 68 3 25

时间复杂度 – O(n)。

Here, we didn’t use the sort function; instead, we just used the for loop to iterate through the elements of the given array, which, on average, has O(N) time complexity.

结论

在本文中,我们学习了如何使用两种不同的方法对给定的波形数组进行排序。我们使用了一种新的逻辑,它的时间复杂度比第一种方法降低了O(log N)。在许多情况下,这些类型的算法有助于减少时间复杂度并实施有效的解决方案。

以上就是使用Python对数组进行波形排序的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1342879.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月13日 05:58:08
下一篇 2025年12月13日 05:58:20

相关推荐

  • 如何使用numpy在Python中计算矩阵的迹?

    使用 Numpy 计算矩阵的迹是线性代数中的常见运算,可用于提取有关矩阵的重要信息。矩阵的迹定义为矩阵主对角线上元素的总和,主对角线从左上角延伸到右下角。在本文中,我们将学习使用 Python 中的 NumPy 库计算矩阵迹的各种方法。 在开始之前,我们首先导入 NumPy 库 – im…

    好文分享 2025年12月13日
    000
  • Python程序用于按列对2D数组进行排序

    当声明二维数组或二维数组时,它被视为矩阵。所以,我们知道矩阵由行和列组成。按升序或降序对属于矩阵特定列的元素进行排序的过程称为跨列对 2D 数组进行排序。让我们考虑一个算法和一个输入输出场景,以了解这个概念的确切应用。 输入输出场景 考虑一个二维数组。 arr = [[ 7, 9, 5, 7 ], …

    2025年12月13日
    000
  • 在Python中的网页抓取和数据提取技术

    Python 已成为各种应用程序的首选编程语言,其多功能性延伸到了网络抓取领域。凭借其丰富的库和框架生态系统,Python 提供了一个强大的工具包,用于从网站提取数据并释放有价值的见解。无论您是数据爱好者、研究人员还是行业专业人士,Python 中的网络抓取都可以成为利用大量在线信息的宝贵技能。 在…

    2025年12月13日
    000
  • Python程序打印文件中与给定模式匹配的所有模式

    在文件中查找与特定模式匹配的行是许多应用程序的典型操作,例如日志分析、文本处理和数据过滤。在本文中,我们将讨论用于打印文件中与给定模式匹配的所有模式的 python 程序。为了解决这个问题,我们首先在文件中创建一个模式来保存它。我们的任务是以编程方式创建我们在文件中看到的确切模式。通过应用一些条件,…

    2025年12月13日
    000
  • 如何使用Python在Excel中替换一个单词?

    在Python中,我们可以使用一个名为openpyxl的第三方Python库将Excel中的一个单词替换为另一个单词。Microsoft Excel是一个用于管理和分析数据的有用工具。使用Python,我们可以自动化一些Excel数据管理任务。在本文中,我们将了解如何使用Python在Excel中替…

    2025年12月13日
    000
  • 使用Python3中的Arcade绘制一个圆形

    arcade 是一个用于创建 2d 游戏和应用程序的 python 库。它是一个易于使用的库,提供各种功能来创建在屏幕上绘制形状和图像的界面。在本文中,我们将使用arcade并在python3中绘制一个圆。 安装 Arcade 在开始绘制圆圈之前,我们需要安装 Arcade 库。您可以使用 Pyth…

    2025年12月13日
    000
  • 哪个更好:C还是Python?

    在这篇文章中,我们将解释Python和C的特点以及它们的用途和区别。因此,让我们决定 python 和 C 哪个更好。 Python Python 是一种高级、面向对象、动态和多用途的编程语言,即多范式语言。 Python 的语法、动态类型和解释性使其成为一种优秀的脚本语言。 它支持多种编程范例,包…

    2025年12月13日
    000
  • 如何使用Python将文本文件的奇数行复制到另一个文件中

    在本文中,我们将向您展示如何使用Python将文本文件的奇数行复制到另一个文本文件。 假设我们获取了一个名为 TextFile.txt 的文本文件,其中包含一些随机文本。我们只需将一个文本文件的所有奇数行复制到另一个文本文件中并打印它们。 TextFile.txt Good MorningThis …

    2025年12月13日
    000
  • 如何在Python中对分组条形图进行注释?

    简介 随着数据可视化成为每个数据分析项目不可或缺的一部分,条形图成为表示分类数据的绝佳工具。当我们想要并排比较多个组时,分组条形图尤其有用。 语法和用例 可以将注释添加到条形图中,以提供附加信息或对所呈现的数据进行说明。 matplotlib的注释功能可用于将这些注释添加到每个条形图上。该函数采用以…

    2025年12月13日
    000
  • 如何使用Python使用动态数组执行Numpy广播?

    “Broadcasting” refers to how NumPy handles arrays of different dimensions during arithmetic operations. The smaller array is “broadcast&#8…

    2025年12月13日
    000
  • 生成任何图像的点状文本的Python脚本

    在数字时代,操纵图像和创造艺术效果已成为一种常见的做法。一种有趣的效果是从图像生成点状文本。此过程涉及将图像的像素转换为点图案,从而创建有趣的文本视觉表示。 在这篇博文中,我们将探索如何创建一个可以从任何给定图像生成点线文本的 Python 脚本。通过利用 Python 的强大功能和一些重要的库,我…

    2025年12月13日
    000
  • 使用Python和Rasa的聊天机器人

    聊天机器人已被公认为企业与客户互动的首选沟通工具,提供了更高效、便捷的交互方式。 Python这种因其开发资源而变得简单的编程语言已成为构建各种聊天机器人的首选。另一方面,Rasa 是一个专门的工具,专注于构建具有自然语言理解的聊天机器人。 在本文中,我们将深入研究使用 Python 和 Rasa …

    2025年12月13日
    000
  • Python程序用于从两个数组中找到不同的元素

    在编程中,数组是一种数据结构,用于存储同质数据元素的集合。数组中的每个元素都由一个键或索引值来标识。 Python 中的数组 Python 没有特定的数据类型来表示数组。相反,我们可以将 List 用作数组。 [1, 4, 6, 5, 3] 从两个数组中查找不同元素意味着识别两个给定数组之间的唯一元…

    2025年12月13日
    000
  • 在Python中的HDF5文件

    文件类型HDF5(分层数据格式5)经常用于存储和处理庞大而复杂的数据集。它是科学和工业用途的完美选择,因为它具有多功能、可扩展且有效的特点。 Python 是可用于生成、读取和修改 HDF5 文件的众多编程语言之一。在本教程中,我们将介绍如何在 Python 中使用 HDF5 文件。 安装和设置 我…

    2025年12月13日
    000
  • 在Python中的高阶函数

    简介 Python 的高阶函数世界 如果您想提高 Python 编程能力并生成更具表现力和更有效的代码,那么您来对地方了。 Python 中的函数不仅仅是专门的代码块。它们也是可以移动、转移、甚至动态生成的强大东西。通过处理其他函数,高阶函数增强了这种多功能性。 本文将广泛讨论高阶函数的原理。我们将…

    2025年12月13日
    000
  • 如何在Python中终止正在运行的Windows进程?

    深入研究 Windows 操作系统上的 Python 开发领域时,毫无疑问会出现需要的情况终止正在运行的进程。此类终止背后的动机可能涉及多种情况,包括无响应、资源消耗过多或仅仅需要停止脚本执行。在这篇综合文章中,我们将探索使用 Python 完成终止 Windows 上正在运行的进程的任务的各种方法…

    2025年12月13日
    000
  • 如何在Python中创建和自定义Venn图?

    维恩图是用来表示集合之间关系的图。要创建维恩图,我们将使用 matplotlib。 Matplotlib是一个在Python中常用的数据可视化库,用于创建交互式的图表和图形。它也用于制作交互式的图像和图表。Matplotlib提供了许多函数来自定义图表和图形。在本教程中,我们将举例说明三个示例来自定…

    2025年12月13日
    000
  • 如何在kivymd-Python中创建横幅?

    在kivymd-python中,横幅是一个向用户显示短消息或通知的图形元素。它可用于通知用户应用程序的状态,例如任务成功完成或发生错误。 横幅可以自定义颜色、文本和屏幕上的位置。它们对于空间有限且向用户快速反馈非常重要的移动应用程序特别有用。横幅可以通过提供及时的相关信息来改善整体用户体验。 横幅类…

    2025年12月13日
    000
  • Python程序打印一个数组

    单个变量和连续内存位置中的同质元素的集合称为数组。数组中的元素可以是任何数据类型,但数组中存在的所有元素应该是同类的,即应该属于相同的数据类型。 数组是一种特殊的变量,它实际上以单个变量的名称存储多个值或元素,具有连续的内存位置,准确地称为“索引”。 指数 索引一词代表索引的复数形式。索引一词表示元…

    2025年12月13日
    000
  • 深入了解Python在智能化教育中的重要作用

    随着人工智能的快速发展,智能化教育也逐渐成为了教育界的热门话题。在众多的人工智能技术中,Python语言因其简洁、易学、功能强大而备受青睐。Python在智能化教育中起着举足轻重的作用,它不仅可以用于开发智能教育应用,还可以支持教师和学生进行自主学习、编程技能的提升以及教学内容的个性化定制。 Pyt…

    2025年12月13日
    000

发表回复

登录后才能评论
关注微信