In the quicksort lecture I have been taught to take middle item as pivot, I am confused about randomized quicksort.
import java.util.*;
public class Main
{
static void quick(long []a,int low,int high)
{
int left=0,right=a.length-1,pivot=(low+high)/2;
if (low>=high)
{
return;
}
while(left<=right)
{
while(a[left]<a[pivot])
left++;
while(a[right]>a[pivot])
right--;
if(left<=right)
{
long temp=a[left];
a[left]=a[right];
a[right]=temp;
left++;
right--;
}
}
quick(a,low,right);
quick(a,left,high);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
long arr[]=new long[size];
for(int i=0;i<size;i++)
arr[i]=sc.nextInt();
quick(arr,0,arr.length-1);
for(long i:arr)
{
System.out.print(i+" ");
}
}
}
I tried this…it has gone through only 1 test case