输入整型数组和排序标识,对其元素按照升序或降序进行排序
数据范围: ,元素大小满足
第一行输入数组元素个数第二行输入待排序的数组,每个数用空格隔开第三行输入一个整数0或1。0代表升序排序,1代表降序排序
输出排好序的数字
8 1 2 4 9 3 55 64 25 0
1 2 3 4 9 25 55 64
5 1 2 3 4 5 1
5 4 3 2 1
while True: try: n=int(input())#读取没用的玩意儿 val=input().split()#以空格区分数组 updown= input()#读取升序降序 val = list(map(int, val))#转换元素为int if updown == '0':#为降序时 val.sort() elif updown == '1':#为升序时 val.sort(reverse= True) val = list(map(str, val))#重组空格隔开 val=' '.join(val) print(val) except: break
while True: try: m=input() n=list(map(int,input().split())) type=int(input()) if type==0: x=sorted(n) if type==1: x=sorted(n,reverse=True) #这是降序 print(*x) except: break
def quick_sort(nums, left, right): if left >= right: return mid = (left+right) // 2 nums[right], nums[mid] = nums[mid], nums[right] devide_index, devide = left, nums[right] for i in range(left, right): if nums[i] < devide: nums[devide_index], nums[i] = nums[i], nums[devide_index] devide_index += 1 nums[right], nums[devide_index] = nums[devide_index], nums[right] quick_sort(nums, left, devide_index-1) quick_sort(nums, devide_index+1, right) while True: try: n, nums, reverse = int(input()), list(map(int, input().split())), int(input()) quick_sort(nums, 0, n-1) if reverse: nums = nums[::-1] print(" ".join(map(str, nums))) except: break
# 方法一: while True: try: count, arr = input(), map(int, input().strip().split(' ')) flag = input().strip() if flag == '0': arr = sorted(arr) # print(arr) if flag == '1': arr = sorted(arr, reverse=True) # print(arr) print(' '.join(map(str, arr))) except: break # 方法二:(reverse=True可以直接写成reverse=1) while True: try: count, arr = input(), map(int, input().strip().split(' ')) flag = int(input().strip()) arr = sorted(arr, reverse=flag) print(' '.join(map(str, arr))) except: break
while True: try: def quick_sort(arr, left, right): """快速排序""" if left >= right: return arr key = arr[left] low = left high = right while left < right: while left < right and arr[right] >= key: right -= 1 arr[left] = arr[right] while left < right and arr[left] <= key: left += 1 arr[right] = arr[left] arr[right] = key quick_sort(arr, low, left - 1) quick_sort(arr, left + 1, high) return arr total_num = int(input()) input_list = list(map(int, input().strip().split())) descending = int(input()) if len(input_list) == 1: print(input_list[0]) if descending == 0: print(" ".join(list(map(str, quick_sort(input_list, 0, len(input_list) - 1))))) else: print(" ".join(list(map(str, quick_sort(input_list, 0, len(input_list) - 1)[::-1])))) except: break
快速排序实现基本功能,通过列表索引实现逆序
while True: try: n = int(input()) b = list(map(int,input().split())) m = input() if m == '1': b = sorted(b, reverse = True) if m == '0': b = sorted(b, reverse = False) res = '' for i in b: res +=''.join(str(i)) res += ''.join(' ') print(res) except: break
while True: try: fn = [] f = "" n = int(input()) lst = input().split() d = int(input()) for i in lst: fn.append(int(i)) if d == 0: final = sorted(fn) for i in final: f += " " + str(i) print (f[1:]) elif d == 1: final = sorted(fn, reverse = True) for i in final: f += " " + str(i) print (f[1:]) except: break
while True: try: num_data=int(input())#----个数 data=input().strip().split()#----输入的单行数据时 sort_flag=int(input())#----------排序方式 for i in range(num_data):#-------冒泡排序时间长 for j in range(1,num_data-i): if int(data[j-1])>int(data[j]): data[j-1],data[j]=data[j],data[j-1] #print(' '.join(data)) if sort_flag==0: print(' '.join(data))#------升序 else: print(' '.join(data[::-1]))#---降序 except: break