I was simply creating a sort selector.
The five integers entered by the user are sorted by heap, bubble, and quick.
t = 0
print("Please enter 5 integers of your choice:\n")
unsorted = []
sorted = []
while t < 5:
n = input()
unsorted.append(n)
print("Is there any sort method you want?\n")
print("1. Hip sort 2. Bubble sort 3. Quick sort\n")
call = input()
def heapsort(unsorted):
size = len(unsorted) #alignmentsize
for i in range(size, -1, -1) : heapify(unsorted, size, i)
for i in range(size-1, -1, -1) :
unsorted[i], unsorted[0] = unsorted[0], unsorted[i]
heapify(unsorted, i, 0)
def heapify(a, size, i):
largest = i
L = 2 * i + 1
R = 2 * i + 2
if L < size and a[L] > a[i]:
largest = L
if R < size and a[R] > a[largest]:
largest = R
if largest != i:
a[largest], a[i] = a[i], a[largest]
heapify(a, size, largest)
if call == 1:
heapsort(unsorted)
print("Sort result is like this.\n")
print(unsorted)
When I build it, I can't print out what the sorting result is like
How can I make it work properly?<
python sorting
call = int(input())
If you enter 1, the call becomes an integer of 1. If you just say call = input()
, it will be the string you entered, that is, "1"
. "1"!=1
.
© 2024 OneMinuteCode. All rights reserved.