Skip to content

Commit

Permalink
Add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
yjcho92 committed Oct 1, 2016
1 parent f3892d4 commit 451234b
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion [Algorithm_hw03]/src/Sorting.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public Sorting() {
this.size = 0;
}

/**
* Get pivot value from partition and call recursively quickSort function.
*
* @param left
* @param right
*/
public void quickSort(int left, int right) {
if (left < right) {
int pivot = partition(left, right);
Expand All @@ -24,6 +30,13 @@ public void quickSort(int left, int right) {
}
}

/**
* Pivot is most right value of array.
*
* @param left
* @param right
* @return New pivot index
*/
private int partition(int left, int right) {

int pivot = this.digits[right];
Expand All @@ -39,16 +52,25 @@ private int partition(int left, int right) {
return i;

/*
int pivot = this.digits[left];
int up = left;
int down = right + 1;
do {
do up++; while (this.digits[up] < pivot && up <= this.size);
do down--; while (this.digits[down] > pivot && down >= 1);
if (up < down) swap(up, down);
} while (up < down);
swap(left, down);
return down;
*/
}

/**
* Get random pivot value from randomizedPartition and call recursively quickSort_withRandom function.
*
* @param left
* @param right
*/
public void quickSort_withRandom(int left, int right) {
if (left < right) {
int randomPivot = randomizedPartition(left, right);
Expand All @@ -57,6 +79,15 @@ public void quickSort_withRandom(int left, int right) {
}
}

/**
* Get three random number and extract mid value.
* And swap it with most right index.
* Then call partition function.
*
* @param left
* @param right
* @return New randomized pivot index
*/
private int randomizedPartition(int left, int right) {
Random random = new Random();

Expand Down Expand Up @@ -92,6 +123,9 @@ public void swap(int a, int b) {
this.digits[b] = temp;
}

/**
* Test for small size of array.
*/
public void defaultNumberSetting() {
this.digits[1] = 10;
this.digits[2] = 1;
Expand Down

0 comments on commit 451234b

Please sign in to comment.