Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
47 views

Quick Sort Program in Java

Quicksort is implemented in Java by recursively sorting subarrays of an array. The algorithm selects a pivot element and partitions the array around it such that all elements less than the pivot come before all elements greater than it. The subarrays are then recursively sorted until the entire array is in order. The code takes in an integer array, calls the quicksort method with the array and indices of the first and last elements, and prints the array before and after sorting.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Quick Sort Program in Java

Quicksort is implemented in Java by recursively sorting subarrays of an array. The algorithm selects a pivot element and partitions the array around it such that all elements less than the pivot come before all elements greater than it. The subarrays are then recursively sorted until the entire array is in order. The code takes in an integer array, calls the quicksort method with the array and indices of the first and last elements, and prints the array before and after sorting.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

21.Program to implement Quick Sort? import java.io.

*; class QuickSort1 { public static void qsort(int a[],int l ,int r) { if(l>=r) return; int i=l,j=r+1; int key =a[l]; while(i<j) { do { i++; }while(a[i]<key); do { j--; }while(a[j]>key); if(i>=j) break; int temp ; temp=a[i]; a[i]=a[j]; a[j]=temp; } a[l]=a[j]; a[j]=key; qsort(a,l,j-1); qsort(a,j+1,r); } static void print(int a[]) { for(int i=0;i<a.length;i++) System.out.println(a[i]); } public static void main(String args[]) throws Exception { DataInputStream in =new DataInputStream(System.in); int ar[]=new int[10]; int len=ar.length; System.out.println("enter elements into the array "); for(int i=0;i<ar.length;i++) ar[i]=Integer.parseInt(in.readLine());

System.out.println("Array elements before sorting "); print(ar); qsort(ar,0,len-1); System.out.println("Array elements After sorting "); print(ar); } }

You might also like