38 std::array<uint64_t, N> data{};
40 bool isSorted =
false;
50 const uint64_t &first,
const uint64_t &last,
51 const uint64_t &val) {
57 uint64_t mid = (first + last) / 2;
59 if (dataArr[mid] == val)
63 else if (val < dataArr[mid])
67 else if (val > dataArr[mid])
70 std::cerr << __func__ <<
":" << __LINE__ <<
": Undefined condition\n";
81 const uint64_t &val)
const {
83 for (uint64_t i = 0; i < top; i++) {
84 if (dataArr[i] == val) {
97 uint64_t
search(
const uint64_t &val) {
109 std::cout <<
"\nElement found at position : " << pos;
111 std::cout <<
"\nElement not found";
123 for (uint64_t i = 0; i < top; i++) {
124 uint64_t min_idx = i;
125 for (uint64_t j = i + 1; j < top; j++) {
127 if (data[j] < data[min_idx]) {
132 std::swap(data[min_idx], data[i]);
146 std::cout <<
"\nOverflow";
157 for (uint64_t i = 0; i < top - 1; i++) {
159 if (data[i] <= val && val <= data[i + 1]) {
169 for (uint64_t i = top; i > pos; i--) {
170 data[i] = data[i - 1];
184 uint64_t pos =
search(val);
187 std::cout <<
"\n Element does not present in the list ";
191 << data[pos] <<
" deleted";
193 for (uint64_t i = pos; i < top; i++) {
206 for (uint64_t i = 0; i < top; i++) {
207 std::cout << data[i] <<
" ";
241 assert(L.search(11) == 0);
242 assert(L.search(12) == 2);
243 assert(L.search(50) == -1);
247 assert(L.isSorted ==
true);
251 assert(L.search(11) == 1);
252 assert(L.search(12) == 2);
253 assert(L.search(50) == -1);
static void test()
Test implementations.
Functions for Dynamic Array algorithm.
Structure of List with supporting methods.
void show()
Utility function to print array.
void sort()
Sort the list.
uint64_t BinarySearch(const std::array< uint64_t, N > &dataArr, const uint64_t &first, const uint64_t &last, const uint64_t &val)
Search an element in the list using binarySearch.
void remove(const uint64_t &val)
To remove the element from the list.
void insert(const uint64_t &val)
Insert the new element in the list.
uint64_t LinearSearch(const std::array< uint64_t, N > &dataArr, const uint64_t &val) const
Search an element using linear search.