Skip to content

Conversation

@RahulSoni0
Copy link
Contributor

Fixes #24

Short description of what this resolves:

Added a Cyclic sort page.

What Changes proposed in this pull request:

  • Introduction to Cycle Sort
  • Complete explanation on Cycle Sort
  • Code for Cycle Sort
  • Explanation of Code Example
  • Complexity Analysis of Cycle Sort

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • Added code examples, test must pass
  • Added documentation so that the program is self-explanatory and educational.
  • Relevant documentation/comments is changed or added
  • PR title follows semantic commit guidelines
  • I acknowledge that all my contributions will be made under the project's license.

Notes:


## Complexity analysis

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).
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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).
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)**.

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 .
move 1 to 1st place in the array ( Index = 0 ).

Step 3 : The original position of 5 is acquired . one cycle is completed
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Step 3 : The original position of 5 is acquired . one cycle is completed
Step 3: The original position of 5 is acquired and one cycle is completed


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

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 .
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 .
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.


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

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 ).
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 ).
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 ).

Comment on lines 57 to 58


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

remove these extra lines

```
//Java program for implementation of Cyclic Sort

package com.Rahul;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
package com.Rahul;

remove this line

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.

1. if the element is found to be at its correct position, simply leave it as it is.
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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
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`.

## Algorithm
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.

1. if the element is found to be at its correct position, simply leave it as it is.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
1. if the element is found to be at its correct position, simply leave it as it is.
1. If the element is found to be at its correct position, simply leave it as it is.



## Algorithm
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.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
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`.
``

Copy link
Owner

@Utkarsh1504 Utkarsh1504 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RahulSoni0 A few small changes, and your PR is ready for merge.
Thanks for your work.🎈🎉

@RahulSoni0
Copy link
Contributor Author

Done . Did all the changes. 😊 You Can Check
And thanks for this opportunity @Utkarsh1504

@Utkarsh1504
Copy link
Owner

Great work @RahulSoni0 ✨🎇, Thanks for your contribution, it is super helpful.🎈🎉✨

@Utkarsh1504 Utkarsh1504 added approved for approved PRS hacktoberfest-accepted eligible PRs for hactoberfest ✅ Ready for merge approved pull request labels Oct 1, 2021
@Utkarsh1504 Utkarsh1504 merged commit 824ba75 into Utkarsh1504:main Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved for approved PRS hacktoberfest-accepted eligible PRs for hactoberfest ✅ Ready for merge approved pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

new lesson: Cycle Sort

2 participants