-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomiseQuick.java
More file actions
68 lines (53 loc) · 1.29 KB
/
randomiseQuick.java
File metadata and controls
68 lines (53 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.*;
import java.util.Scanner;
import java.util.Arrays;
class main{
public static void quickSort(int arr[], int begin, int end) {
if (begin < end) {
randomise(arr,begin,end);
int partitionIndex = partition(arr, begin, end);
quickSort(arr, begin, partitionIndex-1);
quickSort(arr, partitionIndex+1, end);
}
}
public static void randomise(int arr[],int l,int r){
Random rand= new Random();
int temp=rand.nextInt(r-l)+l;
int swap=arr[temp];
arr[temp]=arr[r];
arr[r]=swap;
}
public static int partition(int arr[], int begin, int end) {
int pivot = arr[end];
int i = begin;
for (int j = begin; j < end; j++) {
if (arr[j] <pivot) {
int swapTemp = arr[i];
arr[i] = arr[j];
arr[j] = swapTemp;
i++;
}
}
int swapTemp = arr[i];
arr[i] = arr[end];
arr[end] = swapTemp;
return i;
}
public static void display(int arr[]){
int n=arr.length;
for(int i=0;i<n;i++)
System.out.println(arr[i]);
}
public static void main(String args[]){
int i,j,n;
Scanner sc=new Scanner (System.in);
n=sc.nextInt();
int arr[] =new int[n];
for(i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
quickSort(arr,0,n-1);
display(arr);
}
}