使用Python对数组进行波形排序
在本文中,我们将学习一个Python程序,用于对数组进行波形排序。
假设我们有一个未排序的输入数组。我们现在将以波形的方式对输入数组进行排序。如果数组 'arr [0..n-1]' 满足 arr [0] >= arr [1] = arr [3] = .....,则该数组被排序为波形。
Methods Used
以下是用于完成此任务的各种方法 &miinus;
使用内置的sort()函数
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 arguments def 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 array inputArray = [12, 45, 15, 4, 6, 70, 68, 3, 25] # getting the length of the input array arrayLength = len(inputArray) # printing the given array/list print("The Given list is:", inputArray) # calling the above defined sortingInWaveform() function by # passing input array, length of the array as arguments sortingInWaveform(inputArray, arrayLength) print("The Result Array after sorting in wave form is:") # traversing through all the elements of the array for 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 arguments def 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 array inputArray = [12, 45, 15, 4, 6, 70, 68, 3, 25] # getting the length of the input array arrayLength = len(inputArray) print("The Given list is:", inputArray) # calling the above defined sortingInWaveform() function by # passing input array, length of the array as arguments sortingInWaveform(inputArray, arrayLength) print("The Result Array after sorting in wave form is:") # traversing through all the elements of the array for 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对数组进行波形排序的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

PHP和Python各有优劣,选择取决于项目需求和个人偏好。1.PHP适合快速开发和维护大型Web应用。2.Python在数据科学和机器学习领域占据主导地位。

在CentOS系统上高效训练PyTorch模型,需要分步骤进行,本文将提供详细指南。一、环境准备:Python及依赖项安装:CentOS系统通常预装Python,但版本可能较旧。建议使用yum或dnf安装Python3并升级pip:sudoyumupdatepython3(或sudodnfupdatepython3),pip3install--upgradepip。CUDA与cuDNN(GPU加速):如果使用NVIDIAGPU,需安装CUDATool

Docker利用Linux内核特性,提供高效、隔离的应用运行环境。其工作原理如下:1. 镜像作为只读模板,包含运行应用所需的一切;2. 联合文件系统(UnionFS)层叠多个文件系统,只存储差异部分,节省空间并加快速度;3. 守护进程管理镜像和容器,客户端用于交互;4. Namespaces和cgroups实现容器隔离和资源限制;5. 多种网络模式支持容器互联。理解这些核心概念,才能更好地利用Docker。

在CentOS系统上启用PyTorchGPU加速,需要安装CUDA、cuDNN以及PyTorch的GPU版本。以下步骤将引导您完成这一过程:CUDA和cuDNN安装确定CUDA版本兼容性:使用nvidia-smi命令查看您的NVIDIA显卡支持的CUDA版本。例如,您的MX450显卡可能支持CUDA11.1或更高版本。下载并安装CUDAToolkit:访问NVIDIACUDAToolkit官网,根据您显卡支持的最高CUDA版本下载并安装相应的版本。安装cuDNN库:前

Python和JavaScript在社区、库和资源方面的对比各有优劣。1)Python社区友好,适合初学者,但前端开发资源不如JavaScript丰富。2)Python在数据科学和机器学习库方面强大,JavaScript则在前端开发库和框架上更胜一筹。3)两者的学习资源都丰富,但Python适合从官方文档开始,JavaScript则以MDNWebDocs为佳。选择应基于项目需求和个人兴趣。

在CentOS下选择PyTorch版本时,需要考虑以下几个关键因素:1.CUDA版本兼容性GPU支持:如果你有NVIDIAGPU并且希望利用GPU加速,需要选择支持相应CUDA版本的PyTorch。可以通过运行nvidia-smi命令查看你的显卡支持的CUDA版本。CPU版本:如果没有GPU或不想使用GPU,可以选择CPU版本的PyTorch。2.Python版本PyTorch

MinIO对象存储:CentOS系统下的高性能部署MinIO是一款基于Go语言开发的高性能、分布式对象存储系统,与AmazonS3兼容。它支持多种客户端语言,包括Java、Python、JavaScript和Go。本文将简要介绍MinIO在CentOS系统上的安装和兼容性。CentOS版本兼容性MinIO已在多个CentOS版本上得到验证,包括但不限于:CentOS7.9:提供完整的安装指南,涵盖集群配置、环境准备、配置文件设置、磁盘分区以及MinI

CentOS 安装 Nginx 需要遵循以下步骤:安装依赖包,如开发工具、pcre-devel 和 openssl-devel。下载 Nginx 源码包,解压后编译安装,并指定安装路径为 /usr/local/nginx。创建 Nginx 用户和用户组,并设置权限。修改配置文件 nginx.conf,配置监听端口和域名/IP 地址。启动 Nginx 服务。需要注意常见的错误,如依赖问题、端口冲突和配置文件错误。性能优化需要根据具体情况调整,如开启缓存和调整 worker 进程数量。
