Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
Sorting algorithms

Macros

#define BEAD(i, j)   beads[i * max + j]
 Create easy access of elements from a 2D matrix stored in memory as a 1D array.
 

Functions

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...
 

Detailed Description

Function Documentation

◆ bead_sort()

void bead_sort ( int *  a,
size_t  len 
)

This is where the sorting of the array takes place.

Parameters
[in,out]aarray to be sorted
[in]lenArray Size
38 {
39  int i, j, max, sum;
40  unsigned char *beads;
41 
42  for (i = 1, max = a[0]; i < len; i++)
43  if (a[i] > max)
44  max = a[i];
45 
46  beads = calloc(1, max * len);
47 
48  /* mark the beads */
49  for (i = 0; i < len; i++)
50  for (j = 0; j < a[i]; j++) BEAD(i, j) = 1;
51 
52  for (j = 0; j < max; j++)
53  {
54  /* count how many beads are on each post */
55  for (sum = i = 0; i < len; i++)
56  {
57  sum += BEAD(i, j);
58  BEAD(i, j) = 0;
59  }
60  /* mark bottom sum beads */
61  for (i = len - sum; i < len; i++) BEAD(i, j) = 1;
62  }
63 
64  for (i = 0; i < len; i++)
65  {
66  for (j = 0; j < max && BEAD(i, j); j++)
67  ;
68  a[i] = j;
69  }
70  free(beads);
71 }

◆ display()

void display ( const int *  arr,
int  n 
)

Displays the array, passed to this method.

Parameters
[in]arrarray to display
[in]nnumber of elements in the array
24 {
25  for (int i = 0; i < n; i++)
26  {
27  printf("%d ", arr[i]);
28  }
29 
30  printf("\n");
31 }

◆ merge()

void merge ( int *  a,
int  l,
int  r,
int  n 
)

Perform merge of segments.

Parameters
aarray to sort
lleft index for merge
rright index for merge
ntotal number of elements in the array
34 {
35  int *b = (int *)malloc(n * sizeof(int)); /* dynamic memory must be freed */
36  int c = l;
37  int p1, p2;
38  p1 = l;
39  p2 = ((l + r) / 2) + 1;
40  while ((p1 < ((l + r) / 2) + 1) && (p2 < r + 1))
41  {
42  if (a[p1] <= a[p2])
43  {
44  b[c++] = a[p1];
45  p1++;
46  }
47  else
48  {
49  b[c++] = a[p2];
50  p2++;
51  }
52  }
53 
54  if (p2 == r + 1)
55  {
56  while ((p1 < ((l + r) / 2) + 1))
57  {
58  b[c++] = a[p1];
59  p1++;
60  }
61  }
62  else
63  {
64  while ((p2 < r + 1))
65  {
66  b[c++] = a[p2];
67  p2++;
68  }
69  }
70 
71  for (c = l; c < r - l + 1; c++) a[c] = b[c];
72 
73  free(b);
74 }

◆ merge_sort()

void merge_sort ( int *  a,
int  n,
int  l,
int  r 
)

Merge sort algorithm implementation.

Parameters
aarray to sort
nnumber of elements in the array
lindex to sort from
rindex to sort till
83 {
84  if (r - l == 1)
85  {
86  if (a[l] > a[r])
87  swap(&a[l], &a[r]);
88  }
89  else if (l != r)
90  {
91  merge_sort(a, n, l, (l + r) / 2);
92  merge_sort(a, n, ((l + r) / 2) + 1, r);
93  merge(a, l, r, n);
94  }
95 
96  /* no change if l == r */
97 }
Here is the call graph for this function:

◆ shell_sort()

void shell_sort ( int *  array,
long  LEN 
)

Shell sort algorithm.


Optimized algorithm - takes half the time as other

Parameters
[in,out]arrayarray to sort
[in]LENlength of the array
42 {
43  const int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
44  const int gap_len = 8;
45  long i, j, g;
46 
47  for (g = 0; g < gap_len; g++)
48  { // for each gap
49  int gap = gaps[g];
50  for (i = gap; i < LEN; i++)
51  { // from gap position to the end
52  int tmp = array[i];
53 
54  for (j = i; j >= gap && (array[j - gap] - tmp) > 0; j -= gap)
55  array[j] = array[j - gap];
56  array[j] = tmp;
57  }
58  }
59 #ifdef DEBUG
60  for (i = 0; i < LEN; i++) printf("%s\t", data[i]);
61 #endif
62 }

◆ swap()

void swap ( int *  a,
int *  b 
)

Swap two integer variables.

Function to swap values of two integers.

Parameters
[in,out]apointer to first variable
[in,out]bpointer to second variable
[in,out]areference to first variable
[in,out]breference to second variable
18 {
19  int t;
20  t = *a;
21  *a = *b;
22  *b = t;
23 }
swap
void swap(int *a, int *b)
Swap two integer variables.
Definition: merge_sort.c:17
data
Definition: prime_factoriziation.c:25
merge
void merge(int *a, int l, int r, int n)
Perform merge of segments.
Definition: merge_sort.c:33
max
#define max(a, b)
shorthand for maximum value
Definition: kohonen_som_topology.c:39
merge_sort
void merge_sort(int *a, int n, int l, int r)
Merge sort algorithm implementation.
Definition: merge_sort.c:82
BEAD
#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