在本文中,我们将学习一个 python 程序来对波形中的数组进行排序。
假设我们采用了一个未排序的输入数组。我们现在将对波形中的输入数组进行排序。数组 'arr[0..n-1]' 以波形排序,如果 arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= .....
以下是用于完成此任务的各种方法&miinus;
以下是执行所需任务要遵循的算法/步骤。−
以下程序使用 python 内置 sort() 函数对波形中的输入数组进行排序 −
# 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=" ")
在执行时,上述程序将生成以下输出 &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
时间复杂度 − O(nLogn).
在这里,给定的数组是使用排序函数排序的,该函数通常具有 O(NlogN) 时间复杂度。
如果应用了 O(nLogn) 排序算法,如合并排序、堆排序等,则上述方法具有 O(nLogn) 时间复杂度。
以下是执行所需任务要遵循的算法/步骤。−
以下程序仅使用一个 for 循环且不带内置函数以波形对输入数组进行排序 -
# 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=" ")
在执行时,上述程序将生成以下输出 -
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)。
在这里,我们没有使用排序函数;相反,我们只是使用 for 循环来迭代给定数组的元素,平均而言,该数组具有 O(N) 时间复杂度。
在本文中,我们学习了如何使用两种不同的方法对给定的波形阵列进行排序。与第一种方法相比,O(log N)时间复杂度降低的新逻辑是我们用来降低时间复杂度的逻辑。在许多情况下,这些算法有助于降低时间复杂性并执行有效的解决方案。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有