A little improvement, assuming there is only 1 match.
list1 = [15, 12, 13, 19, 14]
list2 = [19, 13, 15, 12, 14]
def function(l1, l2):
for i in range(len(list1)):
for j in range(len(list2)):
if list1[i] == list2[j]:
print ('B[' + str(i) + ']' + ' with ' + 'A[' + str(j) + ']')
break;
function(list1, list2)
Only a little better than O(n²)
There is another approach where complexity is the one of sort function (O(n log(n)).
list1 = [15, 12, 13, 19, 14]
list2 = [19, 13, 15, 12, 14]
list3 = [0, 1, 2, 3, 4]
def sortindirect(elem):
return list2[elem]
list3.sort(key=sortindirect)
def function(l1, l2):
for i in range(len(list1)):
for j in range(len(list2)):
if list1[i] == list2[list3[j]]:
print ('B[' + str(i) + ']' + ' with ' + 'A[' + str(j) + ']')
Dichotomic search - Wikipedia[
^]
Binary search algorithm - Wikipedia[
^]
for i in range(len(list2)):
print (list2[i])
for i in range(len(list3)):
print (list2[[list3[i]])