-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathQuickSortADT.java
72 lines (55 loc) · 1.52 KB
/
QuickSortADT.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
import java.util.*;
public class QuickSortADT
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n; // n is number of elements
int a[],i; // array reference
System.out.println("Enter the size");
n = sc.nextInt();
a = new int[n]; // dynamic memory allocation for array 'a'
System.out.println("Enter elements..."); // input the array
for(i=0;i<n;i++)
a[i] = sc.nextInt();
System.out.println("Before Sorting....");
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
quickSort(a,0,n-1);
System.out.println("\nAfter Sorting....");
for(i=0;i<n;i++)
System.out.print(a[i]+" ");
}
public static void quickSort(int a[], int left, int right)
{
int pivot, l,u,temp;
l= left;
u = right;
pivot = left; // pivot or key starts always from leftmost item
while(left!=right)
{
while((a[right] >= a[pivot] ) && (left!=right)) // R to L
right--;
if(left!=right) // swap
{
temp = a[pivot];
a[pivot] = a[right];
a[right] = temp;
pivot = right; // change the pos of pivot
}
while(( a[left]<=a[pivot] ) && (left!=right))// L to R
left++;
if(left!=right) // swap
{
temp = a[pivot];
a[pivot] = a[left];
a[left] = temp;
pivot = left; // change the pos of pivot
}
}
if(l<pivot) // apply quicksort if left sublist exists
quickSort(a,l,pivot-1);
if(pivot<u) //apply quicksort if right sublist exists
quickSort(a,pivot+1,u);
}
}