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
+9-13Lines changed: 9 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,25 +1,23 @@
1
1
---
2
-
path: "Cyclic"
2
+
path: "/cyclesort"
3
3
title: "Cyclic Sort"
4
4
order: "5E"
5
5
section: "Searching & Sorting"
6
6
description: "learn Sorting algorithms"
7
7
---
8
8
9
-
10
9
Cycle sort is a comparison sorting algorithm which forces array to be factored into the number of cycles where each of them can be rotated to produce a sorted array. It is theoretically optimal in the sense that it reduces the number of writes to the original array.
11
10
12
-
13
11
It is an in-place and unstable sorting algorithm.
14
12
15
13
It is optimal in terms of number of memory writes. It minimizes the number of memory writes to sort. Each value is either written zero times, if it’s already in its correct position, or written one time to its correct position.
16
14
17
-
18
15
## Algorithm
16
+
19
17
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
18
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`.
19
+
1. If the element is found to be at its correct position, simply leave it as it is.
20
+
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`.
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 ).
63
61
64
62
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 ).
63
+
move `1` to 1st place in the array ( Index = 0 ).
66
64
67
65
Step 3: The original position of 5 is acquired and one cycle is completed
68
66
69
67
Step 5: Repeat same for all elements.
70
68
71
69
## Complexity analysis
72
-
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)**.
74
70
75
-
71
+
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