importjava.io.BufferedReader;importjava.io.InputStreamReader;importjava.util.Arrays;importjava.util.Random;publicclassDay30{staticintpartition(int[]array,intlow,inthigh){intj,temp,i=low+1;Randomrandom=newRandom();intx=random.nextInt(high-low)+low;temp=array[low];array[low]=array[x];array[x]=temp;for(j=low+1;j<=high;j++){if(array[j]<=array[low]&&j!=i){temp=array[j];array[j]=array[i];array[i++]=temp;}elseif(array[j]<=array[low]){i++;}}temp=array[i-1];array[i-1]=array[low];array[low]=temp;returni-1;}staticvoidDay30(int[]array,intlow,inthigh){if(low<high){intmid=partition(array,low,high);Day30(array,low,mid-1);Day30(array,mid+1,high);}}publicstaticvoidmain(String[]args){BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));intsize;System.out.println("Enter the size of the array");try{size=Integer.parseInt(br.readLine());}catch(Exceptione){System.out.println("Invalid Input");return;}int[]array=newint[size];System.out.println("Enter array elements");inti;for(i=0;i<array.length;i++){try{array[i]=Integer.parseInt(br.readLine());}catch(Exceptione){System.out.println("Error");}}System.out.println("The initial array is");System.out.println(Arrays.toString(array));Day30(array,0,array.length-1);System.out.println("The sorted array is");System.out.println(Arrays.toString(array));}}
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)