Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
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 RecursionInsertionSort (int *arr, int size)
 Insertion sort algorithm implements using Recursion. 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. More...
 
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}
#define max(a, b)
shorthand for maximum value
Definition: kohonen_som_topology.c:39
#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
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26
#define calloc(elemCount, elemSize)
This macro replace the standard calloc function with calloc_dbg.
Definition: malloc_dbg.h:22

◆ 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 + 1; c++) a[c] = b[c];
72
73 free(b);
74}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition: malloc_dbg.h:18

◆ 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}
void merge_sort(int *a, int n, int l, int r)
Merge sort algorithm implementation.
Definition: merge_sort.c:82
Here is the call graph for this function:

◆ RecursionInsertionSort()

void RecursionInsertionSort ( int *  arr,
int  size 
)

Insertion sort algorithm implements using Recursion.

Parameters
arrarray to be sorted
sizesize of array
21{
22 if(size <= 0)
23 {
24 return;
25 }
26
27 // marking recursive call to reach starting element
28 RecursionInsertionSort(arr,size-1);
29
30 int key = arr[size-1];
31 int j = size-2;
32 // swapping logic for insertion sort
33 while(j >= 0 && arr[j] > key)
34 {
35 arr[j+1] = arr[j];
36 j--;
37 }
38 arr[j+1] = key;
39}
void RecursionInsertionSort(int *arr, int size)
Insertion sort algorithm implements using Recursion.
Definition: insertion_sort_recursive.c:20
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}
#define LEN
Linear input code | Compressed code | Linear output code ---------------—+--------------—+-----------...
Definition: alaw.c:28
Definition: prime_factoriziation.c:25

◆ show_data()

void show_data ( int *  arr,
long  len 
)

Helper function to print array values.

17{
18 for (long i = 0; i < len; i++) printf("%3d ", arr[i]);
19 printf("\n");
20}

◆ 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}