Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
2XB3FinalProject
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Christopher Schankula
2XB3FinalProject
Commits
4dde7241
Commit
4dde7241
authored
7 years ago
by
Lawrence Chung
Browse files
Options
Downloads
Plain Diff
Conflict fix
parents
018a6081
5c7e3d16
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/sandbox/GeneralCompare.java
+3
-2
3 additions, 2 deletions
src/sandbox/GeneralCompare.java
src/sort/GeneralCompare.java
+2
-2
2 additions, 2 deletions
src/sort/GeneralCompare.java
src/sort/MergeSort.java
+45
-23
45 additions, 23 deletions
src/sort/MergeSort.java
with
50 additions
and
27 deletions
src/sandbox/GeneralCompare.java
+
3
−
2
View file @
4dde7241
package
sandbox
;
public
interface
GeneralCompare
<
T
>
{
public
int
compare
(
T
a1
,
T
a2
);
public
interface
GeneralCompare
{
public
int
eval
(
Comparable
a1
,
Comparable
a2
);
}
This diff is collapsed.
Click to expand it.
src/sort/GeneralCompare.java
+
2
−
2
View file @
4dde7241
package
sort
;
public
interface
GeneralCompare
<
T
>
{
public
int
compare
(
T
a1
,
T
a2
);
public
interface
GeneralCompare
{
public
int
compare
(
Comparable
a1
,
Comparable
a2
);
}
This diff is collapsed.
Click to expand it.
src/sort/MergeSort.java
+
45
−
23
View file @
4dde7241
package
sort
;
import
java.lang.reflect.Array
;
public
class
MergeSort
{
private
static
Comparable
[]
aux
;
public
static
void
merge
(
Comparable
[]
x
,
int
lo
,
int
hi
,
GeneralCompare
gc
){
public
static
void
main
(
String
[]
args
)
{
GeneralCompare
b1
;
b1
=
(
a1
,
a2
)
->
(
Integer
)
a1
-
(
Integer
)
a2
;
Integer
[]
test
=
{
3
,
4
,
2
,
1
,
5
,
7
,
9
,
10
};
sort
(
test
,
0
,
Array
.
getLength
(
test
)
-
1
,
b1
);
int
n
=
hi
-
lo
;
aux
=
new
Comparable
[
n
];
for
(
int
i
=
0
;
i
<
(
Array
.
getLength
(
test
)
-
1
)
;
i
++)
{
System
.
out
.
println
(
test
[
i
]);
}
}
public
static
void
sort
(
Comparable
[]
x
,
int
lo
,
int
hi
,
GeneralCompare
gc
)
{
aux
=
new
Comparable
[
Array
.
getLength
(
x
)];
sortWrapped
(
x
,
0
,
Array
.
getLength
(
x
)
-
1
,
gc
);
}
private
static
void
sortWrapped
(
Comparable
[]
x
,
int
lo
,
int
hi
,
GeneralCompare
gc
)
{
int
n
=
hi
-
lo
;
if
(
n
<=
1
)
return
;
// Recursively merge each half of the array
int
mid
=
(
n
/
2
)
+
1
;
merge
(
x
,
lo
,
mid
,
gc
);
merge
(
x
,
mid
+
1
,
hi
,
gc
);
// Recursively sort each half of the array
int
mid
=
lo
+
(
n
/
2
);
sortWrapped
(
x
,
lo
,
mid
,
gc
);
sortWrapped
(
x
,
mid
+
1
,
hi
,
gc
);
merge
(
x
,
lo
,
hi
,
gc
);
}
private
static
void
merge
(
Comparable
[]
x
,
int
lo
,
int
hi
,
GeneralCompare
gc
){
int
n
=
hi
-
lo
;
int
mid
=
lo
+
(
n
/
2
);
// Fill auxiliary array
for
(
int
k
=
1
;
k
<=
n
;
k
++){
System
.
out
.
println
(
n
);
for
(
int
k
=
lo
;
k
<=
hi
;
k
++){
aux
[
k
]
=
x
[
k
];
}
//
while
((
mid
<
hi
)
|
(
lo
<
mid
))
{
if
(
compare
(
aux
[
lo
],
aux
[
mid
]))
x
[
lo
++]
=
mid
;
int
i
=
0
;
// Merging two sorted arrays
while
((
lo
<=
mid
)
&
(
mid
<=
hi
))
{
if
(
gc
.
compare
(
aux
[
lo
],
aux
[
mid
])
>
0
)
x
[
i
++]
=
aux
[
mid
++];
else
x
[
i
++]
=
aux
[
lo
++];
}
/*
// Is this portion sorting the aux array? Check
for(int k = 1; k <= n; k++){
if(i > j-1)
...
...
@@ -40,9 +64,7 @@ public class MergeSort{
x[k] = aux[j++];
else
x[k] = aux[i++];
}
}
}*/
}
}
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