-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbubble_sort.go
91 lines (77 loc) · 1.1 KB
/
bubble_sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import "fmt"
var C int
type Node struct {
value string
prev *Node
next *Node
}
type List struct {
head *Node
tail *Node
}
func (l *List) Sort(C int) {
for ; C > 0; C-- {
n := l.head
for n != nil {
if n.next == nil {
break
}
if n.value > n.next.value {
tmp := n.next.value
n.next.value = n.value
n.value = tmp
}
n = n.next
}
}
}
func (l *List) Show() {
n := l.tail
C = 0
for n != nil {
fmt.Println(n.value)
C = C + 1
n = n.prev
}
}
func (l *List) Insertfront(value string) {
n := &Node{
value: value,
next: l.head,
}
if l.head != nil {
l.head.prev = n
}
l.head = n
if l.tail == nil {
l.tail = n
}
}
func (l *List) Insertback(value string) {
n := &Node{
value: value,
prev: l.tail,
}
if l.tail != nil {
l.tail.next = n
}
l.tail = n
if l.head == nil {
l.head = n
}
}
func main() {
l := List{}
l.Insertback("11")
l.Insertback("13")
l.Insertfront("09")
l.Insertfront("07")
l.Insertfront("17")
l.Insertfront("29")
fmt.Println("Original List")
l.Show()
l.Sort(C)
fmt.Println("Bubble Sorted List")
l.Show()
}