Skip to content

Commit b376504

Browse files
Utkarsh MishraUtkarsh Mishra
authored andcommitted
site fixed
1 parent abdc27c commit b376504

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

lessons/cyclesort.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
---
2-
path: "Cyclic"
2+
path: "/cyclesort"
33
title: "Cyclic Sort"
44
order: "5E"
55
section: "Searching & Sorting"
66
description: "learn Sorting algorithms"
77
---
88

9-
109
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.
1110

12-
1311
It is an in-place and unstable sorting algorithm.
1412

1513
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.
1614

17-
1815
## Algorithm
16+
1917
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`.
2018

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

2422
## Code Implementation
2523

@@ -33,8 +31,8 @@ public class CyclicSort {
3331
sort(arr);
3432
System.out.println(Arrays.toString(arr));
3533
}
36-
37-
34+
35+
3836
//Function to sort array using Cyclic sort
3937
public static void sort(int[] arr) {
4038
int i = 0;
@@ -47,7 +45,7 @@ public class CyclicSort {
4745
}
4846
}
4947
}
50-
//Function to swap two elements
48+
//Function to swap two elements
5149
public static void swap(int[] arr, int first, int second) {
5250
int temp = arr[first];
5351
arr[first] = arr[second];
@@ -62,14 +60,12 @@ public class CyclicSort {
6260
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 ).
6361

6462
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 ).
6664

6765
Step 3: The original position of 5 is acquired and one cycle is completed
6866

6967
Step 5: Repeat same for all elements.
7068

7169
## 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)**.
7470

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

Comments
 (0)