C Everything
This is a C repository containing a curated set of generic data structures and algorithm.
array.c
Go to the documentation of this file.
1 
5 #include "array.h"
6 
17 void bubble_sort(t_gen a, int n, t_dparams *op)
18 {
19  int idx1, idx2;
20 
21  for (idx1 = 0; idx1 < (n-1); idx1++) {
22  // Last idx elements are already in place
23  for (idx2 = 0; idx2 < (n-idx1-1); idx2++) {
24  if (op->cmpr_idx(a, idx2, idx2+1) == eGREAT) {
25  op->swap_idx(a, idx2, idx2+1);
26  }
27  }
28  }
29 }
30 
40 void selection_sort(t_gen a, int n, t_dparams *op)
41 {
42  int start_idx, min_idx, idx;
43 
44  // Scan segments A[0], A[1], ...
45  for (start_idx = 0; start_idx < n; start_idx++) {
46  // locate position of min elem in cur segment
47  min_idx = start_idx;
48  for (idx = min_idx+1; idx < n; idx++) {
49  if (op->cmpr_idx(a, idx, min_idx) == eLESS) {
50  min_idx = idx;
51  }
52  }
53  // Move minm to start of cur segment
54  op->swap_idx(a, start_idx, min_idx);
55  }
56 }
57 
67 void insertion_sort(t_gen a, int n, t_dparams *op)
68 {
69  int pos, nxt_pos;
70 
71  // grow longer and longer sorted segments
72  for (pos = 1; pos < n; pos++) {
73  // In each iter A[0] - A[pos-1] is already sorted
74  //Move the first elem after sorted segmen left
75  // till it is in corrext place
76  nxt_pos = pos;
77  while (nxt_pos > 0 &&
78  op->cmpr_idx(a, nxt_pos, nxt_pos - 1) == eLESS) {
79  op->swap_idx(a, nxt_pos, nxt_pos - 1);
80  nxt_pos -= 1;
81  }
82  }
83 }
84 
94 int quick_sort_partition(t_gen a, int lo, int hi, t_dparams *op)
95 {
96  int pivot = hi;
97  int green, yellow;
98 
99  yellow = lo - 1;
100  for (int green = lo; green <= (hi - 1); green++) {
101  if (op->cmpr_idx(a, green, pivot) == eLESS) {
102  op->swap_idx(a, ++yellow, green);
103  }
104  }
105  op->swap_idx(a, ++yellow, pivot);
106 
107  return yellow;
108 }
109 
122 void quick_sort(t_gen a, int n, t_dparams *op)
123 {
124  int top, *stack, lo, hi, pivot;
125 
126  // Create an auxiliary stack
127  stack = (int*)get_mem(n, sizeof(int));
128 
129  // initialize top of stack
130  top = -1;
131 
132  // push initial values of l and h to stack
133  lo = 0;
134  hi = n - 1;
135  stack[++top] = lo;
136  stack[++top] = hi;
137 
138  // Keep popping from stack while is not empty
139  while (top >= 0) {
140  // Pop h and l
141  hi = stack[top--];
142  lo = stack[top--];
143 
144  // Set pivot element at its correct position
145  // in sorted array
146  pivot = quick_sort_partition(a, lo, hi, op);
147 
148  // If there are elements on left side of pivot,
149  // then push left side to stack
150  if ((pivot - 1) > lo) {
151  stack[++top] = lo;
152  stack[++top] = pivot - 1;
153  }
154 
155  // If there are elements on right side of pivot,
156  // then push right side to stack
157  if ((pivot + 1) < hi) {
158  stack[++top] = pivot + 1;
159  stack[++top] = hi;
160  }
161  }
162 
163  // free auxillary stack created
164  free_mem(stack);
165 }
166 
167 
void insertion_sort(t_gen a, int n, t_dparams *op)
Insertion sort builds the final sorted array one item at a time. It has an O(n2) time complexity
Definition: array.c:67
void quick_sort(t_gen a, int n, t_dparams *op)
Quicksort is an in-place sorting algorithm is a divide and conquer algorithm which relies on a partit...
Definition: array.c:122
int quick_sort_partition(t_gen a, int lo, int hi, t_dparams *op)
Util function to be used by quick sort for sorting partitions
Definition: array.c:94
void selection_sort(t_gen a, int n, t_dparams *op)
Selection sort is an in-place comparison sorting algorithm. It has an O(n2) time complexity
Definition: array.c:40
void bubble_sort(t_gen a, int n, t_dparams *op)
Bubble sort is simplest sorting algorithm that works by repeatedly swapping the adjacent elements if ...
Definition: array.c:17
Contains declations of array operations and structure.
#define free_mem(mem_addr)
Definition: memory_manager.h:9
#define get_mem(nmemb, size)
Definition: memory_manager.h:8
data params struct defn
Definition: common.h:18
f_swp_idx swap_idx
Routine used for swapring elems in given array indicies.
Definition: common.h:29
f_cmp_idx cmpr_idx
Routine used for comparing elems in given array indicies.
Definition: common.h:28
stack struct defn
Definition: stack.h:19
void * t_gen
Base Data type used for all data structure and data elements.
Definition: typedefs.h:41
@ eGREAT
Definition: typedefs.h:37
@ eLESS
Definition: typedefs.h:35