- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoogeSort.java
More file actions
Latest commit
95 lines (40 loc) · 1.26 KB
/
Copy pathStoogeSort.java
File metadata and controls
95 lines (40 loc) · 1.26 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
importjava.util.Random;
publicclassStooge_Sort
{
publicstaticintN = 20;
publicstaticint[] sequence = newint[N];
publicstaticint[] stoogeSort(int[] L, inti, intj)
{
if (L[j] < L[i])
{
intswap = L[i];
L[i] = L[j];
L[j] = swap;
}
if ((j - i + 1) >= 3)
{
intt = (j - i + 1) / 3;
stoogeSort(L, i, j - t);
stoogeSort(L, i + t, j);
stoogeSort(L, i, j - t);
}
returnL;
}
publicstaticvoidprintSequence(int[] sorted_sequence)
{
for (inti = 0; i < sorted_sequence.length; i++)
System.out.print(sorted_sequence[i] + " ");
}
publicstaticvoidmain(String[] args)
{
Randomrandom = newRandom();
System.out
.println("Sorting of randomly generated numbers using STOOGE SORT");
for (inti = 0; i < N; i++)
sequence[i] = Math.abs(random.nextInt(1000));
System.out.println("\nOriginal Sequence: ");
printSequence(sequence);
System.out.println("\nSorted Sequence: ");
printSequence(stoogeSort(sequence, 0, sequence.length - 1));
}
}