Skip to content

Commit 48cf263

Browse files
committed
Added Shell Sort and Quick Sort
1 parent 9b77675 commit 48cf263

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

QuickSort.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class QuickSort
2+
{
3+
public static void main(String[]args)
4+
{
5+
int a[]={10,75,-15,17,0,-2};
6+
QSort(a,0,a.length-1);
7+
System.out.println("Sorted Array : ");
8+
System.out.print("[");
9+
for(int i=0;i<a.length;i++)
10+
{
11+
if(i == a.length-1)
12+
System.out.print(a[i]);
13+
else
14+
System.out.print(a[i] + ", ");
15+
}
16+
System.out.println("]");
17+
}
18+
19+
public static void QSort(int a[],int l,int h)
20+
{
21+
if(l<h)
22+
{
23+
int loc=Partion(a,l,h);
24+
QSort(a,l,loc-1);
25+
QSort(a,loc+1,h);
26+
}
27+
}
28+
29+
public static int Partion(int a[],int l,int h)
30+
{
31+
int pivot = a[l];
32+
int start=l;
33+
int end=h;int tmp;
34+
35+
while(start < end)
36+
{
37+
while(start<=h && a[start]<=pivot)
38+
{
39+
start++;
40+
}
41+
while(a[end]>pivot)
42+
{
43+
end--;
44+
}
45+
if(start<end)
46+
{
47+
tmp=a[start];
48+
a[start]=a[end];
49+
a[end]=tmp;
50+
51+
52+
}
53+
54+
}
55+
56+
57+
tmp=a[l];
58+
a[l]=a[end];
59+
a[end]=tmp;
60+
return end;
61+
}
62+
}

ShellSort.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public class ShellSort
2+
{
3+
public static void main(String[] args)
4+
{
5+
int a[]={25,-17,8,92,-21,0};
6+
7+
for(int gap=a.length/2;gap>0;gap/=2)
8+
{
9+
10+
for(int i=gap;i<a.length;i++)
11+
{
12+
int tmp=a[i];
13+
int j=i;
14+
15+
while(j>=gap &&a[j-gap]>tmp)
16+
{
17+
a[j]=a[j-gap];
18+
j-=gap;
19+
}
20+
a[j]=tmp;
21+
22+
}
23+
}
24+
25+
System.out.println("Sorted Array : ");
26+
System.out.print("[");
27+
for(int i=0;i<a.length;i++)
28+
{
29+
if(i == a.length-1)
30+
System.out.print(a[i]);
31+
else
32+
System.out.print(a[i] + ", ");
33+
}
34+
System.out.println("]");
35+
}
36+
37+
}

0 commit comments

Comments
 (0)