-
Notifications
You must be signed in to change notification settings - Fork 18
/
Sort.java
194 lines (190 loc) · 3.09 KB
/
Sort.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import java.io.*;
import java.lang.*;
class Sort
{
public static void main(String args[])throws IOException
{
int ch;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
do
{
System.out.println("\n\n\n1.Bubble Sort\n2.Selection Sort\n3.Insertion Sort.\n4.Quick Sort.\n5.Merge Sort.\n6.Exit.");
ch=Integer.parseInt(br.readLine());
if(ch==6)
return;
System.out.println("Enter n");
int n=Integer.parseInt(br.readLine());
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=Integer.parseInt(br.readLine());
}
switch(ch)
{
case 1:
BubbleSort(a,n);
break;
case 2:
SelectionSort(a,n);
break;
case 3:
InsertionSort(a,n);
break;
case 4:
int start=0;
int end=n-1;
QuickSort(a,start,end);
print(a,n);
break;
case 5:
MergeSort(a,n);
print(a,n);
break;
}
}
while(ch!=6);
}
public static void BubbleSort(int a[],int n)
{
int temp;
boolean swap;
for(int i=0;i<n-1;i++)
{
swap=false;
for(int j=0;j<n-1;j++)
{
if(a[j]>a[(j+1)])
{
temp=a[j];
a[j]=a[(j+1)];
a[(j+1)]=temp;
swap=true;
}
//System.out.print(a[j]);
}
//System.out.println();
if(swap==false)
break;
}
print(a,n);
}
public static void SelectionSort(int a[], int n)
{
for (int i=0;i<n-1;i++)
{
int imin=i;
int temp;
for(int j=i+1;j<n;j++)
{
if(a[j]<a[imin])
imin=j;
}
temp=a[i];
a[i]=a[imin];
a[imin]=temp;
}
print(a,n);
}
public static void InsertionSort(int a[],int n)
{
for(int i=1;i<n;i++)
{
int val=a[i];
int hole=i;
while(hole>0&&a[hole-1]>val)
{
a[hole]=a[hole-1];
hole=hole-1;
}
a[hole]=val;
}
print(a,n);
}
public static void MergeSort(int a[],int n)
{
if(n<=1)
return;
int mid=n/2;
int left[]=new int[mid];
int right[]=new int[n-mid];
for(int i=0;i<mid;i++)
left[i]=a[i];
for(int i=mid;i<n;i++)
right[i-mid]=a[i];
MergeSort(left,mid);
MergeSort(right,n-mid);
Merge(left,right,a);
}
public static void Merge(int left[],int right[],int a[])
{
int nL=left.length;
int nR=right.length;
int i,j,k;
i=j=k=0;
while(i<nL&&j<nR)
{
if(left[i]<=right[j])
{
a[k]=left[i];
i++;
k++;
}
else
{
a[k]=right[j];
j++;
k++;
}
}
while(i<nL)
{
a[k]=left[i];
i++;
k++;
}
while(j<nR)
{
a[k]=right[j];
j++;
k++;
}
}
public static void QuickSort(int a[],int start,int end)
{
if(start<end)
{
int pIndex=QuickPartition(a,start,end);
QuickSort(a,start,pIndex-1);
QuickSort(a,pIndex+1,end);
}
else
return;
}
public static int QuickPartition(int a[],int start,int end)
{
int temp;
int pivot=a[end];
int pIndex=start;
for(int i=start;i<end;i++)
{
if(a[i]<=pivot)
{
//swap a[i],apindex
temp=a[i];
a[i]=a[pIndex];
a[pIndex]=temp;
pIndex++;
}
}
temp=a[pIndex];
a[pIndex]=a[end];
a[end]=temp;
return pIndex;
}
public static void print(int a[],int n)
{
System.out.println();
for(int i=0;i<n;i++)
System.out.print(a[i]+"\t");
}
}