728x90
반응형

수행 코드

arr = [40, 70, 60, 30, 10, 50]

# acending
for i in range( len(arr)-1, 0, -1):    # number of pairs.    
    for j in range(i):
        print( "i=", i, "j=", j )
        if arr[j] > arr[j+1]:           # comparision 
            arr[j], arr[j+1]   = arr[j+1], arr[j]   #swap 
        print(" Inner Result: ", arr )
    
    print(" >> Partial Result: ", arr )

print(">>>> Final Result: ", arr )

 

수행 결과 화면

PS D:\PythonCode> python .\BubbleSorting.py
i= 5 j= 0
 Inner Result:  [40, 70, 60, 30, 10, 50]     
i= 5 j= 1
 Inner Result:  [40, 60, 70, 30, 10, 50]     
i= 5 j= 2
 Inner Result:  [40, 60, 30, 70, 10, 50]     
i= 5 j= 3
 Inner Result:  [40, 60, 30, 10, 70, 50]     
i= 5 j= 4
 Inner Result:  [40, 60, 30, 10, 50, 70]
 >> Partial Result:  [40, 60, 30, 10, 50, 70]
i= 4 j= 0
 Inner Result:  [40, 60, 30, 10, 50, 70]
i= 4 j= 1
 Inner Result:  [40, 30, 60, 10, 50, 70]
i= 4 j= 2
 Inner Result:  [40, 30, 10, 60, 50, 70]
i= 4 j= 3
 Inner Result:  [40, 30, 10, 50, 60, 70]
 >> Partial Result:  [40, 30, 10, 50, 60, 70]
i= 3 j= 0
 Inner Result:  [30, 40, 10, 50, 60, 70]
i= 3 j= 1
 Inner Result:  [30, 10, 40, 50, 60, 70]
i= 3 j= 2
 Inner Result:  [30, 10, 40, 50, 60, 70]
 >> Partial Result:  [30, 10, 40, 50, 60, 70]
i= 2 j= 0
 Inner Result:  [10, 30, 40, 50, 60, 70]
i= 2 j= 1
 Inner Result:  [10, 30, 40, 50, 60, 70]
 >> Partial Result:  [10, 30, 40, 50, 60, 70]
i= 1 j= 0
 Inner Result:  [10, 30, 40, 50, 60, 70]
 >> Partial Result:  [10, 30, 40, 50, 60, 70]
>>>> Final Result:  [10, 30, 40, 50, 60, 70]
PS D:\PythonCode>

 

수행결과 분석

 

728x90
반응형

+ Recent posts