You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lessons/cyclesort.md
+8-10Lines changed: 8 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,17 +16,16 @@ It is optimal in terms of number of memory writes. It minimizes the number of me
16
16
17
17
18
18
## Algorithm
19
-
Consider an array of n distinct elements. An element a is given, index of a can be calculated by counting the number of elements that are smaller than a.
19
+
Consider an array of `n` distinct elements. An element `a` is given, index of `a` can be calculated by counting the number of elements that are smaller than `a`.
20
20
21
-
1.if the element is found to be at its correct position, simply leave it as it is.
22
-
2. Otherwise, find the correct position of a by counting the total number of elements that are less than a. where it must be present in the sorted array. The other element b which is replaced is to be moved to its correct position. This process continues until we got an element at the original position of a.
21
+
1.If the element is found to be at its correct position, simply leave it as it is.
22
+
2. Otherwise, find the correct position of `a` by counting the total number of elements that are less than `a`. where it must be present in the sorted array. The other element `b` which is replaced is to be moved to its correct position. This process continues until we got an element at the original position of `a`.
23
23
24
24
## Code Implementation
25
25
26
26
```
27
27
//Java program for implementation of Cyclic Sort
28
28
29
-
package com.Rahul;
30
29
31
30
public class CyclicSort {
32
31
public static void main(String[] args) {
@@ -55,23 +54,22 @@ public class CyclicSort {
55
54
arr[second] = temp;
56
55
}
57
56
58
-
59
57
}
60
58
```
61
59
62
60
### Explanation of the example arr [] = {5, 4, 3, 2, 1}
63
61
64
-
Step 1: Count the number of elements less than 5, there are 4 elements less than 5 . move 5 to 5th place in the array ( Index = 4 ).
62
+
Step 1: Count the number of elements less than 5, there are 4 elements less than 5 . move `5` to 5th place in the array ( Index = 4 ).
65
63
66
-
Step 2: So, 5 will take the position of 1 and then count the number of elements less than 1, there are no elements less than 1 .
67
-
move 1 to 1st place in the array ( Index = 0 ).
64
+
Step 2: So, 5 will take the position of 1 and then count the number of elements less than 1, there are no elements less than 1 .
65
+
move `1` to 1st place in the array ( Index = 0 ).
68
66
69
-
Step 3: The original position of 5 is acquired . one cycle is completed
67
+
Step 3: The original position of 5 is acquired and one cycle is completed
70
68
71
69
Step 5: Repeat same for all elements.
72
70
73
71
## Complexity analysis
74
72
75
-
The time complexity of the cyclic sort is O(n). The while loop, in the worst case can iterate a maximum of 2n-1 times. As you can see, we are not incrementing the index i when swapping the numbers, this will result in more than ‘n’ iterations of the loop, but in the worst-case scenario, the while loop will swap a total of ‘n-1’ numbers and once a number is at its correct index, we will move on to the next number by incrementing i . So overall, our algorithm will take O(n) + O(n-1) or we can say O(2n-1) which is asymptotically equivalent to O(n).
73
+
The time complexity of the cyclic sort is **O(n)**. The while loop, in the worst case, can iterate a maximum of `2n-1` times. As you can see, we are not incrementing the index `i` when swapping the numbers, this will result in more than `n` iterations of the loop, but in the worst-case scenario, the while loop will swap a total of `n-1` numbers and once a number is at its correct index, we will move on to the next number by incrementing `i`. So overall, our algorithm will take **O(n) + O(n-1)** or we can say **O(2n-1)** which is asymptotically equivalent to **O(n)**.
0 commit comments