def linear_search(arr,x):
for i in range(len(arr)):
if arr[i] == x:
print("[[find]] index :",i)
break
arr = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
x = 6
linear_search(arr,x)
전진 이동법(Move to Front Method)
def move_to_front(arr,x):
for i in range(len(arr)):
if arr[i] == x:
for j in range(i-1,-1,-1):
arr[j+1] = arr[j]
arr[0] = x
return i
return False
arr = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
x = 6
res = move_to_front(arr,x)
if not res:
print("Not found")
else:
print("[[find]] index :",res)
print("arr:",arr)
전위법(Transpose Method)
def transpose(arr,x):
for i in range(len(arr)):
if arr[i] == x:
if i!=0 :
arr[i],arr[i-1] = arr[i-1],arr[i]
return i
return False
arr = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
x = 6
res = transpose(arr,x)
if not res:
print("Not found")
else:
print("[[find]] index :",res)
print("arr:",arr)