Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
Go
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Stiemerling
Go
Commits
18852cf6
Commit
18852cf6
authored
16 years ago
by
Robert Griesemer
Browse files
Options
Downloads
Patches
Plain Diff
- added sort package and test case
R=r OCL=14975 CL=14975
parent
c3ca0561
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/lib/make.bash
+1
-1
1 addition, 1 deletion
src/lib/make.bash
src/lib/sort.go
+95
-0
95 additions, 0 deletions
src/lib/sort.go
test/sorting.go
+62
-0
62 additions, 0 deletions
test/sorting.go
with
158 additions
and
1 deletion
src/lib/make.bash
+
1
−
1
View file @
18852cf6
...
...
@@ -7,7 +7,7 @@
echo
;
echo
;
echo
%%%% making lib %%%%
;
echo
rm
-f
*
.6
for
i
in
fmt.go flag.go container/vector.go
for
i
in
fmt.go flag.go container/vector.go
sort.go
do
base
=
$(
basename
$i
.go
)
echo
6g
-o
$GOROOT
/pkg/
$base
.6
$i
...
...
This diff is collapsed.
Click to expand it.
src/lib/sort.go
0 → 100644
+
95
−
0
View file @
18852cf6
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package
Sort
export
type
SortInterface
interface
{
len
()
int
;
less
(
i
,
j
int
)
bool
;
swap
(
i
,
j
int
);
}
func
Pivot
(
data
SortInterface
,
a
,
b
int
)
int
{
// if we have at least 10 elements, find a better median
// by selecting the median of 3 elements and putting it
// at position a
if
b
-
a
>=
10
{
m0
:=
(
a
+
b
)
/
2
;
m1
:=
a
;
m2
:=
b
-
1
;
// bubble sort on 3 elements
if
data
.
less
(
m1
,
m0
)
{
data
.
swap
(
m1
,
m0
);
}
if
data
.
less
(
m2
,
m1
)
{
data
.
swap
(
m2
,
m1
);
}
if
data
.
less
(
m1
,
m0
)
{
data
.
swap
(
m1
,
m0
);
}
// "m0 <= m1 <= m2"
}
m
:=
a
;
for
i
:=
a
+
1
;
i
<
b
;
i
++
{
if
data
.
less
(
i
,
a
)
{
m
++
;
data
.
swap
(
i
,
m
);
}
}
data
.
swap
(
a
,
m
);
return
m
;
}
func
Quicksort
(
data
SortInterface
,
a
,
b
int
)
{
if
a
+
1
<
b
{
m
:=
Pivot
(
data
,
a
,
b
);
Quicksort
(
data
,
0
,
m
);
Quicksort
(
data
,
m
+
1
,
b
);
}
}
export
func
Sort
(
data
SortInterface
)
{
Quicksort
(
data
,
0
,
data
.
len
());
}
export
func
IsSorted
(
data
SortInterface
)
bool
{
n
:=
data
.
len
();
for
i
:=
n
-
1
;
i
>
0
;
i
--
{
if
data
.
less
(
i
,
i
-
1
)
{
return
false
;
}
}
return
true
;
}
// Convenience types for common cases
// TODO: Once we can associate methods with all types, this can be simplified
// since we cann associate the methods with the arrays directly.
export
type
IntArray
struct
{
data
*
[]
int
;
}
func
(
p
*
IntArray
)
len
()
int
{
return
len
(
p
.
data
);
}
func
(
p
*
IntArray
)
less
(
i
,
j
int
)
bool
{
return
p
.
data
[
i
]
<
p
.
data
[
j
];
}
func
(
p
*
IntArray
)
swap
(
i
,
j
int
)
{
p
.
data
[
i
],
p
.
data
[
j
]
=
p
.
data
[
j
],
p
.
data
[
i
];
}
export
type
FloatArray
struct
{
data
*
[]
float
;
}
func
(
p
*
FloatArray
)
len
()
int
{
return
len
(
p
.
data
);
}
func
(
p
*
FloatArray
)
less
(
i
,
j
int
)
bool
{
return
p
.
data
[
i
]
<
p
.
data
[
j
];
}
func
(
p
*
FloatArray
)
swap
(
i
,
j
int
)
{
p
.
data
[
i
],
p
.
data
[
j
]
=
p
.
data
[
j
],
p
.
data
[
i
];
}
export
type
StringArray
struct
{
data
*
[]
string
;
}
func
(
p
*
StringArray
)
len
()
int
{
return
len
(
p
.
data
);
}
func
(
p
*
StringArray
)
less
(
i
,
j
int
)
bool
{
return
p
.
data
[
i
]
<
p
.
data
[
j
];
}
func
(
p
*
StringArray
)
swap
(
i
,
j
int
)
{
p
.
data
[
i
],
p
.
data
[
j
]
=
p
.
data
[
j
],
p
.
data
[
i
];
}
This diff is collapsed.
Click to expand it.
test/sorting.go
0 → 100644
+
62
−
0
View file @
18852cf6
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// $G $F.go && $L $F.$A && ./$A.out
package
main
import
Sort
"sort"
func
main
()
{
{
data
:=
[]
int
{
74
,
59
,
238
,
-
784
,
9845
,
959
,
905
,
0
,
0
,
42
,
7586
,
-
5467984
,
7586
};
a
:=
Sort
.
IntArray
(
&
data
);
Sort
.
Sort
(
&
a
);
/*
for i := 0; i < len(data); i++ {
print(data[i], " ");
}
print("\n");
*/
if
!
Sort
.
IsSorted
(
&
a
)
{
panic
();
}
}
{
data
:=
[]
float
{
74.3
,
59.0
,
238.2
,
-
784.0
,
2.3
,
9845.768
,
-
959.7485
,
905
,
7.8
,
7.8
};
a
:=
Sort
.
FloatArray
(
&
data
);
Sort
.
Sort
(
&
a
);
/*
for i := 0; i < len(data); i++ {
print(data[i], " ");
}
print("\n");
*/
if
!
Sort
.
IsSorted
(
&
a
)
{
panic
();
}
}
{
data
:=
[]
string
{
""
,
"Hello"
,
"foo"
,
"bar"
,
"foo"
,
"f00"
,
"%*&^*&^&"
,
"***"
};
a
:=
Sort
.
StringArray
(
&
data
);
Sort
.
Sort
(
&
a
);
/*
for i := 0; i < len(data); i++ {
print(data[i], " ");
}
print("\n");
*/
if
!
Sort
.
IsSorted
(
&
a
)
{
panic
();
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment