Sponsor

test

Sample Text

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

About & Social

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla elementum viverra pharetra. Nulla facilisis, sapien non pharetra venenatis, tortor erat tempus est, sed accumsan odio ante ac elit. Nulla hendrerit a est vel ornare. Proin eu sapien a sapien dignissim feugiat non eget turpis. Proin at accumsan risus. Pellentesque nunc diam, congue ac lacus

My First Blog Page blog.codingninjas.in Coding Ninja first Blog ...

Search This Blog

Archive

Tags

Post Top Ad

ads

Latest Admit Cards

Beauty

Latest Admissions

Hot

Latest Syllabus

Latest Answer Key

About Us

Recent

Subscribe To Get All The Latest Updates!

email updates

Recent Posts

ads

Post Top Ad

Quick Sort Code

No comments :

Sort an array A using Quick Sort.

Change in the input array itself. So no need to return or print anything.

Input format :
Line 1 : Integer n i.e. Array size
Line 2 : Array elements (separated by space)
Output format :
Array elements in increasing order (separated by space)
Constraints :

1 <= n <= 1000

Sample Input:
6 
2 6 8 5 4 3
Sample Output:
2 3 4 5 6 8
Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.
  1. Always pick first element as pivot.
  2. Always pick last element as pivot (implemented below)
  3. Pick a random element as pivot.
  4. Pick median as pivot.
The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.


public class Solution {
    
    
    private static int findIndex(int[] input,int low,int high,int pivot)
    {
        int pI = low;
        
        int itr=low;
            
            while(itr<=high)
            {
                if(input[itr]<=pivot)
                {
                    int temp = input[itr];
                    
                    input[itr] = input[pI];
                    input[pI] = temp;
                    
                    itr++;
                    pI++;
                }
                else
                {
                    itr++;
                }
            }
        
        return pI-1;
    }
    
    private static void quickSort(int[] input,int low ,int high)
    {
        if(low>=high)
        {
            return;
        }
        
        
        int pivot = input[high];
        
        int pivotIndex = findIndex(input,low,high,pivot);
        
        
        quickSort(input,low,pivotIndex-1);
        
        quickSort(input,pivotIndex +1,high);
    }
 
 public static void quickSort(int[] input) {
        
        int low =0;
        int high = input.length-1;
        
        quickSort(input,low,high);
  
  
 }
 
}

No comments :

Post a Comment