Skip to content
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

Count Repetitions #18

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion other/clrs/07/01/02.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ def partition(numbers, start = 0, end = None):

if value == pivot_value:
repetitions += 1
else:
repetitions = 0;

if value <= pivot_value:
numbers[pivot], numbers[i] = numbers[i], numbers[pivot]
pivot += 1

numbers[pivot], numbers[last] = numbers[last], numbers[pivot]
return pivot - repetitions // 2
return (pivot + 1) - repetitions // 2

def quicksort(numbers, start = 0, end = None):
end = end if end else len(numbers)
Expand Down
2 changes: 1 addition & 1 deletion other/clrs/07/02/03.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
> contains distict elements and is sorted in decreasing order.

In this case `PARTITION` always returns $p$ because all the elements are
greated than the pivot. While the **if** will never be executed, we still get
greater than the pivot. While the **if** will never be executed, we still get
one empty partition and the recurrence is $T(n) = T(n-1) + \Theta(n)$ (even if
its body is not executed, the **for** is still $\Theta(n)$).
6 changes: 3 additions & 3 deletions other/clrs/07/02/04.markdown
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
> Banks often record transactions on an account in order of the times of the
> transactions, but many people like to receive their bank statements with
> checks listed in order by check numbers. People usually write checks in order
> by check number, and merchants usually cash the with reasonable dispatch. The
> by check number, and merchants usually cash them with reasonable dispatch. The
> problem of converting time-of-transaction ordering to check-number ordering
> is therefore the problem of sorting almost-sorted input. Argue that the
> procedure `INSERTION-SORT` would tend to beat the procedure `QUICKSORT` on
Expand All @@ -14,6 +14,6 @@ The more sorted the array is, the less work insertion sort will do. Namely,
the array. In the example above the number of inversions tends to be small so
insertion sort will be close to linear.

On the other hand, if `PARTITION` does picks a pivot that does not participate
in an inversion, it will produce and empty partition. Since there is a small
On the other hand, if `PARTITION` does pick a pivot that does not participate
in an inversion, it will produce an empty partition. Since there is a small
number of inversions, `QUICKSORT` is very likely to produce empty partitions.