Skip to content

Add Sorting algorithm QuickSortMedianOfThree #294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions Sorts/QuickSortMedianOfThree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

public class QuickSortMedianOfThree {

public void sort(int[] array, int leftIndex, int rightIndex){

if (leftIndex < rightIndex) {

medianOfThree(array, leftIndex, rightIndex);
changePivot(array, leftIndex, rightIndex);

int pivotIndex = partition(array, leftIndex, rightIndex);

sort(array, leftIndex, pivotIndex - 1);
sort(array, pivotIndex + 1, rightIndex);

}
}

private void medianOfThree(int[] array, int leftIndex, int rightIndex){

int shortter = leftIndex;
int mid = leftIndex + (rightIndex - leftIndex) / 2;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write instead int mid = (rightIndex + leftIndex) / 2;. Because simpler!

int bigger = rightIndex;

if (array[mid] < array[shortter]) {
swap(array, shortter, mid);
}
if (array[bigger] < array[shortter]) {
swap(array, shortter, bigger);
}
if (array[bigger] < array[mid]) {
swap(array, bigger, mid);
}

}

private void changePivot(int[] array, int leftIndex, int rightIndex) {
int pivotIndex = leftIndex + (rightIndex - leftIndex) / 2;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write instead int pivotIndex = (rightIndex + leftIndex) / 2;. Because simpler.

if (rightIndex - leftIndex >= 1) {
swap(array, pivotIndex, rightIndex - 1);
}

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put in some comments in your code.

private int partition(int[] array, int leftIndex, int rightIndex) {

int i = leftIndex;
int j = leftIndex + 1;
int p = leftIndex;

while (j <= rightIndex) {

if (array[j] < array[p]) {
i = i + 1;
swap(array, i, j);
}
j = j + 1;
}

swap(array, i, p);
return i;
}

private void swap(int[] array, int i, int j) {
if(array == null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Use curly braces.
  • This exception handling you can write to the methods above, too.

throw new IllegalArgumentException();

int temp = array[i];
array[i] = array[j];
array[j] = temp;
}

public static void main(String args[]) {

int[] array = new int[] {6, 41, 32, 7, 26, 4, 37, 49, 11, 18, 36};
int leftIndex = 0;
int rightIdex = array.length - 1;

QuickSortMedianOfThree quick = new QuickSortMedianOfThree();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you write the methods above as static you can use the methods right on.

quick.sort(array, leftIndex, rightIdex);

System.out.println("The sorted array is:");
for (int i=0; i < array.length; i++)
System.out.print(array[i] + " ");

}

}