Quick Sort in Data Structures using Java

Program 1

/// Program for Quick sort
import java.util.*;
class MyClass
{

  void quick_sort(int ar[],int l,int h)
{
      int low,high,key,temp;  
       low=l;
       high=h;
       key=ar[(low+high)/2];
   do
   {
      while(key>ar[low])
      {
            low++;
      }  
     while(key<ar[high])
    {
       high--;
    }      
    if(low<=high)
    {
     temp=ar[low];
     ar[low++]=ar[high];
     ar[high--]=temp; 
    }

   }while(low<=high); 
    
 if(l<high) 
  quick_sort(ar,l,high);
 if(low<h)
  quick_sort(ar,low,h);
}
}

class QuickSort
{
    public static void main(String args[])
    {
                int ar[],n,i,low,high;
                Scanner scan=new Scanner(System.in);
                System.out.println("Enter limit of array");
                n=scan.nextInt();
                ar=new int[n];
                System.out.println("Enter elements in array");
                for(i=0;i<n;i++)
                  ar[i]=scan.nextInt();
                  low=0;
                  high=n-1;
                  MyClass M=new MyClass();
                  M.quick_sort(ar,low,high);
                  System.out.println("Sorted elements ");
                  for(i=0;i<n;i++)
                  System.out.println(ar[i]);

    }
}

 

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *