Skip to content

Commit 7351129

Browse files
authored
Update cyclesort.md
1 parent 58b4c02 commit 7351129

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

lessons/cyclesort.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,16 @@ It is optimal in terms of number of memory writes. It minimizes the number of me
1616

1717

1818
## 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`.
2020

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`.
2323

2424
## Code Implementation
2525

2626
```
2727
//Java program for implementation of Cyclic Sort
2828
29-
package com.Rahul;
3029
3130
public class CyclicSort {
3231
public static void main(String[] args) {
@@ -55,23 +54,22 @@ public class CyclicSort {
5554
arr[second] = temp;
5655
}
5756
58-
5957
}
6058
```
6159

6260
### Explanation of the example arr [] = {5, 4, 3, 2, 1}
6361

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 ).
6563

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 ).
6866

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
7068

7169
Step 5: Repeat same for all elements.
7270

7371
## Complexity analysis
7472

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)**.
7674

7775

0 commit comments

Comments
 (0)