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
ee843456
Commit
ee843456
authored
7 years ago
by
Lawrence Chung
Browse files
Options
Downloads
Plain Diff
Merge branch 'TeamB' of
https://gitlab.cas.mcmaster.ca/schankuc/2XB3
into TeamB
parents
8a7810f2
49f795f2
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/search/RedBlackTree.java
+3
-0
3 additions, 0 deletions
src/search/RedBlackTree.java
src/sort/QuickSelect.java
+60
-23
60 additions, 23 deletions
src/sort/QuickSelect.java
with
63 additions
and
23 deletions
src/search/RedBlackTree.java
+
3
−
0
View file @
ee843456
...
...
@@ -5,6 +5,8 @@ public class RedBlackTree<Key, Value> {
private
GeneralCompare
<
Key
>
compare
;
private
Field
<
Key
,
Value
>
field
;
// Main method only used for testing
public
static
void
main
(
String
[]
args
)
{
GeneralCompare
<
Integer
>
b1
;
b1
=
(
a1
,
a2
)
->
(
Integer
)
a1
-
(
Integer
)
a2
;
...
...
@@ -25,6 +27,7 @@ public class RedBlackTree<Key, Value> {
}
}
/**
* Constructor for a red black tree object
* @param fld A function that retrieves the desired field from an array of comparable items
...
...
This diff is collapsed.
Click to expand it.
src/sort/QuickSelect.java
+
60
−
23
View file @
ee843456
...
...
@@ -5,46 +5,77 @@ import search.GeneralCompare;
// Code from "Algorithms: 4th Edition" by Robert Sedgewick
// Adapted from the Sedgewick Quicksort implementation
public
class
QuickSelect
<
Key
,
Record
>
{
private
int
median
;
private
int
count
;
private
GeneralCompare
<
Key
>
gc
;
public
class
QuickSelect
{
/*
// Main function for testing purposes only
public static void main(String[] args) {
//{1, 2, 3, 4, 5, 6, 7, 8, 9} 5 is median.
i
nt
[]
test
=
{
4
,
6
,
7
,
2
,
3
,
9
,
8
,
1
,
5
};
I
nt
eger
[] test = {4, 6, 7, 2,
2
,
2
,
2,2
,
2
,
2,2 ,2 ,2, 2, 2
};
GeneralCompare<Integer> b1;
b1 = (a1, a2) -> (Integer) a1 - (Integer) a2;
sort
<
Integer
,
Integer
>(
test
,
b1
);
median(test, b1);
System.out.println(test[test.length/2]);
}
*/
/**
* Partially sorts a comparable array such that elements smaller than the median occur in
* the first half of the array, and elements larger than the median occur in the second half
* @param a Array of comparable items
* @param gc Lambda function to compare items
*/
public
static
<
T
>
void
median
(
Comparable
<
T
>[]
a
,
GeneralCompare
<
T
>
gc
)
{
sort
(
a
,
0
,
a
.
length
-
1
,
a
.
length
/
2
,
gc
);
}
public
void
sort
(
Comparable
<
Record
>[]
a
,
GeneralCompare
<
Key
>
genComp
)
{
//StdRandom.shuffle(a); // Eliminate dependence on input
gc
=
genComp
;
median
=
a
.
length
/
2
;
sort
(
a
,
0
,
a
.
length
-
1
);
/**
* Partially sorts a comparable array such that elements smaller than the kth largest element
* occur in the first half of the array, and larger elements occur in the second half
* @param a Array of comparable items
* @param k Pivot element that will be in its sorted place in the array
* @param gc Lambda function to compare items
*/
public
static
<
T
>
void
partialSort
(
Comparable
<
T
>[]
a
,
int
k
,
GeneralCompare
<
T
>
gc
)
{
sort
(
a
,
0
,
a
.
length
-
1
,
k
,
gc
);
}
public
void
sort
(
Comparable
<
Record
>[]
a
,
int
lo
,
int
hi
)
{
/**
* Sorts the half of the array containing the kth (or median) index
* @param a Array of comparable items
* @param lo Lower bound index of the subarray to be sorted
* @param hi Upper bound index of the subarray to be sorted
* @param k Pivot element that will be in its sorted place in the array
* @param gc Lambda function to compare items
*/
public
static
<
T
>
void
sort
(
Comparable
<
T
>[]
a
,
int
lo
,
int
hi
,
int
k
,
GeneralCompare
<
T
>
gc
)
{
if
(
hi
<=
lo
)
return
;
int
j
=
partition
(
a
,
lo
,
hi
);
if
(
j
<
median
)
sort
(
a
,
j
+
1
,
hi
);
// Sort right part a[j+1 .. hi].
else
if
(
j
>
median
)
sort
(
a
,
lo
,
j
-
1
);
// Sort left part a[lo .. j-1].
int
j
=
partition
(
a
,
lo
,
hi
,
gc
);
if
(
j
<
k
)
sort
(
a
,
j
+
1
,
hi
,
k
,
gc
);
// Sort right part a[j+1 .. hi].
else
if
(
j
>
k
)
sort
(
a
,
lo
,
j
-
1
,
k
,
gc
);
// Sort left part a[lo .. j-1].
return
;
}
private
int
partition
(
Comparable
<
Record
>[]
a
,
int
lo
,
int
hi
)
{
/**
* Places elements smaller than a partitioning element at smaller indices than the partitioning element
* Same algorithm for partitioning as standard QuickSort from
* @param a Array of comparable items
* @param lo Index that scans from the left side of the array, points to an item to be compared
* @param hi Index that scans from the right side of the array, points to an item to be compared
* @param gc Lambda function to compare items
* @return
*/
private
static
<
T
>
int
partition
(
Comparable
<
T
>[]
a
,
int
lo
,
int
hi
,
GeneralCompare
<
T
>
gc
)
{
// Partition into a[lo..i-1], a[i], a[i+1..hi].
int
i
=
lo
,
j
=
hi
+
1
;
// left and right scan indices
Comparable
v
=
a
[
lo
];
// partitioning item
Comparable
<
T
>
v
=
a
[
lo
];
// partitioning item
while
(
true
)
{
// Scan right, scan left, check for scan complete, and exchange.
while
(
gc
.
compare
(
(
Value
)
a
[++
i
],
(
Value
)
v
)
<
0
)
while
(
gc
.
compare
(
a
[++
i
],
v
)
<
0
)
if
(
i
==
hi
)
break
;
while
(
gc
.
compare
(
v
,
a
[--
j
])
<
0
)
...
...
@@ -59,8 +90,14 @@ public class QuickSelect<Key, Record> {
return
j
;
}
private
static
void
exch
(
Comparable
[]
a
,
int
b
,
int
c
)
{
Comparable
temp
=
a
[
c
];
/**
* Exchanges the values at two indices of an array
* @param a Array of comparable items
* @param b Index of an item to be swapped
* @param c Index of an item to be swapped
*/
private
static
<
T
>
void
exch
(
Comparable
<
T
>[]
a
,
int
b
,
int
c
)
{
Comparable
<
T
>
temp
=
a
[
c
];
a
[
c
]
=
a
[
b
];
a
[
b
]
=
temp
;
}
...
...
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