【Python】使用Python对数组进行波形排序
在本文中,我们将学习一个Python程序,用于对数组进行波形排序。
假设我们有一个未排序的输入数组。我们现在将以波形的方式对输入数组进行排序。如果数组 'arr [0..n-1]' 满足 arr [0] >= arr [1] <= arr [2] >= arr [3] <= arr [4] >= .....,则该数组被排序为波形。
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】解析matplotlib散点图绘制的简明步骤
- 快速入门:matplotlib散点图绘制步骤解析引言:matplotlib是一个强大的Python数据可视化库,可用于绘制各种类型的图表。其中,散点图是一种常用的图表类型,用于展示数据点之间的关系。本文将介绍使用matplotlib绘制散点图的步骤,以及附带具体的代码示例,帮助读者快速入门。步骤一:导入所需库首先,我们需要导入matplotlib库以及其他可能需要使用的库。在Python代码中,使用import关键字来导入所需库,如下所示:import matplotlib.pyplo
- 【Python】如何在Python中进行图形界面设计和开发
- 如何在Python中进行图形界面设计和开发引言:Python是一种功能强大且易于学习的编程语言,广泛应用于各种领域,包括图形界面设计和开发。Python提供了不少图形库和工具,使得开发者能够轻松地创建具有吸引力的用户界面。本文将介绍如何在Python中进行图形界面设计和开发,并提供一些实际的代码示例。一、图形库的选择Python提供了多个图形库,每个库都有自己的特点和用途。以下是其中几个常用的图形库:Tkinter:Tkinter是Python的标准图形库,它是Python内置的Tk界面工具集。
- 【Python】自定义颜色在Matplotlib柱形图绘制中的应用
- 使用Matplotlib库绘制柱形图时如何自定义颜色Matplotlib是一个功能强大、灵活且易于使用的Python绘图库,可以绘制各种类型的图形,包括柱形图。默认情况下,Matplotlib会自动为柱形图生成一组不同颜色的条形,但是有时候我们需要自定义每个柱形的颜色,以满足特定的需求。下面是一些具体的示例代码,演示如何使用Matplotlib自定义柱形图的颜色:import matplotlib.pyplot as plt # 自定义颜色
- 【Python】使用Python中的len函数统计文本中的单词数量的示例
- Python中的len函数应用实例:如何利用它统计文本中的单词数量在Python编程中,len函数是一个非常有用的函数,它用于返回一个对象的长度或元素的个数。在本文中,将介绍如何使用len函数来统计文本中的单词数量,并提供具体的代码示例。在开始编写代码之前,需要先了解一下如何定义一个单词。在本文中,我们将使用空格作为单词的分隔符,也就是说,任何两个空格之间的字符串都被认为是一个单词。下面是一个简单的代码示例,展示了如何使用len函数统计文本中的单词数量:def count_words(
- 【Python】Python中的字符串查找和替换效率最高的方法是哪个?
- Python中的字符串查找和替换效率最高的方法是哪个?在Python中,字符串是常用的数据类型之一,我们经常需要对字符串进行查找和替换操作。那么,在进行字符串查找和替换时,有哪些方法是效率最高的呢?本文将为你介绍Python中字符串查找和替换的几种常见方法,并比较它们的效率。使用in操作符进行查找使用in操作符可以快速判断一个字符串是否在另一个字符串中出现。例如,我们可以使用如下代码判断字符串"abc"是否在字符串"abcdefg"中出现:if
- 【Python】Python程序将本地时间转换为GMT时间
- 当我们创建一个允许世界各地的用户预订活动的 Web 服务时,我们可能会使用此程序将每个用户的当地时间转换为 GMT,然后再将其放入数据库中。这将使不同时区的用户更容易比较和显示事件时间。不同时区的用户更容易比较和显示事件时间。在 Python 中,我们有一些内置的时间函数,如 timezone()、localize()、now() 和 astimezone(),可用于将本地时间转换为 GMT。当地时间代表当前时间,而 GMT 是通过计算本初子午线定义的。 GMT 代表格林威治标准时间,但现在称为
- 【Python】Python编程初学者的指南-从零开始
- 从零开始的Python入门代码指南Python是一种简单易用且功能强大的编程语言,非常适合初学者入门。本文将为你提供一个从零开始的Python代码指南,帮助你理解Python基础知识,并提供具体代码示例,以帮助你快速上手。安装Python首先,你需要在你的电脑上安装Python。你可以访问官方网站https://www.python.org/downloads/下载最新版本的Python,并按照安装向导进行安装。编写第一个Python程序现在,让我们编写你的第一个Python程序,打开你喜欢的文
- 【Python】Python装饰器的常见用途是什么?
- 在本文中,我们将学习Python装饰器的常见用法Python装饰器是什么?Python装饰器是一段代码,允许对现有函数进行添加或更新,而不必更改底层函数定义。当程序运行时,它尝试编辑自身的另一部分,这被称为元编程。装饰器是一种函数类型,它接受一个函数并返回另一个函数,或者接受一个类并返回另一个类。它可以是任何可调用的(函数、类、方法等),并且可以返回任何内容;它也可以采用一个方法。Python 装饰器使用起来很简单。装饰器接受一个可调用对象,该对象实现了特殊方法__call()__,被称为可调用