【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】如何使用Python实现冒泡排序算法
- 如何使用Python实现冒泡排序算法?冒泡排序算法是一种简单但有效的排序算法,它的思想是不断比较相邻的两个元素,如果它们的顺序不正确,就将它们交换位置,直到整个序列都排好序为止。下面将通过具体的代码示例来演示如何使用Python实现冒泡排序算法。def bubble_sort(arr): n = len(arr) # 外层循环控制比较的轮数 &nbs
- 【Python】pythonGUI写一个exe桌面应用程序
- 一、整体步骤1、安装pyinstaller 3.02、安装wxpython3、安装布局工具wxFormBuilder4、将png生成icon5、upx391w(打包成exe程序)二、工具安装安装布局工具(wxFormBuilder_v3.5.1-rc1.exe)下载地址:http://sourceforge.net/projects/wxformbuilder/files/wxformbuilder/3.1.70/教程地址:https://www.cnblogs.com/jikeboy/p/56
- 【Python】用matplotlib实现数据集散点图的实际应用
- 实战演练:利用Matplotlib绘制数据集的散点图Matplotlib是Python中常用的绘图库之一,它提供了丰富的功能,可以绘制各种类型的图表。其中,散点图是一种常用的数据可视化方式,用于展示两个变量之间的关系。本文将介绍如何利用Matplotlib绘制数据集的散点图,并附上具体的代码示例。首先,我们需要安装Matplotlib库。可以使用pip命令执行以下语句安装:pip install matplotlib安装完成后,我们可以导入Matplotlib库并开始绘制散点
- 【Python】如何升级Python的pip工具
- span style="text-wrap: wrap;">解决常见问题:Python升级pip的实用指南导言:Python是一种流行的高级编程语言,拥有强大的生态系统和广泛的第三方库。而pip是Python的默认包管理工具,用于安装和管理Python包。然而,随着时间的推移,pip的版本可能会变得过时,不支持某些新功能或存在安全漏洞。为了确保我们能够得到最新的功能和修复的漏洞,我们需要升级pip。本文将为您提供一些实用的指南和具体的代码示例。一、使用命令行升级pip打开命令行工具(Windows用户可以使用cmd或PowerShell,macOS或Li</span
- 【Python】快速入门Flask框架:构建简单而灵活的Web应用
- 快速入门Flask框架:构建简单而灵活的Web应用Flask是一个基于Python编程语言的轻量级Web应用框架。它简单而灵活,使得开发者可以快速构建Web应用。Flask提供了核心功能,同时也是一个扩展性强大的框架,通过插件可以实现更多的功能。本篇文章将介绍Flask框架的快速入门,并通过具体的代码示例让读者更加深入理解。一、Flask的安装与环境配置首先,我们需要安装Flask。使用pip命令可以方便地进行安装,打开命令行窗口并输入以下命令:pip install fla
- 【Python】Pandas轻松读取SQL数据库中的数据
- 数据处理利器:Pandas读取SQL数据库中的数据,需要具体代码示例随着数据量的不断增长和复杂性的提高,数据处理成为了现代社会中一个重要的环节。在数据处理过程中,Pandas成为了许多数据分析师和科学家们的首选工具之一。本文将介绍如何使用Pandas库来读取SQL数据库中的数据,并提供一些具体的代码示例。Pandas是基于Python的一个强大的数据处理和分析工具。它提供了丰富的数据结构,如Series和DataFrame,以及各种各样的功能,例如数据清洗、过滤、统计、可视化等。同时,Panda
- 【Python】PyQt5设置窗口宽高
- 在PyQt中,设置窗口(例如QMainWindow或QWidget)的宽度和高度非常简单。你可以通过修改窗口的size属性或使用setFixedSize()和resize()方法来达到目的。以下是几种常见的方法:
- 【Python】深入探究len函数在Python中的实现原理:深入理解其底层机制
- 深入理解Python中的len函数:掌握其底层实现原理,需要具体代码示例引言:Python是一门简洁、易读、容易上手的编程语言。在Python中,len()函数是一种非常常用的内置函数,用于返回某个容器对象(如字符串、列表、元组等)的元素个数。虽然len()函数看似简单,但深入理解其底层实现原理对于提升我们对Python的理解和能力是非常重要的。本文将介绍len()函数的底层实现原理,以及提供具体的代码示例,帮助读者深入理解。一、len()函数的基本用法在开始深入了解len()函数的底层实现原理