图,数据结构
/**
折半查找,折半排序,binary sort
*/
private StorageA circular_shiftsB_; //original data
private StorageA alphabetizedB_; //sorted data
public void alphabetizing() {
alphabetizedB_ = new StorageA();
int alphabetized_count = 0;
// we use binary search to find the proper place
// to insert a line,
// declare variables for binary search
int low = 0;
int high = 0;
int mid = 0;
// process the circular shifts
for (int i = 0; i < circular_shiftsB_.indexs_.size(); i++) {
// current shift array
String current_shiftB = null;
// get current_shiftB
if (i == circular_shiftsB_.indexs_.size() - 1) {
current_shiftB = new String(circular_shiftsB_.chars_
.substring(circular_shiftsB_.indexs_.get(i)));
} else {
current_shiftB = circular_shiftsB_.chars_.substring(
circular_shiftsB_.indexs_.get(i),
circular_shiftsB_.indexs_.get(i + 1));
}
// binary search to the right place to insert
// the i-th line
low = 0;
high = alphabetized_count - 1;
while (low <= high) {
// find the mid line
mid = (low + high) / 2;
String mid_lineB = null;
// get mid_lineB
if (mid == alphabetizedB_.indexs_.size() - 1) {
mid_lineB = new String(alphabetizedB_.chars_
.substring(alphabetizedB_.indexs_.get(mid)));
} else {
mid_lineB = new String(alphabetizedB_.chars_.substring(
alphabetizedB_.indexs_.get(mid),
alphabetizedB_.indexs_.get(mid + 1)));
}
// find the smaller number of characters between mid and current
// shift
int length = (current_shiftB.length() < mid_lineB.length()) ? current_shiftB
.length()
: mid_lineB.length();
// comparison flag
// if two lines are identical: compared = 0
// if the first line is greater than the second one: compared =
// 1
// if the first line is smaller than the second one: compared =
// -1
int compared = 0;
// compare the lines alphabetically
// comparision is case sensitive, i.e., upper cases are
// considered
// greater than lower cases
for (int j = 0; j < length; j++) {
if (current_shiftB.charAt(j) > mid_lineB.charAt(j)) {
compared = 1;
break;
} else if (current_shiftB.charAt(j) < mid_lineB.charAt(j)) {
compared = -1;
break;
}
}
// if compared == 0 check if the lines have the equal length
// the line that has greater length is greater than the other
// line
if (compared == 0) {
if (current_shiftB.length() < mid_lineB.length())
compared = -1;
else if (current_shiftB.length() > mid_lineB.length())
compared = 1;
}
switch (compared) {
case 1: // i-th line greater
low = mid + 1;
break;
case -1: // i-th line smaller
high = mid - 1;
break;
default: // i-th line equal
low = mid;
high = mid - 1;
break;
}
}
// alphabetizedB_.chars_.insert(offset, str)
// alphabetizedB_.indexs_.add(alphabetizedB_.chars_.length())
if (alphabetizedB_.indexs_.size() == 0) {
alphabetizedB_.indexs_.add(0);
alphabetizedB_.chars_.append(current_shiftB);
} else {
if (low > alphabetizedB_.indexs_.size() - 1) {
alphabetizedB_.indexs_.add(alphabetizedB_.chars_.length());
alphabetizedB_.chars_.append(current_shiftB);
} else {
alphabetizedB_.chars_.insert(alphabetizedB_.indexs_
.get(low), current_shiftB);
alphabetizedB_.indexs_.add(0);
for (int k = alphabetizedB_.indexs_.size() - 1; k > low; k--) {
alphabetizedB_.indexs_.set(k, alphabetizedB_.indexs_
.get(k - 1)
+ current_shiftB.length());
}
}
}
// increment the count of alphabetized shifted lines
alphabetized_count++;
}
}