Skip to content

Commit 59ec7fc

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#69 from mehakagrawal/patch-1
Quick Sort
2 parents 02f508f + 615d347 commit 59ec7fc

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

quick sort

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
public class QuickSort {
2+
public static void main(String[] args) {
3+
int i;
4+
int[] arr={90,23,101,45,65,23,67,89,34,23};
5+
quickSort(arr, 0, 9);
6+
System.out.println("\n The sorted array is: \n");
7+
for(i=0;i<10;i++)
8+
System.out.println(arr[i]);
9+
}
10+
public static int partition(int a[], int beg, int end)
11+
{
12+
13+
int left, right, temp, loc, flag;
14+
loc = left = beg;
15+
right = end;
16+
flag = 0;
17+
while(flag != 1)
18+
{
19+
while((a[loc] <= a[right]) && (loc!=right))
20+
right--;
21+
if(loc==right)
22+
flag =1;
23+
elseif(a[loc]>a[right])
24+
{
25+
temp = a[loc];
26+
a[loc] = a[right];
27+
a[right] = temp;
28+
loc = right;
29+
}
30+
if(flag!=1)
31+
{
32+
while((a[loc] >= a[left]) && (loc!=left))
33+
left++;
34+
if(loc==left)
35+
flag =1;
36+
elseif(a[loc] <a[left])
37+
{
38+
temp = a[loc];
39+
a[loc] = a[left];
40+
a[left] = temp;
41+
loc = left;
42+
}
43+
}
44+
}
45+
returnloc;
46+
}
47+
static void quickSort(int a[], int beg, int end)
48+
{
49+
50+
int loc;
51+
if(beg<end)
52+
{
53+
loc = partition(a, beg, end);
54+
quickSort(a, beg, loc-1);
55+
quickSort(a, loc+1, end);
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)