We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Quicksort 2 - Sorting
  2. Discussions

Quicksort 2 - Sorting

Problem
Submissions
Leaderboard
Discussions

    You are viewing a single comment's thread. Return to all comments →

  • patelmit7474
    6 months ago+ 0 comments

    **Java Solution: **

    static void quickSort(int[] ar, int start, int end) {
                if (start < end) {
                int pi = partition(ar, start, end);
                quickSort(ar, start, pi - 1);
                quickSort(ar, pi + 1, end);
                printArray(ar, start, end);
                }
    	    }
            
            static int partition(int[] ar, int start, int end) {
                int pivot = ar[start];
                int i = start + 1, j = end;
                
                while (i <= j) {
                    if (ar[i] > pivot && ar[j] < pivot) {
                        int temp = ar[i];
                        ar[i] = ar[j];
                        ar[j] = temp;
                    }
                    
                    while (i <= end && ar[i] < pivot) i++;
                    while (j > start && ar[j] > pivot) j--;
                }
                ar[start] = ar[j];
                ar[j] = pivot;
                return j;
            }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy