Skip to content

Commit

Permalink
Add: Code example of radix sort with counting sort
Browse files Browse the repository at this point in the history
Add: Code example of radix sort with modified version of counting sort to take in an argument telling which index the sorting is based on
  • Loading branch information
joachmak authored Nov 11, 2020
1 parent 4d607f0 commit f4fe0f6
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Algoritmer/Sortering/radix_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,25 @@ $\Theta(d(n+k))$ | $\Theta(d(n+k))$ | $\Theta(d(n+k))$ | $O(n+k)$
der $d$ er antallet siffer i maksimum, $n$ er antall elementer og $k$ er antallet unike elementer.

## Python kodeeksempel
```python
def modified_counting_sort(A, B, idx): # A = Usortert liste. B = Liste hvor resultatet skal puttes. idx = indeksen som sorteringen skal basere seg på
k = 10
C = [0]*(k+1)
for i in range(len(A)):
index = int(str(A[i])[idx])
C[index] += 1
for i in range(1, k+1):
C[i] += C[i-1]
for i in range(len(A)-1, -1, -1):
index = int(str(A[i])[idx])
B[C[index]-1] = A[i]
C[index] -= 1
return B


def radix_sort(A, d):
for i in range(d-1, -1, -1):
B = [0]*len(A)
A = modified_counting_sort(A,B,i)
return A
```

0 comments on commit f4fe0f6

Please sign in to comment.