Skip to content
Snippets Groups Projects

Lab6

4 files
+ 121670
1
Compare changes
  • Side-by-side
  • Inline

Files

lab6/ricerca.go 0 → 100644
+ 78
0
 
package main
 
 
import (
 
"fmt"
 
_ "slices"
 
"strconv"
 
)
 
 
func main() {
 
table := []int{-9, -1, 0, 13, 14, 14, 12, 29, 31, 24, 36, 36, 44, 44, 8}
 
x1,x2 := algoX(table,14)
 
y1,y2 := algoY(table,44)
 
fmt.Println(x1,x2,y1,y2)
 
 
}
 
 
func algoX(table []int, x int) (bool, string) {
 
s := ""
 
for i, el := range table {
 
if el == x {
 
s += strconv.Itoa(i)
 
}
 
}
 
if s != ""{
 
return true,s
 
}
 
return false,""
 
}
 
 
func algoY(table []int, x int) (bool, string) {
 
// slices.Sort(table)
 
// fmt.Println(table)
 
s := ""
 
low, high := 0, len(table)-1
 
//LOW = 0, HIGH = 14
 
// MID = 7
 
//HIGH = 6
 
// MID = 0+6 = 3
 
// low = 4
 
for low <= high {
 
mid := (low + high) / 2
 
if table[mid] < x {
 
low = mid + 1
 
fmt.Println(low,mid,high)
 
} else if table[mid] > x {
 
high = mid - 1
 
} else {
 
s+=strconv.Itoa(mid)
 
table[mid] = 999
 
low,high = 0,len(table)-1
 
}
 
}
 
if s != ""{
 
return true,s
 
}
 
return false, ""
 
}
 
 
//1) 0,1,2,3,4
 
//2) 4,3,6
 
//3)
 
/*
 
A X
 
B N
 
C X
 
D N
 
E N
 
F X
 
G Y
 
H N
 
 
4) no, algoX controlla tutte le posizione dell'array finche non trova l'elemento, Y è più efficente se è ordinato
 
5) AlgoX = ricercaIterative, AlgoY = ricerca binaria
 
6) bisogna aggiungere una variabile stringa dove contateno la posizione dell'elemento X, e ritorno la stringa
 
7) no, poichè effettuando la ricerca binaria, quando si trova un elemento si ferma,
 
per trovare i duplicati bisognerebbe implementare una ricerca che rimuove l'elemento trovato e riparte dall'inizio,
 
oppure che rimpiazza l'elemento trovato con un altro valore, cambia si la complessità
 
*/
 
\ No newline at end of file
Loading