Algorithms_in_C
1.0.0
Set of algorithms implemented in C.
|
|
#define | BEAD(i, j) beads[i * max + j] |
| Create easy access of elements from a 2D matrix stored in memory as a 1D array.
|
|
|
void | display (const int *arr, int n) |
| Displays the array, passed to this method. More...
|
|
void | bead_sort (int *a, size_t len) |
| This is where the sorting of the array takes place. More...
|
|
void | swap (int *a, int *b) |
| Swap two integer variables. More...
|
|
void | merge (int *a, int l, int r, int n) |
| Perform merge of segments. More...
|
|
void | merge_sort (int *a, int n, int l, int r) |
| Merge sort algorithm implementation. More...
|
|
void | show_data (int *arr, long len) |
| Helper function to print array values.
|
|
void | shell_sort (int *array, long LEN) |
| Shell sort algorithm. More...
|
|
◆ bead_sort()
void bead_sort |
( |
int * |
a, |
|
|
size_t |
len |
|
) |
| |
This is where the sorting of the array takes place.
- Parameters
-
[in,out] | a | array to be sorted |
[in] | len | Array Size |
42 for (i = 1,
max = a[0]; i < len; i++)
46 beads = calloc(1,
max * len);
49 for (i = 0; i < len; i++)
50 for (j = 0; j < a[i]; j++)
BEAD(i, j) = 1;
52 for (j = 0; j <
max; j++)
55 for (sum = i = 0; i < len; i++)
61 for (i = len - sum; i < len; i++)
BEAD(i, j) = 1;
64 for (i = 0; i < len; i++)
66 for (j = 0; j <
max &&
BEAD(i, j); j++)
◆ display()
void display |
( |
const int * |
arr, |
|
|
int |
n |
|
) |
| |
Displays the array, passed to this method.
- Parameters
-
[in] | arr | array to display |
[in] | n | number of elements in the array |
25 for (
int i = 0; i < n; i++)
27 printf(
"%d ", arr[i]);
◆ merge()
void merge |
( |
int * |
a, |
|
|
int |
l, |
|
|
int |
r, |
|
|
int |
n |
|
) |
| |
Perform merge of segments.
- Parameters
-
a | array to sort |
l | left index for merge |
r | right index for merge |
n | total number of elements in the array |
35 int *b = (
int *)malloc(n *
sizeof(
int));
39 p2 = ((l + r) / 2) + 1;
40 while ((p1 < ((l + r) / 2) + 1) && (p2 < r + 1))
56 while ((p1 < ((l + r) / 2) + 1))
71 for (c = l; c < r - l + 1; c++) a[c] = b[c];
◆ merge_sort()
void merge_sort |
( |
int * |
a, |
|
|
int |
n, |
|
|
int |
l, |
|
|
int |
r |
|
) |
| |
Merge sort algorithm implementation.
- Parameters
-
a | array to sort |
n | number of elements in the array |
l | index to sort from |
r | index to sort till |
◆ shell_sort()
void shell_sort |
( |
int * |
array, |
|
|
long |
LEN |
|
) |
| |
Shell sort algorithm.
Optimized algorithm - takes half the time as other
- Parameters
-
[in,out] | array | array to sort |
[in] | LEN | length of the array |
43 const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
44 const int gap_len = 8;
47 for (g = 0; g < gap_len; g++)
50 for (i = gap; i < LEN; i++)
54 for (j = i; j >= gap && (array[j - gap] - tmp) > 0; j -= gap)
55 array[j] = array[j - gap];
60 for (i = 0; i < LEN; i++) printf(
"%s\t",
data[i]);
◆ swap()
void swap |
( |
int * |
a, |
|
|
int * |
b |
|
) |
| |
Swap two integer variables.
Function to swap values of two integers.
- Parameters
-
[in,out] | a | pointer to first variable |
[in,out] | b | pointer to second variable |
[in,out] | a | reference to first variable |
[in,out] | b | reference to second variable |
void swap(int *a, int *b)
Swap two integer variables.
Definition: merge_sort.c:17
Definition: prime_factoriziation.c:25
void merge(int *a, int l, int r, int n)
Perform merge of segments.
Definition: merge_sort.c:33
#define max(a, b)
shorthand for maximum value
Definition: kohonen_som_topology.c:39
void merge_sort(int *a, int n, int l, int r)
Merge sort algorithm implementation.
Definition: merge_sort.c:82
#define BEAD(i, j)
Create easy access of elements from a 2D matrix stored in memory as a 1D array.
Definition: bead_sort.c:16