-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathQuickSort.java
More file actions
95 lines (73 loc) · 1.73 KB
/
QuickSort.java
File metadata and controls
95 lines (73 loc) · 1.73 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.util.Scanner;
public class QuickSort {
static int [] enter_data()
{
int i,n;
Scanner sc = new Scanner(System.in);
System.out.print("Enter no. of elements u want to sort :");
n=sc.nextInt();
int a[]=new int[n];
System.out.println("Enter elements--->");
for(i=0;i<n;i++)
{
System.out.print((i+1)+" Element : ");
a[i]=sc.nextInt();
}
return a;
}
static void quicksort(int a[],int p,int r)
{
if(p<r)
{
int q;
q=partition(a,p,r);
quicksort(a,p,q);
quicksort(a,q+1,r);
}
}
static int partition(int a[],int p,int r)
{
int i, j, pivot, temp;
pivot = a[p];
i = p;
j = r;
while(true)
{
while(a[i] < pivot && a[i] != pivot)
{
i++;
}
while((a[j] > pivot) && (a[j] != pivot))
{
j--;
}
if(i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else
{
return j;
}
}
}
static void printarray(int arr[])
{
int i,len;
len=arr.length;
System.out.println("\nAfter Sorting Elements are : ");
for(i=0;i<len;i++)
{
System.out.println((i+1)+" Element : "+arr[i]);
}
}
public static void main(String[] args) {
int a[],b[],p=0,r;
a=enter_data();
r=a.length;
quicksort(a,p,r-1);
printarray(a);
}
}