-
Notifications
You must be signed in to change notification settings - Fork 14
/
Quicktest1.java
79 lines (66 loc) · 1.69 KB
/
Quicktest1.java
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
69
70
71
72
73
74
75
76
77
78
79
// Write quick sort pseudocode which chooses pivot as middle element in the array
import java.util.*;
class Sort3
{
public static void swap(int arr[],int a,int b)
{
int temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
public static int partition3(int arr[],int lb,int ub)
{
int pivot=arr[lb];
int start=lb;
int end=ub;
while(start<end)
{
while(arr[start]<=pivot)
{
start++;
}
while(arr[end]>pivot)
{
end--;
}
if(start<end)
{
swap(arr,start,end);
}
}
swap(arr,lb,end);
return end;
}
public static void quicksort3(int arr[],int lb,int ub)
{
if(lb<ub)
{
int mid=(lb+ub)/2;
swap(arr,lb,mid);
int loc=partition3(arr,lb,ub);
quicksort3(arr,lb,loc-1);
quicksort3(arr,loc+1,ub);
}
}
}
public class Quicktest1
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of array is:");
int size=sc.nextInt();
int arr[]=new int[size];
System.out.println("Enter the elements of array:");
for(int i=0;i<arr.length;i++)
{
arr[i]= sc.nextInt();
}
Sort3.quicksort3(arr,0,arr.length-1);
System.out.println("Enter the elements of array:");
for(int i=0;i<arr.length;i++)
{
System.out.println(arr[i]);
}
}
}