Please upload a word Doc that contains either one method to solve the following problem:
•Pseudo code describing how to write the code
•Code in Python (or java)
Task:Sort a random array of items alphabetically.Do not call the library function sort in Python,write your own code using comparators and +,-,*,/operators
这是让你上传一个word文档,包含伪代码描述如何写代码
python或者java代码
任务是对随机数组按照字母表排序,不要调用系统库函数,只能用运算符。
def compare_strings(string1, string2):
string1 = string1.lower()
string2 = string2.lower()
for i in range(min(len(string1), len(string2))):
if string1[i] < string2[i]:
return -1
elif string1[i] > string2[i]:
return 1
if len(string1) < len(string2):
return -1
elif len(string1) > len(string2):
return 1
return 0
def custom_sort(arr):
n = len(arr)
for i in range(n - 1):
for j in range(0, n - i - 1):
if compare_strings(arr[j], arr[j + 1]) > 0:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
return arr
random_array = ["Apple", "banana", "cherry", "Date", "elderberry"]
sorted_array = custom_sort(random_array)
print(sorted_array)
中国人 就要用 汉语来问问题 , 你这问题是免费的,然后俺还去翻译一下,然后再去写答案, 你认为合适吗?
翻译:
代码如图,如有帮助给个采纳谢谢 :
代码 :
def bubbleSort(arr):
n = len(arr)
for i in range(n - 1):
for j in range(0, n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
# 测试代码
items = ['apple', 'banana', 'cherry', 'date', 'elderberry']
bubbleSort(items)
print("排序后的数组:", items)