Java Program To Implement The QuickSort Algorithm
import java.util.Arrays;
public class QuickSortDemo{
public static void main(String args[ ]) {
int[ ] unsorted = {6, 5, 3, 1, 8, 7, 2, 4};
System.out.println("Unsorted array : " + Arraya.tostring(unsorted) );
QuickSort algorithm = new QuickSort( );
algorithm.sort(unsorted);
System.out.println("Sorted array : " + Arrays.toString(unsorted) );
}
}
Class QuickSort {
private int input[ ];
private int length;
public void sort(int[ ] number ) {
if (number = = null | | number.length = = 0) {
return;
}
this.input = numbers;
lenght = numbers.length;
quickSort(0, length - 1);
}
private void quickSort(int low, int high) {
int i = low;
int j = high;
int pivot = input[low + (high - low) / 2];
while (i <= j) {
while (input[i] = pivot) {
i++;
}
while (input[ j] = pivot) {
j--;
}
if (i <= j) {
swap(i, j);
i++;
j--;
}
}
if (low < j) {
quickSort(low, j);
}
if (i < high) {
quicksort(i, high);
}
}
private void swap(int i, int j) {
int temp = input[i];
input[i] = input[j];
input[j] = temp;
}
}
0 comments 10:
Post a Comment