Μάθημα : ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ Γ ΕΠΑΛ ΘΕΩΡΙΑ
Κωδικός : T100228
-
Εμφάνιση όλων των ενοτήτων
Σειριακή αναζήτηση
Η σειριακή αναζήτηση διερευνά τα το προς διερεύνηση στοιχείο υπάρχει στη λίστα. Oi πληροφορίες που μπορεί να εξαγχθούν είναι μια ένδειξη π.χ. found με τιμή true αν βρέθηκε το προς διερεύνηση στοιχείο ειδάλλωs έχει τιμή false.
Αλλη πληλοφορία που μπορεί να εξαγχθεί είναι η θέση του ευρεθέντος στοιχείοι που είναι η θέση στη λίστα
Η ΣΕΙΡΙΑΚΗ ΑΝΑΖΗΤΗΣΗ υλοποιείται με τον παρακάτω κώδικα
def linearSearch( array, key ):
pos = -1
i ,N = 0 ,len(array)
while pos < 0 and i < N:
if array[ i ] == key :
pos = i
break
i = i + 1
return pos
testlist=[1,3,4,6,7,10,13,14]
print(linearSearch(testlist,5))
print(linearSearch(testlist,14))
--------------------------------------
def linearSearch( array, key ):
pos = -1
i ,N = 0 ,len(array)
while pos < 0 and i < N:
if array[ i ] == key :
pos = i
break
i = i + 1
return pos
lista=[1,3,4,6,7,10,13,14] #
pos = linearSearch(lista,5)
if pos == -1:
print("to 5 den brathike ")
else:
print("to 5 brethike sth thesi",pos)
pos = linearSearch(lista,14)
if pos == -1:
print("to 14 den brathike ")
else:
print("to 14 brethike sth thesi",pos)
-------------------------------------------------
def linearSearch( array, key ):
pos = -1
i ,N = 0 ,len(array)
while pos < 0 and i < N:
if array[ i ] == key :
pos = i
break
i = i + 1
return pos
vathmoi=[1,3,4,6,7,10,13,14] #
names=['A','b','c','d','e','f','g','j']
pos = linearSearch(names,'d')
if pos == -1:
print("to d den brathike ")
else:
print ("to d brethike sth thesi",pos,'kai phre vathmo',vathmoi[pos])