-
Notifications
You must be signed in to change notification settings - Fork 97
added Cycle Sort #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added Cycle Sort #35
Conversation
lessons/cyclesort.md
Outdated
|
|
||
| ## 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). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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)**. |
lessons/cyclesort.md
Outdated
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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 |
lessons/cyclesort.md
Outdated
|
|
||
| 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 . |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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. |
lessons/cyclesort.md
Outdated
|
|
||
| ### 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 ). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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 ). |
lessons/cyclesort.md
Outdated
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove these extra lines
lessons/cyclesort.md
Outdated
| ``` | ||
| //Java program for implementation of Cyclic Sort | ||
|
|
||
| package com.Rahul; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| package com.Rahul; |
remove this line
lessons/cyclesort.md
Outdated
| 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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`. |
lessons/cyclesort.md
Outdated
| ## 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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. |
lessons/cyclesort.md
Outdated
|
|
||
|
|
||
| ## 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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`. | |
| `` |
Utkarsh1504
left a comment
There was a problem hiding this 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.🎈🎉
|
Done . Did all the changes. 😊 You Can Check |
|
Great work @RahulSoni0 ✨🎇, Thanks for your contribution, it is super helpful.🎈🎉✨ |
Fixes #24
Short description of what this resolves:
Added a Cyclic sort page.
What Changes proposed in this pull request:
Checklist
Notes: