Python algorithm optimization exercises

topic

Receive the 3 numbers input by the user in sequence, print them after sorting.

1-After converting int, judge size order
nums = []
for i in range(3):
    nums.append(int(input('{}:'.format(i))))

if nums[0]>nums[i]:
    if nums[0] > nums[2]:
        i3=nums[0]
        if nums[1]>nums[2]:
            i2=nums[1]
            i1=nums[2]
        else:
            i2=nums[2]
            i1=nums[1]
    else:
        i2=nums[0]
        i3=nums[2]
        i1=nums[1]
else:
    if nums[0]>nums[2]:
        i3=nums[1]
        i2=nums[0]
        i1=nums[2]
    else:
        if nums[1]<nums[2]:
            i1 = nums[0]
            i2 = nums[1]
            i3 = nums[2]
        else:
            i1 = nums[0]
            i2 = nums[2]
            i3 = nums[3]
print(i1,i2,i3)
2-Optimization
nums = []
for i in range(3):
    nums.append(int(input('{}:'.format(i))))

if nums[0] > nums[1]:
    if nums[0] > nums[2]:
        if nums[1] > nums[2]:
            out = [2,1,0]
        else:
            out = [1,2,0]
else:
    if nums[0] > nums[2]:
        out=[2,0,1]
    else:
        if nums[1] < nums[2]:
            out = [0,1,2]
        else:
            out = [0,2,1]
out.reverse() 通过这里,可以控制从小到大升序,从大到小降序
for i in out:
    print(nums[i],end="")
3-Use the max function
# 用最大值函数max、或者for循环,都是可以的
nums = []
for i in range(3):
    nums.append(int(input('{}:'.format(i))))

while True:
    cur = min(nums)
    print(cur)
    nums.remove(cur)
    if len(nums) == 1
        print(nums[0])
        break
4-List sort function
nums = []
for i in range(3):
    nums.append(int(input('{}:'.format(i))))

nums.sort(reverse=False)
print(nums)
5-Bubbling

Using the index, the exchange sort is more beautiful.
Let the two numbers compare.
Go up big and run to the right.

Bubbling

  • Belongs to exchange order

  • Compare sizes and exchange positions. It's like bubbling up

  • Results are sorted in ascending and descending order


  • N numbers in ascending order , from left to right, numbering starts from 0 to n-1,
    the values ​​of index 0 and 1 are compared, if index 0 is large, the two positions are exchanged, if index 1 is large, then no exchange.
    Continue to compare the values ​​of indexes 1 and 2 and place the larger value on the right.
    Until n-2 and n-1 are compared, the first round of comparison is completed.
    The second round is compared from index 0 to n-2, because the rightmost n-1 position is already the maximum.
    By analogy, each round will reduce the rightmost one, and will not participate in the comparison until the last 2 numbers are compared.

  • Descending order

    • Contrary to ascending order

Bubbling method should be learned, but the efficiency is not very high

Bubble code implementation (1)
num_list=[
    [1,9,8,5,6,7,4,3,2],
    [1,2,3,4,5,6,7,8,9]
]
nums=num_list[0]
print(nums) # [1, 9, 8, 5, 6, 7, 4, 3, 2]
length = len(nums)
count_swap = 0
count = 0
# bubble_sort
for i in range(length): # 外层循环控制的是走多少轮,凑这个数
    for j in range(length-i-1): # 内层循环控制的是单轮比较多少次,凑这个数
        count+=1
        if nums[j] > nums[j+1]:
            tmp=nums[j]
            nums[j]=nums[j+1]
            nums[j+1]=tmp
            count_swap +=1
print(nums,count_swap,count)
Bubble code implementation (2)
num_list=[
    [1,9,8,5,6,7,4,3,2],
    [1,2,3,4,5,6,7,8,9]
]
nums=num_list[1]
print(nums) # [1, 9, 8, 5, 6, 7, 4, 3, 2]
length = len(nums)
count_swap = 0
count = 0
# bubble_sort
for i in range(length): # 外层循环控制的是走多少轮,凑这个数
    for j in range(length-i-1): # 内层循环控制的是单轮比较多少次,凑这个数
        flag=True
        count+=1
        if nums[j] > nums[j+1]:
            tmp=nums[j]
            nums[j]=nums[j+1]
            nums[j+1]=tmp
            count_swap +=1
            flag=False
    if flag:
        break
print(nums,count_swap,count)

Bubbling method summary:

  • The bubbling method requires round by round comparison of data.
  • You can set a flag to determine whether there is data exchange in this round. If no exchange occurs, you can end the sorting. If an exchange occurs, continue to the next round of sorting.
  • The worst sorting situation is that the initial order is completely opposite to the target order, and the number of traversal times 1, ..., n-1 is n (n-1) / 2
  • The best case order is that the initial order is exactly the same as the target order, and the number of traversal times is n-1
  • Time complexity O (n2)

Follow-up:

  1. Bubble Sort
  2. Select sort
  3. Insert sort
  4. Heap sort
  5. Quick sort

Guess you like

Origin www.cnblogs.com/gnuzsx/p/12723390.html