11 Dec 2017

write a program for Quick sort.




public class QuickSort {
          private int array[];
          private int length;

          public void sort(int[] inputArr) {
                   if (inputArr == null || inputArr.length == 0) {
                             return;
                   }
                   this.array = inputArr;
                   length = inputArr.length;
                   quickSort(0, length - 1);
          }

          private void quickSort(int lowerIndex, int higherIndex) {
                   int i = lowerIndex;
                   int j = higherIndex;
                   int pivot = array[lowerIndex + (higherIndex - lowerIndex) / 2];
                   while (i <= j) {
                             while (array[i] < pivot)
                                      i++;
                             while (array[j] > pivot)
                                      j--;
                             if (i <= j) {
                                      exchangeNumbers(i, j);
                                      i++;
                                      j--;
                             }
                   }
                   if (lowerIndex < j)
                             quickSort(lowerIndex, j);
                   if (i < higherIndex)
                             quickSort(i, higherIndex);
          }

          private void exchangeNumbers(int i, int j) {
                   int temp = array[i];
                   array[i] = array[j];
                   array[j] = temp;
          }

          public static void main(String a[]) {
                   QuickSort sorter = new QuickSort();
                   int[] input = { 2, 5, 2, 8, 5, 6, 8, 8 };
                   sorter.sort(input);
                   System.out.println("Sorted List is::");
                   for (int i : input) {
                             System.out.print(i);
                             System.out.print(" ");
                   }
          }
}

Output:

Sorted list is ::
2 2 5 5 6 8 8 8