File tree Expand file tree Collapse file tree 1 file changed +5
-4
lines changed
src/main/java/algorithms/sorting/cyclicSort/generalised Expand file tree Collapse file tree 1 file changed +5
-4
lines changed Original file line number Diff line number Diff line change @@ -13,18 +13,19 @@ public class CyclicSort {
13
13
*/
14
14
public static void cyclicSort (int [] arr , int n ) {
15
15
for (int currIdx = 0 ; currIdx < n - 1 ; currIdx ++) {
16
- int currElement = arr [ currIdx ] ;
16
+ int currElement ;
17
17
int rightfulPos ;
18
18
19
19
do {
20
20
rightfulPos = currIdx ; // initialization since elements before currIdx are correctly placed
21
+ currElement = arr [currIdx ];
21
22
for (int i = currIdx + 1 ; i < n ; i ++) { // O(n) find rightfulPos for the currElement
22
23
if (arr [i ] < currElement ) {
23
24
rightfulPos ++;
24
25
}
25
- if ( rightfulPos == currIdx ) { // verified curr position is correct for curr element
26
- break ;
27
- }
26
+ }
27
+ if ( rightfulPos == currIdx ) { // verified curr position is correct for curr element
28
+ break ;
28
29
}
29
30
while (currElement == arr [rightfulPos ]) { // duplicates found, so find next suitable position
30
31
rightfulPos ++;
You can’t perform that action at this time.
0 commit comments