-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterative-Quicksort.java
More file actions
97 lines (83 loc) · 2.38 KB
/
Iterative-Quicksort.java
File metadata and controls
97 lines (83 loc) · 2.38 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
96
97
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class IterativeQuickSort
{
char [] swap(char arr[],int i,int j)
{
char t = arr[i];
arr[i] = arr[j];
arr[j] = t;
return arr;
}
/* This function is same in both iterative and
recursive*/
int partition (char arr[], int l, int h)
{
char x = arr[h];
int i = (l - 1);
for (int j = l; j <= h- 1; j++)
{
if (arr[j] <= x)
{
i++;
// swap arr[i] and arr[j]
arr = swap(arr,i,j);
}
}
// swap arr[i+1] and arr[h]
arr = swap(arr,i+1,h);
return (i + 1);
}
// Sorts arr[l..h] using iterative QuickSort
void QuickSort(char arr[], int l, int h)
{
// create auxiliary stack
int stack[] = new int[h-l+1];
// initialize top of stack
int top = -1;
// push initial values in the stack
stack[++top] = l;
stack[++top] = h;
// keep popping elements until stack is not empty
while (top >= 0)
{
// pop h and l
h = stack[top--];
l = stack[top--];
// set pivot element at it's proper position
int p = partition(arr, l, h);
// If there are elements on left side of pivot,
// then push left side to stack
if ( p-1 > l )
{
stack[ ++top ] = l;
stack[ ++top ] = p - 1;
}
// If there are elements on right side of pivot,
// then push right side to stack
if ( p+1 < h )
{
stack[ ++top ] = p + 1;
stack[ ++top ] = h;
}
}
}
// A utility function to print contents of arr
void printArr( char arr[], int n )
{
int i;
for ( i = 0; i < n; ++i )
System.out.print(arr[i]+" ");
}
// Driver code to test above
public static void main(String args[])
{
IterativeQuickSort ob = new IterativeQuickSort();
char arr[] = {'r', 'u', 'd', 'r', 'a', 'n', 'i', 'i'};
ob.QuickSort(arr, 0, arr.length-1);
ob.printArr(arr, arr.length);
}
}