-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addin Quick3String, Quick3Way, and QuickFindUF to algs4.
Final classes in algs4 :)
- Loading branch information
1 parent
6500dc4
commit c0748ae
Showing
4 changed files
with
439 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using algs4.stdlib; | ||
|
||
namespace algs4.algs4 | ||
{ | ||
public class Quick3String | ||
{ | ||
/// <summary> | ||
/// Cutoff to insertion Sort | ||
/// </summary> | ||
private const int Cutoff = 15; | ||
|
||
/// <summary> | ||
/// Sort the array a[] of strings | ||
/// </summary> | ||
/// <param name="a"></param> | ||
public static void Sort(string[] a) | ||
{ | ||
StdRandom.Shuffle(a); | ||
Sort(a, 0, a.Length - 1, 0); | ||
Debug.Assert(IsSorted(a)); | ||
} | ||
|
||
/// <summary> | ||
/// Return the dth character of s, -1 if d = length of s | ||
/// </summary> | ||
/// <param name="s"></param> | ||
/// <param name="d"></param> | ||
/// <returns></returns> | ||
private static int CharAt(string s, int d) | ||
{ | ||
Debug.Assert(d >= 0 && d <= s.Length); | ||
if (d == s.Length) | ||
{ | ||
return -1; | ||
} | ||
return s[d]; | ||
} | ||
|
||
/// <summary> | ||
/// 3-way string quicksort a[lo..hi] starting at dth character | ||
/// </summary> | ||
/// <param name="a"></param> | ||
/// <param name="lo"></param> | ||
/// <param name="hi"></param> | ||
/// <param name="d"></param> | ||
private static void Sort(string[] a, int lo, int hi, int d) | ||
{ | ||
// cutoff to insertion Sort for small subarrays | ||
if (hi <= lo + Cutoff) | ||
{ | ||
Insertion(a, lo, hi, d); | ||
return; | ||
} | ||
|
||
int lt = lo, gt = hi; | ||
int v = CharAt(a[lo], d); | ||
int i = lo + 1; | ||
while (i <= gt) | ||
{ | ||
int t = CharAt(a[i], d); | ||
if (t < v) | ||
{ | ||
Exch(a, lt++, i++); | ||
} | ||
else if (t > v) | ||
{ | ||
Exch(a, i, gt--); | ||
} | ||
else | ||
{ | ||
i++; | ||
} | ||
} | ||
|
||
// a[lo..lt-1] < v = a[lt..gt] < a[gt+1..hi]. | ||
Sort(a, lo, lt - 1, d); | ||
if (v >= 0) | ||
{ | ||
Sort(a, lt, gt, d + 1); | ||
} | ||
Sort(a, gt + 1, hi, d); | ||
} | ||
|
||
/// <summary> | ||
/// Sort from a[lo] to a[hi], starting at the dth character | ||
/// </summary> | ||
/// <param name="a"></param> | ||
/// <param name="lo"></param> | ||
/// <param name="hi"></param> | ||
/// <param name="d"></param> | ||
private static void Insertion(string[] a, int lo, int hi, int d) | ||
{ | ||
for (int i = lo; i <= hi; i++) | ||
{ | ||
for (int j = i; j > lo && Less(a[j], a[j - 1], d); j--) | ||
{ | ||
Exch(a, j, j - 1); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Exchange a[i] and a[j] | ||
/// </summary> | ||
/// <param name="a"></param> | ||
/// <param name="i"></param> | ||
/// <param name="j"></param> | ||
private static void Exch(string[] a, int i, int j) | ||
{ | ||
string temp = a[i]; | ||
a[i] = a[j]; | ||
a[j] = temp; | ||
} | ||
|
||
/// <summary> | ||
/// Is v less than w, starting at character d | ||
/// </summary> | ||
/// <param name="v"></param> | ||
/// <param name="w"></param> | ||
/// <param name="d"></param> | ||
/// <returns></returns> | ||
private static bool Less(string v, string w, int d) | ||
{ | ||
Debug.Assert(v.Substring(0, d) == w.Substring(0, d)); | ||
for (int i = d; i < Math.Min(v.Length, w.Length); i++) | ||
{ | ||
if (v[i] < w[i]) | ||
{ | ||
return true; | ||
} | ||
if (v[i] > w[i]) | ||
{ | ||
return false; | ||
} | ||
} | ||
return v.Length < w.Length; | ||
} | ||
|
||
/// <summary> | ||
/// Is the array sorted | ||
/// </summary> | ||
/// <param name="a"></param> | ||
/// <returns></returns> | ||
private static bool IsSorted(string[] a) | ||
{ | ||
for (int i = 1; i < a.Length; i++) | ||
{ | ||
if (string.Compare(a[i], a[i - 1], StringComparison.Ordinal) < 0) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public static void RunMain(string[] args) | ||
{ | ||
// read in the strings from standard input | ||
string[] a = StdIn.ReadAllStrings(); | ||
int n = a.Length; | ||
|
||
// Sort the strings | ||
Sort(a); | ||
|
||
// Print the results | ||
for (int i = 0; i < n; i++) | ||
{ | ||
StdOut.PrintLn(a[i]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using algs4.stdlib; | ||
|
||
namespace algs4.algs4 | ||
{ | ||
public static class Quick3Way | ||
{ | ||
/// <summary> | ||
/// Rearranges the array in ascending order, using the natural order. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="a">the array to be sorted</param> | ||
public static void Sort<T>(T[] a) where T : IComparable<T> | ||
{ | ||
StdRandom.Shuffle(a); | ||
Sort(a, 0, a.Length - 1); | ||
Debug.Assert(IsSorted(a)); | ||
} | ||
|
||
/// <summary> | ||
/// Quicksort the subarray a[lo .. hi] using 3-way partitioning | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="a"></param> | ||
/// <param name="lo"></param> | ||
/// <param name="hi"></param> | ||
private static void Sort<T>(T[] a, int lo, int hi) where T : IComparable<T> | ||
{ | ||
if (hi <= lo) | ||
{ | ||
return; | ||
} | ||
int lt = lo, gt = hi; | ||
T v = a[lo]; | ||
int i = lo; | ||
while (i <= gt) | ||
{ | ||
int cmp = a[i].CompareTo(v); | ||
if (cmp < 0) | ||
{ | ||
Exch(a, lt++, i++); | ||
} | ||
else if (cmp > 0) | ||
{ | ||
Exch(a, i, gt--); | ||
} | ||
else | ||
{ | ||
i++; | ||
} | ||
} | ||
|
||
// a[lo..lt-1] < v = a[lt..gt] < a[gt+1..hi]. | ||
Sort(a, lo, lt - 1); | ||
Sort(a, gt + 1, hi); | ||
Debug.Assert(IsSorted(a, lo, hi)); | ||
} | ||
|
||
#region Helper sorting functions | ||
|
||
/// <summary> | ||
/// Is v < w ? | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="v"></param> | ||
/// <param name="w"></param> | ||
/// <returns></returns> | ||
private static bool Less<T>(T v, T w) where T : IComparable<T> | ||
{ | ||
return (v.CompareTo(w) < 0); | ||
} | ||
|
||
/// <summary> | ||
/// Exchange a[i] and a[j] | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="a"></param> | ||
/// <param name="i"></param> | ||
/// <param name="j"></param> | ||
private static void Exch<T>(T[] a, int i, int j) | ||
{ | ||
T swap = a[i]; | ||
a[i] = a[j]; | ||
a[j] = swap; | ||
} | ||
|
||
#endregion | ||
|
||
#region Check if array is sorted - useful for debugging | ||
|
||
private static bool IsSorted<T>(T[] a) where T : IComparable<T> | ||
{ | ||
return IsSorted(a, 0, a.Length - 1); | ||
} | ||
|
||
private static bool IsSorted<T>(T[] a, int lo, int hi) where T : IComparable<T> | ||
{ | ||
for (int i = lo + 1; i <= hi; i++) | ||
{ | ||
if (Less(a[i], a[i - 1])) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
#endregion | ||
|
||
/// <summary> | ||
/// Print array to standard output | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="a"></param> | ||
private static void Show<T>(T[] a) | ||
{ | ||
for (int i = 0; i < a.Length; i++) | ||
{ | ||
StdOut.PrintLn(a[i]); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Reads in a sequence of strings from standard input; 3-way | ||
/// quicksorts them; and prints them to standard output in ascending order. | ||
/// </summary> | ||
/// <param name="args"></param> | ||
public static void RunMain(string[] args) | ||
{ | ||
string[] a = StdIn.ReadAllStrings(); | ||
Sort(a); | ||
Show(a); | ||
} | ||
} | ||
} |
Oops, something went wrong.