# 无序数组排序后的最大相邻差(Python)

1、方法一

# 无序数组求最大相邻差

def QuickSort(arr=[],left=None,right=None):
left=0 if not isinstance(left,(int,float)) else left
right=len(arr)-1if not isinstance(right,(int,float)) else right
if left>right:
return
pivot_index=partition(arr,left,right)
QuickSort(arr,left,pivot_index-1)
QuickSort(arr,pivot_index+1,right)

return arr

def partition(arr,star_index,end_index): # 使用单边循环法
pivot=arr[star_index]
mark=star_index
for i in range(star_index+1,end_index+1):
if arr[i]<pivot:
mark += 1
arr[mark],arr[i]=arr[i],arr[mark]
arr[star_index]=arr[mark]
arr[mark]=pivot
return mark

def get_max_distance(arr=[]):
max_distance=0
for i in range(len(arr)-1):
temp_distance=arr[i+1]-arr[i]
if temp_distance>max_distance:
max_distance=temp_distance
return max_distance

if __name__=='__main__':
array=[3,44,38,5,15,36,46,2,48,19]
print(QuickSort(array))
print(get_max_distance(array))



2、方法二

# 无序数组求最大相邻差

def get_max_distance(arr=[]):
max_value=max(arr)
min_value=min(arr)
d=max_value-min_value
countArray_Length=d+1
countArray=[0]*countArray_Length
for i in range(len(arr)):
countArray[arr[i]-min_value]+= 1

# 统计连续出现0的次数
cur_time = 1
max_time = 1
for i in range(countArray_Length - 1):
if countArray[i] == 0:
if countArray[i] == countArray[i + 1]:
cur_time += 1
max_time = max(cur_time, max_time)
else:
cur_time = 1

return max_time+1

if __name__=='__main__':
array=[3,44,38,5,15,36,46,2,48,19]
print(get_max_distance(array))



3、方法三

# 无序数组求最大相邻差
class Bucket:
def __init__(self):
self.min=None
self.max=None

def get_max_distance(arr=[]):
max_value=max(arr)
min_value=min(arr)
d=max_value-min_value
# 如果d=0，则说明所有元素都相等，返回0
if d==0:
return 0
# 初始化桶
bucket_num=len(arr)
buckets=[]
for i in range(0,bucket_num):
buckets.append(Bucket()) #每个桶只记录了桶内最大值和最小值
# 遍历原始数组，确定每个桶的最大值和最小值
for i in range(0,len(arr)):
# 确定数组元素所归属的桶下标
index=int((arr[i]-min_value)*(bucket_num-1)/d)
if buckets[index].min is None or buckets[index].min>arr[i]:
buckets[index].min=arr[i]
if buckets[index].max is None or buckets[index].max<arr[i]:
buckets[index].max=arr[i]
# 遍历桶，找到最大值
left_max=buckets[0].max
max_distance=0
for i in range(1,bucket_num):
if buckets[i].min is None:
continue
if buckets[i].min-left_max>max_distance:
max_distance=buckets[i].min-left_max
left_max=buckets[i].max
return max_distance

if __name__=='__main__':
array=[3,44,38,5,15,36,46,2,48,19]
print(get_max_distance(array))



04-06 85

03-09 1万+

03-03 674

09-05 93

06-09 109

04-01 132

06-04 288

01-31 36

12-08 105

12-07 72

11-27 113

03-27 103

01-16 3386

08-19 5514

01-14 109

05-04 5814

06-17 559

12-28 607

04-14 59万+

03-13 15万+

02-19 18万+

03-04 14万+

03-06 3159

03-08 2万+

04-25 7万+

03-10 13万+

03-10 19万+

03-12 11万+

03-13 11万+

03-16 2464

03-18 1万+

03-19 8万+

03-19 4万+

03-20 1万+

03-22 5018

03-23 4万+

03-24 3万+

03-25 3万+

05-08 5万+

03-25 9万+

03-27 5万+

03-29 22万+

03-29 10万+

03-30 17万+

06-01 7062

04-02 6643

04-02 4万+

06-01 624

04-06 1万+

04-06 7万+

04-06 4481

04-09 8万+

04-09 3万+

05-17 9563

04-11 4万+

04-15 6万+

04-18 5万+

04-20 5万+

04-24 3万+

05-03 1万+

05-16 5万+

05-06 2万+

05-06 2430

05-07 4205

05-08 4万+

05-10 3880

05-11 2819

05-14 7286

05-14 1419

05-16 4131

05-16 1万+

05-17 5588

05-30 2124

05-18 2779

05-18 9148

05-18 4207

05-19 1万+

05-21 8892

#### 石锤！今年Python要过苦日子了？ 程序员：我疯了！

©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客