如果下一个元素大于当前,我希望在arr中更改顺序。 热修改代码,所以它会起作用吗?
arr = [5, 22, 29, 39, 19, 51, 78, 96, 84]
i = 0
while (i < arr.size-1)
if arr[i].to_i < arr[i+1].to_i
arr[i]
elsif arr[i].to_i > arr[i + 1].to_i
arr[i+1], arr[i] = arr[i], arr[i+1]
end
puts arr[i]
i += 1
end
返回:[5,22,29,39,19,51,78,96,84] 相反:[5,19,22,29,39,51,78,84,96]
答案 0 :(得分:0)
请注意,在排序时,您要重新排列数组。请勿对其进行修改,将其用作参考,并将已排序的项目放在新数组中。
答案 1 :(得分:0)
您可以使用任何排序算法,具体取决于数组(n)的大小,
For Bubble Sort, Time Complexity is O(n^2)
For Merge Sort, Time Complexity is O(nlogn)
For Counting Sort, Time Complexity is O(n) but number in array must be 0.upto 10^6
冒泡排序:它在一次迭代中成对运行并将最大元素放在最后,在第二次迭代中,将第二个最大元素放在倒数第二个位置,依此类推,直到数组排序。
迭代(n-1)次[找到(n-1)个最大数字]
迭代(n-idx-1)次以换取一对数字if(第一个数字为 大于下一个号码)
如果在内循环中停止交换意味着该数组已排序, 所以打破外循环
Ruby代码:
def bubble_sort(arr)
n = arr.size
(n-1).times do |idx|
swapped = false
(n-idx-1).times do |i|
if arr[i] > arr[i+1]
arr[i], arr[i+1] = arr[i+1], arr[i]
swapped = true
end
end
break unless swapped
end
arr
end
p bubble_sort([5, 22, 29, 39, 19, 51, 78, 96, 84])
合并排序:它运行在divide and conquer
策略上,即如果您知道两半的数组已排序,那么您可以使用O中的two pointer
策略对整个数组进行排序( n)中。
For Instance,
#first half : [4,5,7,9]
#second half : [1,2,10,15]
1. Take two pointer l and r assigned to starting index of both halves i.e 0
2. Iterate over l and r upto their lengths to consume both arrays
if element at first_half[l] < second_half[r]
Put first_half[l] in result_array
Increment l pointer
else
Put second_half[r] in result_array
Increment r pointer
此合并操作将使用O(n)对整个数组进行排序。
现在,如果我们将整个数组递归分成两半,我们将获得高度log(n)的二叉树,每个级别将采用O(n)对子问题(一半)进行排序,从而得到O(nlogn)时间复杂性。
Base case would be : single element array is always sorted
Ruby代码:
def merge(left_sorted, right_sorted)
res = []
left_size, right_size = left_sorted.size, right_sorted.size
l = r = 0
loop do
break if r == right_size and l == left_size # break if both halves processed
if r == right_size or (l < left_size and left_sorted[l] < right_sorted[r])
res << left_sorted[l]; l += 1
else
res << right_sorted[r]; r += 1
end
end
res
end
def merge_sort(arr)
size = arr.size
return arr if size <= 1 # base case
mid = arr.size/2 - 1
left_half, right_half = arr[0..mid], arr[mid+1..-1]
left_sorted = merge_sort(left_half)
right_sorted = merge_sort(right_half)
return merge(left_sorted, right_sorted)
end
p merge_sort([5, 22, 29, 39, 19, 51, 78, 96, 84])
计数排序:如果数组中的数字位于范围内(0..10 ^ 6)
,则通过计算数组中的数字来计算O(n)Ruby代码:
def counting_sort(arr)
min, max = arr.min, arr.max
count_arr = [0] * (max - min + 1) # initialize count_array with all 0s
arr.each do |num|
count_arr[num - min] += 1
end
res = []
size = count_arr.size
size.times do |i|
count_arr[i].times do
res << i + min
end
end
res
end
p counting_sort([5, 22, 29, 39, 19, 51, 78, 96, 84])
答案 2 :(得分:-2)
如果你想学习algotirthms,请使用C或C ++。
def bubble_sort(array)
sorted = array.dup
i = 0
l = sorted.length
while i < (l - 1)
j = 0
while j < l - i - 1
if sorted[j] > sorted[j + 1]
tmp = sorted[j]
sorted[j] = sorted[j + 1]
sorted[j + 1] = tmp
end
j += 1
end
i += 1
end
sorted
end
puts bubble_sort([5, 22, 29, 39, 19, 51, 78, 96, 84])