Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
165d7871
Commit
165d7871
authored
Nov 19, 2008
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- adjust sort.go to use new naming conventions
R=rsc DELTA=31 (0 added, 0 deleted, 31 changed) OCL=19618 CL=19620
parent
10002bb0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
31 deletions
+31
-31
sort.go
src/lib/sort.go
+28
-28
sorting.go
test/sorting.go
+3
-3
No files found.
src/lib/sort.go
View file @
165d7871
...
...
@@ -5,9 +5,9 @@
package
sort
export
type
SortInterface
interface
{
l
en
()
int
;
l
ess
(
i
,
j
int
)
bool
;
s
wap
(
i
,
j
int
);
L
en
()
int
;
L
ess
(
i
,
j
int
)
bool
;
S
wap
(
i
,
j
int
);
}
func
min
(
a
,
b
int
)
int
{
...
...
@@ -20,8 +20,8 @@ func min(a, b int) int {
// Insertion sort
func
InsertionSort
(
data
SortInterface
,
a
,
b
int
)
{
for
i
:=
a
+
1
;
i
<
b
;
i
++
{
for
j
:=
i
;
j
>
a
&&
data
.
l
ess
(
j
,
j
-
1
);
j
--
{
data
.
s
wap
(
j
,
j
-
1
);
for
j
:=
i
;
j
>
a
&&
data
.
L
ess
(
j
,
j
-
1
);
j
--
{
data
.
S
wap
(
j
,
j
-
1
);
}
}
}
...
...
@@ -36,15 +36,15 @@ func MedianOfThree(data SortInterface, a, b, c int) {
m2
:=
c
;
// bubble sort on 3 elements
if
data
.
less
(
m1
,
m0
)
{
data
.
s
wap
(
m1
,
m0
);
}
if
data
.
less
(
m2
,
m1
)
{
data
.
s
wap
(
m2
,
m1
);
}
if
data
.
less
(
m1
,
m0
)
{
data
.
s
wap
(
m1
,
m0
);
}
if
data
.
Less
(
m1
,
m0
)
{
data
.
S
wap
(
m1
,
m0
);
}
if
data
.
Less
(
m2
,
m1
)
{
data
.
S
wap
(
m2
,
m1
);
}
if
data
.
Less
(
m1
,
m0
)
{
data
.
S
wap
(
m1
,
m0
);
}
// now data[m0] <= data[m1] <= data[m2]
}
func
SwapRange
(
data
SortInterface
,
a
,
b
,
n
int
)
{
for
i
:=
0
;
i
<
n
;
i
++
{
data
.
s
wap
(
a
+
i
,
b
+
i
);
data
.
S
wap
(
a
+
i
,
b
+
i
);
}
}
...
...
@@ -72,28 +72,28 @@ func Pivot(data SortInterface, lo, hi int) (midlo, midhi int) {
pivot
:=
lo
;
a
,
b
,
c
,
d
:=
lo
+
1
,
lo
+
1
,
hi
,
hi
;
for
b
<
c
{
if
data
.
l
ess
(
b
,
pivot
)
{
// data[b] < pivot
if
data
.
L
ess
(
b
,
pivot
)
{
// data[b] < pivot
b
++
;
continue
;
}
if
!
data
.
l
ess
(
pivot
,
b
)
{
// data[b] = pivot
data
.
s
wap
(
a
,
b
);
if
!
data
.
L
ess
(
pivot
,
b
)
{
// data[b] = pivot
data
.
S
wap
(
a
,
b
);
a
++
;
b
++
;
continue
;
}
if
data
.
l
ess
(
pivot
,
c
-
1
)
{
// data[c-1] > pivot
if
data
.
L
ess
(
pivot
,
c
-
1
)
{
// data[c-1] > pivot
c
--
;
continue
;
}
if
!
data
.
l
ess
(
c
-
1
,
pivot
)
{
// data[c-1] = pivot
data
.
s
wap
(
c
-
1
,
d
-
1
);
if
!
data
.
L
ess
(
c
-
1
,
pivot
)
{
// data[c-1] = pivot
data
.
S
wap
(
c
-
1
,
d
-
1
);
c
--
;
d
--
;
continue
;
}
// data[b] > pivot; data[c-1] < pivot
data
.
s
wap
(
b
,
c
-
1
);
data
.
S
wap
(
b
,
c
-
1
);
b
++
;
c
--
;
}
...
...
@@ -118,14 +118,14 @@ func Quicksort(data SortInterface, a, b int) {
}
export
func
Sort
(
data
SortInterface
)
{
Quicksort
(
data
,
0
,
data
.
l
en
());
Quicksort
(
data
,
0
,
data
.
L
en
());
}
export
func
IsSorted
(
data
SortInterface
)
bool
{
n
:=
data
.
l
en
();
n
:=
data
.
L
en
();
for
i
:=
n
-
1
;
i
>
0
;
i
--
{
if
data
.
l
ess
(
i
,
i
-
1
)
{
if
data
.
L
ess
(
i
,
i
-
1
)
{
return
false
;
}
}
...
...
@@ -139,27 +139,27 @@ export type IntArray struct {
data
*
[]
int
;
}
func
(
p
*
IntArray
)
l
en
()
int
{
return
len
(
p
.
data
);
}
func
(
p
*
IntArray
)
l
ess
(
i
,
j
int
)
bool
{
return
p
.
data
[
i
]
<
p
.
data
[
j
];
}
func
(
p
*
IntArray
)
s
wap
(
i
,
j
int
)
{
p
.
data
[
i
],
p
.
data
[
j
]
=
p
.
data
[
j
],
p
.
data
[
i
];
}
func
(
p
*
IntArray
)
L
en
()
int
{
return
len
(
p
.
data
);
}
func
(
p
*
IntArray
)
L
ess
(
i
,
j
int
)
bool
{
return
p
.
data
[
i
]
<
p
.
data
[
j
];
}
func
(
p
*
IntArray
)
S
wap
(
i
,
j
int
)
{
p
.
data
[
i
],
p
.
data
[
j
]
=
p
.
data
[
j
],
p
.
data
[
i
];
}
export
type
FloatArray
struct
{
data
*
[]
float
;
}
func
(
p
*
FloatArray
)
l
en
()
int
{
return
len
(
p
.
data
);
}
func
(
p
*
FloatArray
)
l
ess
(
i
,
j
int
)
bool
{
return
p
.
data
[
i
]
<
p
.
data
[
j
];
}
func
(
p
*
FloatArray
)
s
wap
(
i
,
j
int
)
{
p
.
data
[
i
],
p
.
data
[
j
]
=
p
.
data
[
j
],
p
.
data
[
i
];
}
func
(
p
*
FloatArray
)
L
en
()
int
{
return
len
(
p
.
data
);
}
func
(
p
*
FloatArray
)
L
ess
(
i
,
j
int
)
bool
{
return
p
.
data
[
i
]
<
p
.
data
[
j
];
}
func
(
p
*
FloatArray
)
S
wap
(
i
,
j
int
)
{
p
.
data
[
i
],
p
.
data
[
j
]
=
p
.
data
[
j
],
p
.
data
[
i
];
}
export
type
StringArray
struct
{
data
*
[]
string
;
}
func
(
p
*
StringArray
)
l
en
()
int
{
return
len
(
p
.
data
);
}
func
(
p
*
StringArray
)
l
ess
(
i
,
j
int
)
bool
{
return
p
.
data
[
i
]
<
p
.
data
[
j
];
}
func
(
p
*
StringArray
)
s
wap
(
i
,
j
int
)
{
p
.
data
[
i
],
p
.
data
[
j
]
=
p
.
data
[
j
],
p
.
data
[
i
];
}
func
(
p
*
StringArray
)
L
en
()
int
{
return
len
(
p
.
data
);
}
func
(
p
*
StringArray
)
L
ess
(
i
,
j
int
)
bool
{
return
p
.
data
[
i
]
<
p
.
data
[
j
];
}
func
(
p
*
StringArray
)
S
wap
(
i
,
j
int
)
{
p
.
data
[
i
],
p
.
data
[
j
]
=
p
.
data
[
j
],
p
.
data
[
i
];
}
// Convenience wrappers for common cases
...
...
test/sorting.go
View file @
165d7871
...
...
@@ -158,9 +158,9 @@ type TestingData struct {
nswap
int
;
}
func
(
d
*
TestingData
)
l
en
()
int
{
return
len
(
d
.
data
);
}
func
(
d
*
TestingData
)
l
ess
(
i
,
j
int
)
bool
{
return
d
.
data
[
i
]
<
d
.
data
[
j
];
}
func
(
d
*
TestingData
)
s
wap
(
i
,
j
int
)
{
func
(
d
*
TestingData
)
L
en
()
int
{
return
len
(
d
.
data
);
}
func
(
d
*
TestingData
)
L
ess
(
i
,
j
int
)
bool
{
return
d
.
data
[
i
]
<
d
.
data
[
j
];
}
func
(
d
*
TestingData
)
S
wap
(
i
,
j
int
)
{
if
d
.
nswap
>=
d
.
maxswap
{
panicln
(
"used"
,
d
.
nswap
,
"swaps sorting"
,
len
(
d
.
data
),
"array"
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment