在本文中,我們將學習一個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中文網其他相關文章!

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于Seaborn的相关问题,包括了数据可视化处理的散点图、折线图、条形图等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于进程池与进程锁的相关问题,包括进程池的创建模块,进程池函数等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于简历筛选的相关问题,包括了定义 ReadDoc 类用以读取 word 文件以及定义 search_word 函数用以筛选的相关内容,下面一起来看一下,希望对大家有帮助。

VS Code的确是一款非常热门、有强大用户基础的一款开发工具。本文给大家介绍一下10款高效、好用的插件,能够让原本单薄的VS Code如虎添翼,开发效率顿时提升到一个新的阶段。

pythn的中文意思是巨蟒、蟒蛇。1989年圣诞节期间,Guido van Rossum在家闲的没事干,为了跟朋友庆祝圣诞节,决定发明一种全新的脚本语言。他很喜欢一个肥皂剧叫Monty Python,所以便把这门语言叫做python。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于数据类型之字符串、数字的相关问题,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于Python的相关知识,其中主要介绍了关于numpy模块的相关问题,Numpy是Numerical Python extensions的缩写,字面意思是Python数值计算扩展,下面一起来看一下,希望对大家有帮助。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

Dreamweaver Mac版
視覺化網頁開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。