Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what mistake m doing help me
Java
public class QuickSortExample {

    public static void main(String[] args) {
        int arr[] = {12, 34, 22, 64, 34, 33, 23, 64, 33};
        int i = 0;
        int j = arr.length;     
        
        while (i < j) {
            i = quickSort(arr, i, i + 1, j - 1);

        }
        for (i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }

    public static int quickSort(int arr[], int pivot, int i, int j) {

        if (i > j) {

            swap(arr, pivot, j);
            return i;
        }

        while (i < arr.length && arr[i] <= arr[pivot]) {

            i++;
        }

        while (j >= 1 && arr[j] >= arr[pivot]) {

            j--;
        }
        if (i < j) {
            swap(arr, i, j);
        }


        return quickSort(arr, pivot, i, j);
    }

    public static void swap(int[] arr, int i, int j) {
        int temp;
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}
Posted
Updated 1-Apr-18 21:27pm
v3
Comments
Sandeep Mewara 22-Jul-12 7:15am    
You should have shared your issue too that you are facing.

 
Share this answer
 
- one Array as input - another one as output. You can see the result better doing it that way. the output array should be class member, so you do not need to pass it around that much.

- consistent naming of variables. You are changing around the names, using a lot of i and j. That's more confusion than needed. Keep it straight, one name for one variable that is fixed for all times. Also choose longer names, i and j are considered for counting in loops only. how about "iNext" ?
 
Share this answer
 
Here's the code for quick sort

Java
public class QuickSort 
{
   int partition(int arrNum[], int low, int high)
   {
      int pivot = arrNum[high]; 
      int a = (low - 1); // smaller element index
      for(int b = low; b < high; b++)
      {
         // condition to check current element is smaller than or equal to pivot
         if(arrNum[b] <= pivot)
         {
            a++;
            // swapping arrNum[a] and arrNum[b]
            int temp = arrNum[a];
            arrNum[a] = arrNum[b];
            arrNum[b] = temp;
         }
      }
 
      // swapping arrNum[a + 1] and arrNum[high]
      int temp = arrNum[a + 1];
      arrNum[a + 1] = arrNum[high];
      arrNum[high] = temp;
 
      return a + 1;
   }
 
   void sortNumber(int arr[], int low, int high)
   {
      if(low < high)
      { 
         int part = partition(arr, low, high);
         // Recursive function sort elements before partition and after partition
         sortNumber(arr, low, part - 1);
         sortNumber(arr, part + 1, high);
      }
   }
 
   // printing utility function
   static void printingArray(int arr[])
   {
      int num = arr.length;
      for(int a = 0; a < num; ++a)
         System.out.print(arr[a] + " ");
      System.out.println();
   }
 
   public static void main(String[] args) 
   {
      int arr[] = {33, 36, 63, 34, 45, 78};
      int n = arr.length;
 
      QuickSort qs = new QuickSort();
      qs.sortNumber(arr, 0, n - 1);
 
      System.out.println("Quicksort sorted array : ");
      printingArray(arr);
   }
}
 
Share this answer
 
Comments
Patrice T 2-Apr-18 5:06am    
6 years too late

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900