Quick_sort_in sorting (#224)
* Update 01_dijkstras_algorithm.go * Create Test_report.binary_quick_sort.cpp.docx * 1 fix * fix it better
This commit is contained in:
@@ -21,4 +21,4 @@ function quicksort(array) {
|
|||||||
return quicksort(less).concat([pivot], quicksort(greater));
|
return quicksort(less).concat([pivot], quicksort(greater));
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(quicksort([10, 5, 2, 3])); // [2, 3, 5, 10]
|
console.log(quicksort([10, 5, 2, 3]));
|
||||||
Binary file not shown.
@@ -5,69 +5,90 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var graph map[string]map[string]float64
|
||||||
|
var costs map[string]float64
|
||||||
|
var parents map[string]string
|
||||||
|
var processed []string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
graph := make(map[string]map[string]int)
|
// the graph
|
||||||
graph["start"] = map[string]int{}
|
graph = make(map[string]map[string]float64)
|
||||||
|
graph["start"] = make(map[string]float64)
|
||||||
graph["start"]["a"] = 6
|
graph["start"]["a"] = 6
|
||||||
graph["start"]["b"] = 2
|
graph["start"]["b"] = 2
|
||||||
|
|
||||||
graph["a"] = map[string]int{}
|
graph["a"] = make(map[string]float64)
|
||||||
graph["a"]["finish"] = 1
|
graph["a"]["fin"] = 1
|
||||||
|
|
||||||
graph["b"] = map[string]int{}
|
graph["b"] = make(map[string]float64)
|
||||||
graph["b"]["a"] = 3
|
graph["b"]["a"] = 3
|
||||||
graph["b"]["finish"] = 5
|
graph["b"]["fin"] = 5
|
||||||
|
|
||||||
graph["finish"] = map[string]int{}
|
graph["fin"] = make(map[string]float64)
|
||||||
|
|
||||||
costs, parents := findShortestPath(graph, "start", "finish")
|
// the costs table
|
||||||
fmt.Println(costs, parents)
|
costs = make(map[string]float64)
|
||||||
}
|
costs["a"] = 6
|
||||||
|
costs["b"] = 2
|
||||||
|
costs["fin"] = math.Inf(1)
|
||||||
|
|
||||||
// Finds shortest path using dijkstra algorithm
|
// the parents table
|
||||||
func findShortestPath(graph map[string]map[string]int, startNode string, finishNode string) (map[string]int, map[string]string) {
|
parents = make(map[string]string)
|
||||||
costs := make(map[string]int)
|
parents["a"] = "start"
|
||||||
costs[finishNode] = math.MaxInt32
|
parents["b"] = "start"
|
||||||
|
parents["fin"] = ""
|
||||||
|
|
||||||
parents := make(map[string]string)
|
// Find the lowest-cost node that you haven't processed yet.
|
||||||
parents[finishNode] = ""
|
node := find_lowest_cost_node(costs)
|
||||||
|
// If you've processed all the nodes, this while loop is done.
|
||||||
|
|
||||||
processed := make(map[string]bool)
|
for node != "" {
|
||||||
|
cost := costs[node]
|
||||||
|
// Go through all the neighbors of this node.
|
||||||
|
neighbors := graph[node]
|
||||||
|
|
||||||
// Initialization of costs and parents
|
for node, _ := range neighbors {
|
||||||
for node, cost := range graph[startNode] {
|
new_cost := cost + neighbors[node]
|
||||||
costs[node] = cost
|
// If it's cheaper to get to this neighbor by going through this node...
|
||||||
parents[node] = startNode
|
if costs[node] > new_cost {
|
||||||
}
|
// ... update the cost for this node.
|
||||||
|
costs[node] = new_cost
|
||||||
lowestCostNode := findLowestCostNode(costs, processed)
|
// This node becomes the new parent for this neighbor.
|
||||||
for ; lowestCostNode != "" ; {
|
parents[node] = node
|
||||||
// Calculation costs for neighbours
|
|
||||||
for node, cost := range graph[lowestCostNode] {
|
|
||||||
newCost := costs[lowestCostNode] + cost
|
|
||||||
if newCost < costs[node] {
|
|
||||||
// Set new cost for this node
|
|
||||||
costs[node] = newCost
|
|
||||||
parents[node] = lowestCostNode
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Mark the node as processed.
|
||||||
processed[lowestCostNode] = true
|
processed = append(processed, node)
|
||||||
lowestCostNode = findLowestCostNode(costs, processed)
|
// Find the next node to process, and loop.
|
||||||
|
node = find_lowest_cost_node(costs)
|
||||||
}
|
}
|
||||||
|
|
||||||
return costs, parents
|
fmt.Println(costs)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func findLowestCostNode(costs map[string]int, processed map[string]bool) string {
|
func find_lowest_cost_node(costs map[string]float64) string {
|
||||||
lowestCost := math.MaxInt32
|
lowest_cost := math.Inf(1)
|
||||||
lowestCostNode := ""
|
lowest_cost_node := ""
|
||||||
for node, cost := range costs {
|
|
||||||
if _, inProcessed := processed[node]; cost < lowestCost && !inProcessed {
|
for node, _ := range costs {
|
||||||
lowestCost = cost
|
// fmt.Println("Node:", node, "Value:", value)
|
||||||
lowestCostNode = node
|
cost := costs[node]
|
||||||
|
// If it's the lowest cost so far and hasn't been processed yet...
|
||||||
|
if cost < lowest_cost && !contains(processed, node) {
|
||||||
|
// ... set it as the new lowest-cost node.
|
||||||
|
lowest_cost = cost
|
||||||
|
lowest_cost_node = node
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return lowest_cost_node
|
||||||
return lowestCostNode
|
}
|
||||||
|
|
||||||
|
func contains(s []string, e string) bool {
|
||||||
|
for _, a := range s {
|
||||||
|
if a == e {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user