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.
 
void bead_sort (int *a, size_t len)
 This is where the sorting of the array takes place.
 
void RecursionInsertionSort (int *arr, int size)
 Insertion sort algorithm implements using Recursion.
 
void swap (int *a, int *b)
 Swap two integer variables.
 
void merge (int *a, int l, int r, int n)
 Perform merge of segments.
 
void merge_sort (int *a, int n, int l, int r)
 Merge sort algorithm implementation.
 
void show_data (int *arr, long len)
 Helper function to print array values.
 
void shell_sort (int *array, long LEN)
 Shell sort algorithm.
 

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 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 if (b == NULL)
37 {
38 printf("Can't Malloc! Please try again.");
39 exit(EXIT_FAILURE);
40 }
41 int c = l;
42 int p1, p2;
43 p1 = l;
44 p2 = ((l + r) / 2) + 1;
45 while ((p1 < ((l + r) / 2) + 1) && (p2 < r + 1))
46 {
47 if (a[p1] <= a[p2])
48 {
49 b[c++] = a[p1];
50 p1++;
51 }
52 else
53 {
54 b[c++] = a[p2];
55 p2++;
56 }
57 }
58
59 if (p2 == r + 1)
60 {
61 while ((p1 < ((l + r) / 2) + 1))
62 {
63 b[c++] = a[p1];
64 p1++;
65 }
66 }
67 else
68 {
69 while ((p2 < r + 1))
70 {
71 b[c++] = a[p2];
72 p2++;
73 }
74 }
75
76 for (c = l; c < r + 1; c++) a[c] = b[c];
77
78 free(b);
79}
#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
88{
89 if (r - l == 1)
90 {
91 if (a[l] > a[r])
92 swap(&a[l], &a[r]);
93 }
94 else if (l != r)
95 {
96 merge_sort(a, n, l, (l + r) / 2);
97 merge_sort(a, n, ((l + r) / 2) + 1, r);
98 merge(a, l, r, n);
99 }
100
101 /* no change if l == r */
102}
void merge_sort(int *a, int n, int l, int r)
Merge sort algorithm implementation.
Definition merge_sort.c:87
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 
)
inline

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}