- python
- go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func merge(A []int, p, q, r int) { | |
const INTMAX = int(^uint(0) >> 1) | |
A1, A2 := make([]int, q-p), make([]int, r-q) | |
copy(A1, A[p:q]) | |
copy(A2, A[q:r]) | |
A1, A2 = append(A1, INTMAX), append(A2, INTMAX) | |
i, j, k := 0, 0, 0 | |
for k = p; k < r; k++ { | |
if A1[i] >= A2[j] { | |
A[k] = A2[j] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func count(A []int, p, q, r int) int { | |
const INTMAX = int(^uint(0) >> 1) | |
A1, A2 := make([]int, q-p), make([]int, r-q) | |
copy(A1, A[p:q]) | |
copy(A2, A[q:r]) | |
A1, A2 = append(A1, INTMAX), append(A2, INTMAX) | |
i, j, k := 0, 0, 0 | |
count := 0 | |
n1 := len(A1) - 1 | |
for k = p; k < r; k++ { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func partition(A []int, p, r int) int { | |
x, i := A[r-1], p | |
for j := p; j < r-1; j++ { | |
if A[j] <= x { | |
A[i], A[j] = A[j], A[i] | |
i++ | |
} | |
} | |
A[i], A[r-1] = A[r-1], A[i] | |
return i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python -m cProfile target.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Quack.c: an array-based implementation of a quack | |
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct node *Quack; | |
Quack createQuack(void); // create and return Quack | |
Quack destroyQuack(Quack); // remove the Quack | |
void push(int, Quack); // put int on the top of the quack | |
void qush(int, Quack); // put int at the bottom of the quack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// InsertionSortForBucketSort O(n^2) | |
func InsertionSortForBucketSort(A []float64) { | |
for j := 1; j < len(A); j++ { | |
key, i := A[j], j-1 | |
for i >= 0 && A[i] > key { | |
A[i+1] = A[i] | |
i-- | |
} | |
A[i+1] = key | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CountSort O(n + k) | |
func CountSort(A []int, k int) { | |
c := make([]int, k+1) | |
for _, a := range A { | |
c[a]++ | |
} | |
for i := 1; i < k+1; i++ { | |
c[i] += c[i-1] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CountSortForRadixSort O(n) | |
func CountSortForRadixSort(A []int, d int, k int) { | |
dNum := 1 | |
for i := 0; i < d; i++ { | |
dNum *= k | |
} | |
c := make([]int, 11) | |
for _, a := range A { | |
c[a/dNum%k]++ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Min(A []int) int { | |
if len(A) < 1 { | |
return 0 | |
} | |
min := A[0] | |
for _, a := range A { | |
if a < min { | |
min = a | |
} | |
} |
OlderNewer