-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathimpr_selec.java
56 lines (46 loc) · 897 Bytes
/
impr_selec.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
class improve
{
static void minimprove(int[] arr, int n)
{
for (int i = 0, j = n - 1; i < j; i++, j--)
{
int min = arr[i], max = arr[i];
int min_i = i, max_i = i;
for (int k = i; k <= j; k++)
{
if (arr[k] > max)
{
max = arr[k];
max_i = k;
}
else if (arr[k] < min)
{
min = arr[k];
min_i = k;
}
}
swaptemp(arr, i, min_i);
if (arr[min_i] == max)
swaptemp(arr, j, min_i);
else
swaptemp(arr, j, max_i);
}
}
static int[] swaptemp(int []arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
return arr;
}
public static void main(String[] args)
{
int arr[] = { 23, 78, 45, 8, 32, 56, 1 };
int n = arr.length;
minimprove(arr, n);
System.out.printf("Sorted array:\n");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.println("");
}
}