首頁  >  文章  >  後端開發  >  使用Python對數組進行波形排序

使用Python對數組進行波形排序

王林
王林轉載
2023-09-15 20:45:02696瀏覽

使用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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除