restructed gitlab repository
This commit is contained in:
parent
dc9efc22b8
commit
2d0298342b
|
@ -0,0 +1,13 @@
|
||||||
|
CMakeLists.txt.user
|
||||||
|
CMakeCache.txt
|
||||||
|
CMakeFiles
|
||||||
|
CMakeScripts
|
||||||
|
Testing
|
||||||
|
Makefile
|
||||||
|
cmake_install.cmake
|
||||||
|
install_manifest.txt
|
||||||
|
compile_commands.json
|
||||||
|
CTestTestfile.cmake
|
||||||
|
_deps
|
||||||
|
Main
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
project(Search)
|
||||||
|
add_executable(
|
||||||
|
Main
|
||||||
|
|
||||||
|
src/main.c
|
||||||
|
|
||||||
|
src/bst/bst.h
|
||||||
|
src/bst/bst.c
|
||||||
|
|
||||||
|
src/hashtable/hashtable.c
|
||||||
|
src/hashtable/hashtable.h
|
||||||
|
|
||||||
|
src/binheap/binheap.c
|
||||||
|
src/binheap/binheap.h
|
||||||
|
|
||||||
|
src/utils/arrays.c
|
||||||
|
src/utils/arrays.h
|
||||||
|
)
|
|
@ -0,0 +1,339 @@
|
||||||
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
Version 2, June 1991
|
||||||
|
|
||||||
|
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||||
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
Preamble
|
||||||
|
|
||||||
|
The licenses for most software are designed to take away your
|
||||||
|
freedom to share and change it. By contrast, the GNU General Public
|
||||||
|
License is intended to guarantee your freedom to share and change free
|
||||||
|
software--to make sure the software is free for all its users. This
|
||||||
|
General Public License applies to most of the Free Software
|
||||||
|
Foundation's software and to any other program whose authors commit to
|
||||||
|
using it. (Some other Free Software Foundation software is covered by
|
||||||
|
the GNU Lesser General Public License instead.) You can apply it to
|
||||||
|
your programs, too.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
|
have the freedom to distribute copies of free software (and charge for
|
||||||
|
this service if you wish), that you receive source code or can get it
|
||||||
|
if you want it, that you can change the software or use pieces of it
|
||||||
|
in new free programs; and that you know you can do these things.
|
||||||
|
|
||||||
|
To protect your rights, we need to make restrictions that forbid
|
||||||
|
anyone to deny you these rights or to ask you to surrender the rights.
|
||||||
|
These restrictions translate to certain responsibilities for you if you
|
||||||
|
distribute copies of the software, or if you modify it.
|
||||||
|
|
||||||
|
For example, if you distribute copies of such a program, whether
|
||||||
|
gratis or for a fee, you must give the recipients all the rights that
|
||||||
|
you have. You must make sure that they, too, receive or can get the
|
||||||
|
source code. And you must show them these terms so they know their
|
||||||
|
rights.
|
||||||
|
|
||||||
|
We protect your rights with two steps: (1) copyright the software, and
|
||||||
|
(2) offer you this license which gives you legal permission to copy,
|
||||||
|
distribute and/or modify the software.
|
||||||
|
|
||||||
|
Also, for each author's protection and ours, we want to make certain
|
||||||
|
that everyone understands that there is no warranty for this free
|
||||||
|
software. If the software is modified by someone else and passed on, we
|
||||||
|
want its recipients to know that what they have is not the original, so
|
||||||
|
that any problems introduced by others will not reflect on the original
|
||||||
|
authors' reputations.
|
||||||
|
|
||||||
|
Finally, any free program is threatened constantly by software
|
||||||
|
patents. We wish to avoid the danger that redistributors of a free
|
||||||
|
program will individually obtain patent licenses, in effect making the
|
||||||
|
program proprietary. To prevent this, we have made it clear that any
|
||||||
|
patent must be licensed for everyone's free use or not licensed at all.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||||
|
|
||||||
|
0. This License applies to any program or other work which contains
|
||||||
|
a notice placed by the copyright holder saying it may be distributed
|
||||||
|
under the terms of this General Public License. The "Program", below,
|
||||||
|
refers to any such program or work, and a "work based on the Program"
|
||||||
|
means either the Program or any derivative work under copyright law:
|
||||||
|
that is to say, a work containing the Program or a portion of it,
|
||||||
|
either verbatim or with modifications and/or translated into another
|
||||||
|
language. (Hereinafter, translation is included without limitation in
|
||||||
|
the term "modification".) Each licensee is addressed as "you".
|
||||||
|
|
||||||
|
Activities other than copying, distribution and modification are not
|
||||||
|
covered by this License; they are outside its scope. The act of
|
||||||
|
running the Program is not restricted, and the output from the Program
|
||||||
|
is covered only if its contents constitute a work based on the
|
||||||
|
Program (independent of having been made by running the Program).
|
||||||
|
Whether that is true depends on what the Program does.
|
||||||
|
|
||||||
|
1. You may copy and distribute verbatim copies of the Program's
|
||||||
|
source code as you receive it, in any medium, provided that you
|
||||||
|
conspicuously and appropriately publish on each copy an appropriate
|
||||||
|
copyright notice and disclaimer of warranty; keep intact all the
|
||||||
|
notices that refer to this License and to the absence of any warranty;
|
||||||
|
and give any other recipients of the Program a copy of this License
|
||||||
|
along with the Program.
|
||||||
|
|
||||||
|
You may charge a fee for the physical act of transferring a copy, and
|
||||||
|
you may at your option offer warranty protection in exchange for a fee.
|
||||||
|
|
||||||
|
2. You may modify your copy or copies of the Program or any portion
|
||||||
|
of it, thus forming a work based on the Program, and copy and
|
||||||
|
distribute such modifications or work under the terms of Section 1
|
||||||
|
above, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) You must cause the modified files to carry prominent notices
|
||||||
|
stating that you changed the files and the date of any change.
|
||||||
|
|
||||||
|
b) You must cause any work that you distribute or publish, that in
|
||||||
|
whole or in part contains or is derived from the Program or any
|
||||||
|
part thereof, to be licensed as a whole at no charge to all third
|
||||||
|
parties under the terms of this License.
|
||||||
|
|
||||||
|
c) If the modified program normally reads commands interactively
|
||||||
|
when run, you must cause it, when started running for such
|
||||||
|
interactive use in the most ordinary way, to print or display an
|
||||||
|
announcement including an appropriate copyright notice and a
|
||||||
|
notice that there is no warranty (or else, saying that you provide
|
||||||
|
a warranty) and that users may redistribute the program under
|
||||||
|
these conditions, and telling the user how to view a copy of this
|
||||||
|
License. (Exception: if the Program itself is interactive but
|
||||||
|
does not normally print such an announcement, your work based on
|
||||||
|
the Program is not required to print an announcement.)
|
||||||
|
|
||||||
|
These requirements apply to the modified work as a whole. If
|
||||||
|
identifiable sections of that work are not derived from the Program,
|
||||||
|
and can be reasonably considered independent and separate works in
|
||||||
|
themselves, then this License, and its terms, do not apply to those
|
||||||
|
sections when you distribute them as separate works. But when you
|
||||||
|
distribute the same sections as part of a whole which is a work based
|
||||||
|
on the Program, the distribution of the whole must be on the terms of
|
||||||
|
this License, whose permissions for other licensees extend to the
|
||||||
|
entire whole, and thus to each and every part regardless of who wrote it.
|
||||||
|
|
||||||
|
Thus, it is not the intent of this section to claim rights or contest
|
||||||
|
your rights to work written entirely by you; rather, the intent is to
|
||||||
|
exercise the right to control the distribution of derivative or
|
||||||
|
collective works based on the Program.
|
||||||
|
|
||||||
|
In addition, mere aggregation of another work not based on the Program
|
||||||
|
with the Program (or with a work based on the Program) on a volume of
|
||||||
|
a storage or distribution medium does not bring the other work under
|
||||||
|
the scope of this License.
|
||||||
|
|
||||||
|
3. You may copy and distribute the Program (or a work based on it,
|
||||||
|
under Section 2) in object code or executable form under the terms of
|
||||||
|
Sections 1 and 2 above provided that you also do one of the following:
|
||||||
|
|
||||||
|
a) Accompany it with the complete corresponding machine-readable
|
||||||
|
source code, which must be distributed under the terms of Sections
|
||||||
|
1 and 2 above on a medium customarily used for software interchange; or,
|
||||||
|
|
||||||
|
b) Accompany it with a written offer, valid for at least three
|
||||||
|
years, to give any third party, for a charge no more than your
|
||||||
|
cost of physically performing source distribution, a complete
|
||||||
|
machine-readable copy of the corresponding source code, to be
|
||||||
|
distributed under the terms of Sections 1 and 2 above on a medium
|
||||||
|
customarily used for software interchange; or,
|
||||||
|
|
||||||
|
c) Accompany it with the information you received as to the offer
|
||||||
|
to distribute corresponding source code. (This alternative is
|
||||||
|
allowed only for noncommercial distribution and only if you
|
||||||
|
received the program in object code or executable form with such
|
||||||
|
an offer, in accord with Subsection b above.)
|
||||||
|
|
||||||
|
The source code for a work means the preferred form of the work for
|
||||||
|
making modifications to it. For an executable work, complete source
|
||||||
|
code means all the source code for all modules it contains, plus any
|
||||||
|
associated interface definition files, plus the scripts used to
|
||||||
|
control compilation and installation of the executable. However, as a
|
||||||
|
special exception, the source code distributed need not include
|
||||||
|
anything that is normally distributed (in either source or binary
|
||||||
|
form) with the major components (compiler, kernel, and so on) of the
|
||||||
|
operating system on which the executable runs, unless that component
|
||||||
|
itself accompanies the executable.
|
||||||
|
|
||||||
|
If distribution of executable or object code is made by offering
|
||||||
|
access to copy from a designated place, then offering equivalent
|
||||||
|
access to copy the source code from the same place counts as
|
||||||
|
distribution of the source code, even though third parties are not
|
||||||
|
compelled to copy the source along with the object code.
|
||||||
|
|
||||||
|
4. You may not copy, modify, sublicense, or distribute the Program
|
||||||
|
except as expressly provided under this License. Any attempt
|
||||||
|
otherwise to copy, modify, sublicense or distribute the Program is
|
||||||
|
void, and will automatically terminate your rights under this License.
|
||||||
|
However, parties who have received copies, or rights, from you under
|
||||||
|
this License will not have their licenses terminated so long as such
|
||||||
|
parties remain in full compliance.
|
||||||
|
|
||||||
|
5. You are not required to accept this License, since you have not
|
||||||
|
signed it. However, nothing else grants you permission to modify or
|
||||||
|
distribute the Program or its derivative works. These actions are
|
||||||
|
prohibited by law if you do not accept this License. Therefore, by
|
||||||
|
modifying or distributing the Program (or any work based on the
|
||||||
|
Program), you indicate your acceptance of this License to do so, and
|
||||||
|
all its terms and conditions for copying, distributing or modifying
|
||||||
|
the Program or works based on it.
|
||||||
|
|
||||||
|
6. Each time you redistribute the Program (or any work based on the
|
||||||
|
Program), the recipient automatically receives a license from the
|
||||||
|
original licensor to copy, distribute or modify the Program subject to
|
||||||
|
these terms and conditions. You may not impose any further
|
||||||
|
restrictions on the recipients' exercise of the rights granted herein.
|
||||||
|
You are not responsible for enforcing compliance by third parties to
|
||||||
|
this License.
|
||||||
|
|
||||||
|
7. If, as a consequence of a court judgment or allegation of patent
|
||||||
|
infringement or for any other reason (not limited to patent issues),
|
||||||
|
conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot
|
||||||
|
distribute so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you
|
||||||
|
may not distribute the Program at all. For example, if a patent
|
||||||
|
license would not permit royalty-free redistribution of the Program by
|
||||||
|
all those who receive copies directly or indirectly through you, then
|
||||||
|
the only way you could satisfy both it and this License would be to
|
||||||
|
refrain entirely from distribution of the Program.
|
||||||
|
|
||||||
|
If any portion of this section is held invalid or unenforceable under
|
||||||
|
any particular circumstance, the balance of the section is intended to
|
||||||
|
apply and the section as a whole is intended to apply in other
|
||||||
|
circumstances.
|
||||||
|
|
||||||
|
It is not the purpose of this section to induce you to infringe any
|
||||||
|
patents or other property right claims or to contest validity of any
|
||||||
|
such claims; this section has the sole purpose of protecting the
|
||||||
|
integrity of the free software distribution system, which is
|
||||||
|
implemented by public license practices. Many people have made
|
||||||
|
generous contributions to the wide range of software distributed
|
||||||
|
through that system in reliance on consistent application of that
|
||||||
|
system; it is up to the author/donor to decide if he or she is willing
|
||||||
|
to distribute software through any other system and a licensee cannot
|
||||||
|
impose that choice.
|
||||||
|
|
||||||
|
This section is intended to make thoroughly clear what is believed to
|
||||||
|
be a consequence of the rest of this License.
|
||||||
|
|
||||||
|
8. If the distribution and/or use of the Program is restricted in
|
||||||
|
certain countries either by patents or by copyrighted interfaces, the
|
||||||
|
original copyright holder who places the Program under this License
|
||||||
|
may add an explicit geographical distribution limitation excluding
|
||||||
|
those countries, so that distribution is permitted only in or among
|
||||||
|
countries not thus excluded. In such case, this License incorporates
|
||||||
|
the limitation as if written in the body of this License.
|
||||||
|
|
||||||
|
9. The Free Software Foundation may publish revised and/or new versions
|
||||||
|
of the General Public License from time to time. Such new versions will
|
||||||
|
be similar in spirit to the present version, but may differ in detail to
|
||||||
|
address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the Program
|
||||||
|
specifies a version number of this License which applies to it and "any
|
||||||
|
later version", you have the option of following the terms and conditions
|
||||||
|
either of that version or of any later version published by the Free
|
||||||
|
Software Foundation. If the Program does not specify a version number of
|
||||||
|
this License, you may choose any version ever published by the Free Software
|
||||||
|
Foundation.
|
||||||
|
|
||||||
|
10. If you wish to incorporate parts of the Program into other free
|
||||||
|
programs whose distribution conditions are different, write to the author
|
||||||
|
to ask for permission. For software which is copyrighted by the Free
|
||||||
|
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||||
|
make exceptions for this. Our decision will be guided by the two goals
|
||||||
|
of preserving the free status of all derivatives of our free software and
|
||||||
|
of promoting the sharing and reuse of software generally.
|
||||||
|
|
||||||
|
NO WARRANTY
|
||||||
|
|
||||||
|
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||||
|
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||||
|
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||||
|
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||||
|
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||||
|
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||||
|
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||||
|
REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||||
|
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||||
|
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||||
|
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||||
|
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||||
|
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||||
|
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||||
|
POSSIBILITY OF SUCH DAMAGES.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest
|
||||||
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
free software which everyone can redistribute and change under these terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest
|
||||||
|
to attach them to the start of each source file to most effectively
|
||||||
|
convey the exclusion of warranty; and each file should have at least
|
||||||
|
the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License along
|
||||||
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If the program is interactive, make it output a short notice like this
|
||||||
|
when it starts in an interactive mode:
|
||||||
|
|
||||||
|
Gnomovision version 69, Copyright (C) year name of author
|
||||||
|
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
|
This is free software, and you are welcome to redistribute it
|
||||||
|
under certain conditions; type `show c' for details.
|
||||||
|
|
||||||
|
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||||
|
parts of the General Public License. Of course, the commands you use may
|
||||||
|
be called something other than `show w' and `show c'; they could even be
|
||||||
|
mouse-clicks or menu items--whatever suits your program.
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or your
|
||||||
|
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||||
|
necessary. Here is a sample; alter the names:
|
||||||
|
|
||||||
|
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||||
|
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||||
|
|
||||||
|
<signature of Ty Coon>, 1 April 1989
|
||||||
|
Ty Coon, President of Vice
|
||||||
|
|
||||||
|
This General Public License does not permit incorporating your program into
|
||||||
|
proprietary programs. If your program is a subroutine library, you may
|
||||||
|
consider it more useful to permit linking proprietary applications with the
|
||||||
|
library. If this is what you want to do, use the GNU Lesser General
|
||||||
|
Public License instead of this License.
|
|
@ -0,0 +1,6 @@
|
||||||
|
# DHBW-Datastructures
|
||||||
|
Datastructures and related algorithms from school written in C99.
|
||||||
|
This repo includes:
|
||||||
|
* Binary max heap for signed integer (including heap-sort)
|
||||||
|
* Hashmap for (string, signed integer) tuples
|
||||||
|
* Binary search tree (BST)
|
|
@ -0,0 +1,119 @@
|
||||||
|
#include "binheap.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
Maxbinheap newMaxbinheap(unsigned int capacity) {
|
||||||
|
Maxbinheap heap = {
|
||||||
|
.capacity = capacity,
|
||||||
|
.size = 0,
|
||||||
|
.array = (int*) calloc(capacity, sizeof(int))
|
||||||
|
};
|
||||||
|
|
||||||
|
return heap;
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroyMaxbinheap(Maxbinheap *heap) {
|
||||||
|
// deallocate memory
|
||||||
|
free(heap->array);
|
||||||
|
// clear data
|
||||||
|
heap->capacity = 0;
|
||||||
|
heap->array = NULL;
|
||||||
|
heap->size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
Maxbinheap fromArray(const int* array, unsigned int len) {
|
||||||
|
Maxbinheap heap = newMaxbinheap(len);
|
||||||
|
|
||||||
|
for (const int* x = array; x < array + len; x++)
|
||||||
|
insert(&heap, *x);
|
||||||
|
|
||||||
|
return heap;
|
||||||
|
}
|
||||||
|
|
||||||
|
void swim(Maxbinheap *heap, int k) {
|
||||||
|
while (k > 0) {
|
||||||
|
|
||||||
|
if (heap->array[k / 2] > heap->array[k]) {
|
||||||
|
// parent element is larger, we stop
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// parent is smaller, move the parent down
|
||||||
|
swap(heap->array + k, heap->array + k / 2);
|
||||||
|
|
||||||
|
k = k/2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sink(Maxbinheap *heap, int k) {
|
||||||
|
while (2 * k <= heap->size) {
|
||||||
|
int j = 2 * k;
|
||||||
|
|
||||||
|
// either select left or right child
|
||||||
|
if (j < heap->size && heap->array[j] < heap->array[j + 1])
|
||||||
|
j ++;
|
||||||
|
|
||||||
|
// continue as long as its not high up enough
|
||||||
|
if (!(heap->array[k] < heap->array[j]))
|
||||||
|
break;
|
||||||
|
|
||||||
|
swap(heap->array + k, heap->array + j);
|
||||||
|
k = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void insert(Maxbinheap *heap, int element) {
|
||||||
|
if (heap->size + 1 >= heap->capacity)
|
||||||
|
// reallocate the array
|
||||||
|
heap->array = reallocarray(heap->array, heap->capacity + 4, sizeof(int));
|
||||||
|
|
||||||
|
// insert new element at the end
|
||||||
|
heap->array[heap->size] = element;
|
||||||
|
heap->size ++;
|
||||||
|
|
||||||
|
// let it swim up to its right position
|
||||||
|
swim(heap, heap->size - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int removeMax(Maxbinheap *heap) {
|
||||||
|
int max = heap->array[0];
|
||||||
|
|
||||||
|
heap->size --;
|
||||||
|
|
||||||
|
// take smallest element and put in as root
|
||||||
|
swap(heap->array, heap->array + heap->size);
|
||||||
|
|
||||||
|
// let new root sink in
|
||||||
|
sink(heap, 0);
|
||||||
|
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sinkArray(int* array, int k, const int len) {
|
||||||
|
while (2 * k <= len) {
|
||||||
|
int j = 2 * k;
|
||||||
|
|
||||||
|
// either select left or right child
|
||||||
|
if (j < len && array[j] < array[j + 1])
|
||||||
|
j ++;
|
||||||
|
|
||||||
|
// continue as long as its not high up enough
|
||||||
|
if (!(array[k] < array[j]))
|
||||||
|
break;
|
||||||
|
|
||||||
|
swap(array + k, array + j);
|
||||||
|
k = j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void heapsort(int* array, const int len) {
|
||||||
|
for (int x = len/2; x >= 0; x--)
|
||||||
|
sinkArray(array, x, len);
|
||||||
|
|
||||||
|
int n = len;
|
||||||
|
|
||||||
|
while (n > 0) {
|
||||||
|
swap(array, array + n);
|
||||||
|
n --;
|
||||||
|
sinkArray(array, 1, n);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
|
||||||
|
#ifndef _BINHEAP_H_
|
||||||
|
#define _BINHEAP_H_
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "../utils/arrays.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum binary heap for signed integers
|
||||||
|
*/
|
||||||
|
typedef struct maxbinheap {
|
||||||
|
// amount of elements that fit into the heap
|
||||||
|
unsigned int capacity;
|
||||||
|
// number of inserted elements
|
||||||
|
unsigned int size;
|
||||||
|
// data of heap
|
||||||
|
int* array;
|
||||||
|
} Maxbinheap;
|
||||||
|
|
||||||
|
// create new heap
|
||||||
|
Maxbinheap newMaxbinheap(unsigned int capacity);
|
||||||
|
// free memory
|
||||||
|
void destroyMaxbinheap(Maxbinheap *heap);
|
||||||
|
// create a new binary heap from the array
|
||||||
|
Maxbinheap fromArray(const int* array, unsigned int len);
|
||||||
|
|
||||||
|
// add new element
|
||||||
|
void insert(Maxbinheap *heap, int element);
|
||||||
|
|
||||||
|
// remove maximum element
|
||||||
|
int removeMax(Maxbinheap *heap);
|
||||||
|
|
||||||
|
// sort array using an inplace binary heap
|
||||||
|
void heapsort(int* array, const int len);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,147 @@
|
||||||
|
#include "bst.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
Bst bst_new() {
|
||||||
|
Bst bst = {
|
||||||
|
.root = NULL
|
||||||
|
};
|
||||||
|
|
||||||
|
return bst;
|
||||||
|
}
|
||||||
|
|
||||||
|
void dealloc_node(Node *node) {
|
||||||
|
if (node == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
dealloc_node(node->left);
|
||||||
|
dealloc_node(node->right);
|
||||||
|
|
||||||
|
free(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bst_destroy(Bst *bst) {
|
||||||
|
dealloc_node(bst->root);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node* new_node(char value) {
|
||||||
|
Node *n = (Node*) calloc(sizeof(Node), 1);
|
||||||
|
n->value = value;
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
void place_node(Node *node, char value) {
|
||||||
|
if (value > node->value) {
|
||||||
|
|
||||||
|
if (node->right == NULL) {
|
||||||
|
node->right = new_node(value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
place_node(node->right, value);
|
||||||
|
} else {
|
||||||
|
|
||||||
|
if (node->left == NULL) {
|
||||||
|
node->left = new_node(value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
place_node(node->left, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bst_insert(Bst *bst, char value) {
|
||||||
|
if (bst->root == NULL) {
|
||||||
|
bst->root = new_node(value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
place_node(bst->root, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool search_node(Node* node, char value) {
|
||||||
|
if (value == node->value) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value > node->value) {
|
||||||
|
if (node->right == NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return search_node(node->right, value);
|
||||||
|
} else {
|
||||||
|
if (node->left == NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return search_node(node->left, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool bst_search(Bst *bst, char value) {
|
||||||
|
return search_node(bst->root, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Node** minimum(Node *node) {
|
||||||
|
if (node->left->left == NULL) {
|
||||||
|
return &node->left;
|
||||||
|
}
|
||||||
|
|
||||||
|
return minimum(node->left);
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove_node(Node **node, char value) {
|
||||||
|
|
||||||
|
if (value > (*node)->value)
|
||||||
|
remove_node(&(*node)->right, value);
|
||||||
|
else if (value < (*node)->value)
|
||||||
|
remove_node(&(*node)->left, value);
|
||||||
|
else {
|
||||||
|
if ((*node)->right == (*node)->left) {
|
||||||
|
*node = NULL;
|
||||||
|
} else if ((*node)->left == NULL) {
|
||||||
|
*node = (*node)->right;
|
||||||
|
} else if ((*node)->right == NULL) {
|
||||||
|
*node = (*node)->left;
|
||||||
|
} else {
|
||||||
|
Node **min = minimum(*node);
|
||||||
|
(*node)->value = (*min)->value;
|
||||||
|
*min = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bst_remove(Bst *bst, char value) {
|
||||||
|
if (bst->root == NULL)
|
||||||
|
return;
|
||||||
|
|
||||||
|
remove_node(&bst->root, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
char node_max(Node *node) {
|
||||||
|
if (node->right == NULL)
|
||||||
|
return node->value;
|
||||||
|
|
||||||
|
return node_max(node->right);
|
||||||
|
}
|
||||||
|
|
||||||
|
char bst_max(Bst *bst) {
|
||||||
|
if (bst->root == NULL)
|
||||||
|
return '\0';
|
||||||
|
|
||||||
|
return node_max(bst->root);
|
||||||
|
}
|
||||||
|
|
||||||
|
char node_min(Node *node) {
|
||||||
|
if (node->left == NULL)
|
||||||
|
return node->value;
|
||||||
|
|
||||||
|
return node_min(node->left);
|
||||||
|
}
|
||||||
|
|
||||||
|
char bst_min(Bst *bst) {
|
||||||
|
if (bst->root == NULL)
|
||||||
|
return '\0';
|
||||||
|
|
||||||
|
return node_min(bst->root);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
|
||||||
|
#ifndef _BST_H_
|
||||||
|
#define _BST_H_
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
typedef struct node {
|
||||||
|
char value;
|
||||||
|
struct node *left;
|
||||||
|
struct node *right;
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
typedef struct bst {
|
||||||
|
Node *root;
|
||||||
|
} Bst;
|
||||||
|
|
||||||
|
Bst bst_new();
|
||||||
|
void bst_destroy(Bst *bst);
|
||||||
|
|
||||||
|
void bst_insert(Bst *bst, char value);
|
||||||
|
void bst_remove(Bst *bst, char value);
|
||||||
|
bool bst_search(Bst *bst, char value);
|
||||||
|
|
||||||
|
char bst_max(Bst *bst);
|
||||||
|
char bst_min(Bst *bst);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,108 @@
|
||||||
|
|
||||||
|
#include "hashtable.h"
|
||||||
|
|
||||||
|
Hashmap new_hashmap(unsigned int capacity) {
|
||||||
|
Hashmap map = {
|
||||||
|
.capacity = capacity,
|
||||||
|
.keys = (const char**) calloc(sizeof(const char**), capacity),
|
||||||
|
.values = (int*) calloc(sizeof(int), capacity)
|
||||||
|
};
|
||||||
|
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
|
void destroy_hashmap(Hashmap *hashmap) {
|
||||||
|
if (hashmap->keys != NULL) {
|
||||||
|
free(hashmap->keys);
|
||||||
|
free(hashmap->values);
|
||||||
|
|
||||||
|
hashmap->keys = NULL;
|
||||||
|
hashmap->values = NULL;
|
||||||
|
hashmap->capacity = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_hashmap(Hashmap *hashmap) {
|
||||||
|
for (int x = 0; x < hashmap->capacity; x++) {
|
||||||
|
printf("%08x (%d)\t| %s\n", hashmap->values[x], hashmap->values[x], hashmap->keys[x]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int hash(const char *string) {
|
||||||
|
const int len = strlen(string);
|
||||||
|
|
||||||
|
unsigned int hash = 0;
|
||||||
|
|
||||||
|
for (int x = 0; x < len; x++)
|
||||||
|
hash += (hash * 31 + string[x]) / (1 << x) - 1;
|
||||||
|
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int hash2(const char *string) {
|
||||||
|
const int len = strlen(string);
|
||||||
|
|
||||||
|
unsigned int hash = 0;
|
||||||
|
|
||||||
|
for (int x = 0; x < len; x++)
|
||||||
|
hash += (hash * 31 + string[x] + 7) / (1 << x) - 1;
|
||||||
|
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define QUADRADIC_PROBING
|
||||||
|
|
||||||
|
unsigned int address_of(Hashmap *hashtable, const char *string) {
|
||||||
|
unsigned int bucket = hash(string);
|
||||||
|
|
||||||
|
int iteration = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
|
||||||
|
#ifdef LINEAR_PROBING
|
||||||
|
|
||||||
|
bucket = (bucket + 1) % hashtable->capacity;
|
||||||
|
|
||||||
|
#elifdef QUADRADIC_PROBING
|
||||||
|
|
||||||
|
bucket = (bucket + iteration * iteration) % hashtable->capacity;
|
||||||
|
iteration ++;
|
||||||
|
|
||||||
|
#elifdef DOULE_HASHING
|
||||||
|
|
||||||
|
bucket = iteration == 0 ? bucket : (hash(string) + iteration * hash2(string)) % hashtable->capacity;
|
||||||
|
|
||||||
|
#else
|
||||||
|
#error *** No collision avoidance method specified ***
|
||||||
|
#endif
|
||||||
|
|
||||||
|
} while (hashtable->values[bucket] != 0 && strcmp(hashtable->keys[bucket], string) != 0);
|
||||||
|
|
||||||
|
return bucket;
|
||||||
|
}
|
||||||
|
|
||||||
|
void put(Hashmap *hashtable, const char* string, const int value) {
|
||||||
|
unsigned int bucket = address_of(hashtable, string);
|
||||||
|
|
||||||
|
hashtable->values[bucket] = value;
|
||||||
|
hashtable->keys[bucket] = string;
|
||||||
|
}
|
||||||
|
|
||||||
|
void hashmap_remove(Hashmap *hashtable, const char* string) {
|
||||||
|
unsigned int bucket = address_of(hashtable, string);
|
||||||
|
|
||||||
|
if (hashtable->keys[bucket] != NULL) {
|
||||||
|
hashtable->keys[bucket] = NULL;
|
||||||
|
hashtable->values[bucket] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int get(Hashmap *hashtable, const char* string) {
|
||||||
|
unsigned int bucket = address_of(hashtable, string);
|
||||||
|
|
||||||
|
if (hashtable->keys[bucket] != NULL) {
|
||||||
|
return hashtable->values[bucket];
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
#ifndef _HASHTABLE_H_
|
||||||
|
#define _HASHTABLE_H_
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hashtable for strings
|
||||||
|
* */
|
||||||
|
typedef struct hashmap {
|
||||||
|
// total amount of buckets
|
||||||
|
unsigned int capacity;
|
||||||
|
// key data
|
||||||
|
const char **keys;
|
||||||
|
// value data
|
||||||
|
int *values;
|
||||||
|
} Hashmap;
|
||||||
|
|
||||||
|
void put(Hashmap *hashmap, const char* string, const int value);
|
||||||
|
|
||||||
|
void hashmap_remove(Hashmap *hashmap, const char* string);
|
||||||
|
|
||||||
|
int get(Hashmap *hashmap, const char* string);
|
||||||
|
|
||||||
|
Hashmap new_hashmap(unsigned int capacity);
|
||||||
|
|
||||||
|
void destroy_hashmap(Hashmap *hashmap);
|
||||||
|
|
||||||
|
void print_hashmap(Hashmap *hashmap);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,60 @@
|
||||||
|
#include "binheap/binheap.h"
|
||||||
|
#include "hashtable/hashtable.h"
|
||||||
|
#include "bst/bst.h"
|
||||||
|
#include "utils/arrays.h"
|
||||||
|
|
||||||
|
void testMaxbinheap() {
|
||||||
|
// random array of values
|
||||||
|
const int array[] = { 1, 2, 3, 4, 5, 6, 7 };
|
||||||
|
|
||||||
|
// sorted binary max heap
|
||||||
|
Maxbinheap heap = fromArray(array, sizeof(array) / sizeof(int) + 1);
|
||||||
|
|
||||||
|
insert(&heap, 8);
|
||||||
|
|
||||||
|
print_array(heap.array, heap.size);
|
||||||
|
|
||||||
|
// free memory
|
||||||
|
destroyMaxbinheap(&heap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void testHashmap() {
|
||||||
|
Hashmap table = new_hashmap(10);
|
||||||
|
|
||||||
|
put(&table, "Henry", 14);
|
||||||
|
put(&table, "Henry", 17);
|
||||||
|
put(&table, "Jenifer", 35);
|
||||||
|
put(&table, "Harry", 42);
|
||||||
|
|
||||||
|
hashmap_remove(&table, "Jenifer");
|
||||||
|
|
||||||
|
print_hashmap(&table);
|
||||||
|
|
||||||
|
destroy_hashmap(&table);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_bst() {
|
||||||
|
Bst bst = bst_new();
|
||||||
|
|
||||||
|
bst_insert(&bst, 'v');
|
||||||
|
bst_insert(&bst, 'b');
|
||||||
|
bst_insert(&bst, 'a');
|
||||||
|
bst_insert(&bst, 'd');
|
||||||
|
|
||||||
|
bst_remove(&bst, 'v');
|
||||||
|
|
||||||
|
printf("found: %d\n", bst_search(&bst, 'a') );
|
||||||
|
printf("max: %c\n", bst_max(&bst));
|
||||||
|
|
||||||
|
bst_destroy(&bst);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// testMaxbinheap();
|
||||||
|
|
||||||
|
// testHashmap();
|
||||||
|
|
||||||
|
test_bst();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,114 @@
|
||||||
|
#include "arrays.h"
|
||||||
|
|
||||||
|
#ifdef VISUALIZE
|
||||||
|
|
||||||
|
static int *ARR;
|
||||||
|
static int LEN;
|
||||||
|
static char *NAME;
|
||||||
|
static int COUNT;
|
||||||
|
|
||||||
|
/* msleep(): Sleep for the requested number of milliseconds. */
|
||||||
|
int msleep(long msec) {
|
||||||
|
struct timespec ts;
|
||||||
|
int res;
|
||||||
|
|
||||||
|
if (msec < 0) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ts.tv_sec = msec / 1000;
|
||||||
|
ts.tv_nsec = (msec % 1000) * 1000000;
|
||||||
|
|
||||||
|
do {
|
||||||
|
res = nanosleep(&ts, &ts);
|
||||||
|
} while (res && errno == EINTR);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw(int *a, int *b) {
|
||||||
|
const char *SOLID_BLOCK = "\u2588";
|
||||||
|
const char *YELLOW = "\e[0;33m";
|
||||||
|
const char *CYAN = "\e[0;36m";
|
||||||
|
const char *RESET = "\e[0m";
|
||||||
|
|
||||||
|
printf("\e[1;1H\e[2J");
|
||||||
|
printf("Algorithm: %s Swaps: %d\n", NAME, COUNT);
|
||||||
|
for (int y = 0; y < LEN; y++) {
|
||||||
|
for (int x = 0; x < LEN; x++) {
|
||||||
|
int h = ARR[x];
|
||||||
|
|
||||||
|
char *color = (char *)RESET;
|
||||||
|
if (ARR + x == a) {
|
||||||
|
color = (char *)YELLOW;
|
||||||
|
} else if (ARR + x == b) {
|
||||||
|
color = (char *)CYAN;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s%s", color, LEN - h <= y ? SOLID_BLOCK : " ");
|
||||||
|
}
|
||||||
|
printf("%s\n", RESET);
|
||||||
|
}
|
||||||
|
msleep(50);
|
||||||
|
}
|
||||||
|
|
||||||
|
void init(int *arr, int len, const char *name) {
|
||||||
|
COUNT = 0;
|
||||||
|
ARR = arr;
|
||||||
|
LEN = len;
|
||||||
|
NAME = (char *)name;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void gen_worst(int *arr, int len) {
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
arr[i] = len - i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_array(int *arr, int len) {
|
||||||
|
for (int x = 0; x < len; x++) {
|
||||||
|
printf("%s%d", x == 0 ? "" : ", ", arr[x]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void populate(int *arr, int len) {
|
||||||
|
for (int x = 0; x < len; x++) {
|
||||||
|
arr[x] = x + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void shuffle(int *arr, int len) {
|
||||||
|
for (int x = 0; x < len; x++) {
|
||||||
|
int y = rand() % len;
|
||||||
|
|
||||||
|
int t = arr[x];
|
||||||
|
arr[x] = arr[y];
|
||||||
|
arr[y] = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(int *a, int *b) {
|
||||||
|
int t = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = t;
|
||||||
|
|
||||||
|
#ifdef VISUALIZE
|
||||||
|
COUNT++;
|
||||||
|
draw(a, b);
|
||||||
|
#endif // VISUALIZE
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_sorted(int *arr, int len) {
|
||||||
|
int last = 0;
|
||||||
|
for (int x = 0; x < len; x++) {
|
||||||
|
if (last > arr[x]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
last = arr[x];
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
#ifndef _ARRAYS_H_
|
||||||
|
#define _ARRAYS_H_
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <Windows.h>
|
||||||
|
#else
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void populate(int *, int);
|
||||||
|
|
||||||
|
void shuffle(int *, int);
|
||||||
|
|
||||||
|
void gen_worst(int *, int);
|
||||||
|
|
||||||
|
void swap(int *, int *);
|
||||||
|
|
||||||
|
bool is_sorted(int *, int);
|
||||||
|
|
||||||
|
void print_array(int *, int);
|
||||||
|
|
||||||
|
void init(int *, int, const char *);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,5 @@
|
||||||
|
Main
|
||||||
|
CMakeFiles
|
||||||
|
cmake_install.cmake
|
||||||
|
CMakeCache.txt
|
||||||
|
MakeFile
|
|
@ -0,0 +1,3 @@
|
||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
project(Search)
|
||||||
|
add_executable(Main src/main.c src/search.h src/search.c src/bench.h src/bench.c)
|
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2022 Teridax
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
|
@ -0,0 +1,11 @@
|
||||||
|
# DHBW-Searches
|
||||||
|
Programm to compare the runtime of naive, kmp and boyer-moore search algorithms written in C.
|
||||||
|
## Test yourself
|
||||||
|
Clone the respository<br>
|
||||||
|
```git clone https://github.com/Servostar/DHBW-Searches.git```<br>
|
||||||
|
`cd DHBW-Searches`<br>
|
||||||
|
Generate build files<br>
|
||||||
|
`cmake .`<br>
|
||||||
|
Run cmake to build the code<br>
|
||||||
|
`cmake --build .`<br>
|
||||||
|
Now you can run the binary `Main`
|
|
@ -0,0 +1,31 @@
|
||||||
|
#include "bench.h"
|
||||||
|
|
||||||
|
void benchmark_search(bool (*algorithm)(), const char* name) {
|
||||||
|
// take start time
|
||||||
|
long long start = millis();
|
||||||
|
|
||||||
|
// run algorithm
|
||||||
|
bool match = algorithm();
|
||||||
|
|
||||||
|
// take end time and compute difference as passed time
|
||||||
|
long long delta = millis() - start;
|
||||||
|
|
||||||
|
printf("%s took: %lldms and found %s\n", name, delta, match ? "a match" : "nothing");
|
||||||
|
}
|
||||||
|
|
||||||
|
long long millis() {
|
||||||
|
// stores current time
|
||||||
|
struct timespec now;
|
||||||
|
// get unix timestap
|
||||||
|
timespec_get(&now, TIME_UTC);
|
||||||
|
|
||||||
|
long long millis = 0;
|
||||||
|
|
||||||
|
// convert seconds to millis and sum
|
||||||
|
millis += ((long long) now.tv_sec) * 1000;
|
||||||
|
// convert nanos to millis and sum
|
||||||
|
millis += ((long long) now.tv_nsec) / 1000000;
|
||||||
|
|
||||||
|
return millis;
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
#ifndef _BENCH_H_
|
||||||
|
#define _BENCH_H_
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
// return the passed millis in unix time
|
||||||
|
long long millis();
|
||||||
|
|
||||||
|
void benchmark_search(bool (*algorithm)(), const char* name);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,46 @@
|
||||||
|
#include "search.h"
|
||||||
|
#include "bench.h"
|
||||||
|
|
||||||
|
// length of our source string to search in
|
||||||
|
const long SOURCE_SIZE = 100000000;
|
||||||
|
// pattern to search in string
|
||||||
|
const char* PATTERN = "needle";
|
||||||
|
|
||||||
|
char* SOURCE;
|
||||||
|
|
||||||
|
void generate_source();
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
// allocate some space for our source string
|
||||||
|
SOURCE = (char*) malloc(SOURCE_SIZE);
|
||||||
|
|
||||||
|
// initalize source string with worst case search scenario for our pattern
|
||||||
|
generate_source();
|
||||||
|
|
||||||
|
// benachmark
|
||||||
|
benchmark_search(&naive_search, "naive");
|
||||||
|
benchmark_search(&kmp_search, "kmp");
|
||||||
|
benchmark_search(&boyermoore_search, "boyer-moore");
|
||||||
|
|
||||||
|
// free source string
|
||||||
|
free(SOURCE);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void generate_source() {
|
||||||
|
int patlen = strlen(PATTERN);
|
||||||
|
|
||||||
|
// fill the source string with our pattern and replace every last char of
|
||||||
|
// our pattern with a binary 1 to assure a missmatch
|
||||||
|
for (int i = 0; i < SOURCE_SIZE - patlen; i += patlen) {
|
||||||
|
memcpy(SOURCE + i, PATTERN, patlen);
|
||||||
|
SOURCE[i + patlen - 1] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// put our pattern at the end of our source,
|
||||||
|
// this will be the matching part of our source
|
||||||
|
memcpy(SOURCE + SOURCE_SIZE - patlen - 1, PATTERN, patlen);
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,100 @@
|
||||||
|
#include "search.h"
|
||||||
|
|
||||||
|
#define MAX(i, j) (((i) > (j)) ? (i) : (j))
|
||||||
|
|
||||||
|
bool boyermoore_search() {
|
||||||
|
|
||||||
|
int right[256];
|
||||||
|
memset(right, -1, 256 * sizeof(int));
|
||||||
|
|
||||||
|
int patlen = strlen(PATTERN);
|
||||||
|
for (int i = 0; i < patlen; i++) {
|
||||||
|
right[(unsigned int) PATTERN[i]] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
int skip = 0;
|
||||||
|
for (int i = 0; i <= SOURCE_SIZE - patlen; i += skip) {
|
||||||
|
skip = 0;
|
||||||
|
for (int k = patlen-1; k >= 0; k--) {
|
||||||
|
if (PATTERN[k] != SOURCE[i + k]) {
|
||||||
|
skip = MAX(1, k - right[SOURCE[i + k]]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (skip == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool kmp_search() {
|
||||||
|
|
||||||
|
int patlen = strlen(PATTERN);
|
||||||
|
int* jumptable = (int*) calloc(patlen, sizeof(int));
|
||||||
|
|
||||||
|
for (int i = 1, j = 0; i < patlen;) {
|
||||||
|
if (PATTERN[i] == PATTERN[j]) {
|
||||||
|
jumptable[i] = j + 1;
|
||||||
|
i ++;
|
||||||
|
j ++;
|
||||||
|
} else if (j > 0) {
|
||||||
|
j = jumptable[j - 1];
|
||||||
|
} else {
|
||||||
|
jumptable[i] = 0;
|
||||||
|
i ++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* pat = PATTERN;
|
||||||
|
|
||||||
|
int j = 0;
|
||||||
|
// for every character in source compare it to a charactor of our pattern
|
||||||
|
for (int i = 0; i < SOURCE_SIZE;) {
|
||||||
|
if (PATTERN[j] == SOURCE[i]) {
|
||||||
|
i ++;
|
||||||
|
j ++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (j == patlen) {
|
||||||
|
return true;
|
||||||
|
} else if (i < SOURCE_SIZE && PATTERN[j] != SOURCE[i]) {
|
||||||
|
if (j == 0) {
|
||||||
|
i ++;
|
||||||
|
} else {
|
||||||
|
j = jumptable[j - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
free(jumptable);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool naive_search() {
|
||||||
|
|
||||||
|
// loop through our source string
|
||||||
|
for (int i = 0; i < SOURCE_SIZE; i++) {
|
||||||
|
|
||||||
|
const char* src = SOURCE + i;
|
||||||
|
for (const char* pat = PATTERN; *src != '\0';) {
|
||||||
|
if (*pat != *src) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
pat ++;
|
||||||
|
src ++;
|
||||||
|
|
||||||
|
if (*pat == '\0') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
#ifndef _SEARCH_H_
|
||||||
|
#define _SEARCH_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
// string to search for
|
||||||
|
extern const char* PATTERN;
|
||||||
|
// source to search in
|
||||||
|
extern char* SOURCE;
|
||||||
|
// length of our source as a constant, saves some time when
|
||||||
|
// our source is several mb large
|
||||||
|
extern const long SOURCE_SIZE;
|
||||||
|
|
||||||
|
bool naive_search();
|
||||||
|
|
||||||
|
bool kmp_search();
|
||||||
|
|
||||||
|
bool boyermoore_search();
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,13 @@
|
||||||
|
CMakeLists.txt.user
|
||||||
|
CMakeCache.txt
|
||||||
|
CMakeFiles
|
||||||
|
CMakeScripts
|
||||||
|
Testing
|
||||||
|
Makefile
|
||||||
|
cmake_install.cmake
|
||||||
|
install_manifest.txt
|
||||||
|
compile_commands.json
|
||||||
|
CTestTestfile.cmake
|
||||||
|
_deps
|
||||||
|
Main
|
||||||
|
.cmake
|
|
@ -0,0 +1,23 @@
|
||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
#set(CMAKE_C_STANDARD 99) # we will use C99
|
||||||
|
project(Search)
|
||||||
|
add_executable(
|
||||||
|
Main
|
||||||
|
|
||||||
|
src/arrays.h
|
||||||
|
src/arrays.c
|
||||||
|
src/bench.h
|
||||||
|
src/bench.c
|
||||||
|
src/sort.h
|
||||||
|
src/main.c
|
||||||
|
src/sort/bubble.c
|
||||||
|
src/sort/selection.c
|
||||||
|
src/sort/insertion.c
|
||||||
|
src/sort/quick.c
|
||||||
|
src/sort/merge.c
|
||||||
|
src/sort/gnome.c
|
||||||
|
src/sort/bogo.c
|
||||||
|
src/sort/bozo.c
|
||||||
|
src/sort/shell.c
|
||||||
|
src/sort/radix.c
|
||||||
|
)
|
|
@ -0,0 +1,339 @@
|
||||||
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
Version 2, June 1991
|
||||||
|
|
||||||
|
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||||
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
Preamble
|
||||||
|
|
||||||
|
The licenses for most software are designed to take away your
|
||||||
|
freedom to share and change it. By contrast, the GNU General Public
|
||||||
|
License is intended to guarantee your freedom to share and change free
|
||||||
|
software--to make sure the software is free for all its users. This
|
||||||
|
General Public License applies to most of the Free Software
|
||||||
|
Foundation's software and to any other program whose authors commit to
|
||||||
|
using it. (Some other Free Software Foundation software is covered by
|
||||||
|
the GNU Lesser General Public License instead.) You can apply it to
|
||||||
|
your programs, too.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
|
have the freedom to distribute copies of free software (and charge for
|
||||||
|
this service if you wish), that you receive source code or can get it
|
||||||
|
if you want it, that you can change the software or use pieces of it
|
||||||
|
in new free programs; and that you know you can do these things.
|
||||||
|
|
||||||
|
To protect your rights, we need to make restrictions that forbid
|
||||||
|
anyone to deny you these rights or to ask you to surrender the rights.
|
||||||
|
These restrictions translate to certain responsibilities for you if you
|
||||||
|
distribute copies of the software, or if you modify it.
|
||||||
|
|
||||||
|
For example, if you distribute copies of such a program, whether
|
||||||
|
gratis or for a fee, you must give the recipients all the rights that
|
||||||
|
you have. You must make sure that they, too, receive or can get the
|
||||||
|
source code. And you must show them these terms so they know their
|
||||||
|
rights.
|
||||||
|
|
||||||
|
We protect your rights with two steps: (1) copyright the software, and
|
||||||
|
(2) offer you this license which gives you legal permission to copy,
|
||||||
|
distribute and/or modify the software.
|
||||||
|
|
||||||
|
Also, for each author's protection and ours, we want to make certain
|
||||||
|
that everyone understands that there is no warranty for this free
|
||||||
|
software. If the software is modified by someone else and passed on, we
|
||||||
|
want its recipients to know that what they have is not the original, so
|
||||||
|
that any problems introduced by others will not reflect on the original
|
||||||
|
authors' reputations.
|
||||||
|
|
||||||
|
Finally, any free program is threatened constantly by software
|
||||||
|
patents. We wish to avoid the danger that redistributors of a free
|
||||||
|
program will individually obtain patent licenses, in effect making the
|
||||||
|
program proprietary. To prevent this, we have made it clear that any
|
||||||
|
patent must be licensed for everyone's free use or not licensed at all.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||||
|
|
||||||
|
0. This License applies to any program or other work which contains
|
||||||
|
a notice placed by the copyright holder saying it may be distributed
|
||||||
|
under the terms of this General Public License. The "Program", below,
|
||||||
|
refers to any such program or work, and a "work based on the Program"
|
||||||
|
means either the Program or any derivative work under copyright law:
|
||||||
|
that is to say, a work containing the Program or a portion of it,
|
||||||
|
either verbatim or with modifications and/or translated into another
|
||||||
|
language. (Hereinafter, translation is included without limitation in
|
||||||
|
the term "modification".) Each licensee is addressed as "you".
|
||||||
|
|
||||||
|
Activities other than copying, distribution and modification are not
|
||||||
|
covered by this License; they are outside its scope. The act of
|
||||||
|
running the Program is not restricted, and the output from the Program
|
||||||
|
is covered only if its contents constitute a work based on the
|
||||||
|
Program (independent of having been made by running the Program).
|
||||||
|
Whether that is true depends on what the Program does.
|
||||||
|
|
||||||
|
1. You may copy and distribute verbatim copies of the Program's
|
||||||
|
source code as you receive it, in any medium, provided that you
|
||||||
|
conspicuously and appropriately publish on each copy an appropriate
|
||||||
|
copyright notice and disclaimer of warranty; keep intact all the
|
||||||
|
notices that refer to this License and to the absence of any warranty;
|
||||||
|
and give any other recipients of the Program a copy of this License
|
||||||
|
along with the Program.
|
||||||
|
|
||||||
|
You may charge a fee for the physical act of transferring a copy, and
|
||||||
|
you may at your option offer warranty protection in exchange for a fee.
|
||||||
|
|
||||||
|
2. You may modify your copy or copies of the Program or any portion
|
||||||
|
of it, thus forming a work based on the Program, and copy and
|
||||||
|
distribute such modifications or work under the terms of Section 1
|
||||||
|
above, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) You must cause the modified files to carry prominent notices
|
||||||
|
stating that you changed the files and the date of any change.
|
||||||
|
|
||||||
|
b) You must cause any work that you distribute or publish, that in
|
||||||
|
whole or in part contains or is derived from the Program or any
|
||||||
|
part thereof, to be licensed as a whole at no charge to all third
|
||||||
|
parties under the terms of this License.
|
||||||
|
|
||||||
|
c) If the modified program normally reads commands interactively
|
||||||
|
when run, you must cause it, when started running for such
|
||||||
|
interactive use in the most ordinary way, to print or display an
|
||||||
|
announcement including an appropriate copyright notice and a
|
||||||
|
notice that there is no warranty (or else, saying that you provide
|
||||||
|
a warranty) and that users may redistribute the program under
|
||||||
|
these conditions, and telling the user how to view a copy of this
|
||||||
|
License. (Exception: if the Program itself is interactive but
|
||||||
|
does not normally print such an announcement, your work based on
|
||||||
|
the Program is not required to print an announcement.)
|
||||||
|
|
||||||
|
These requirements apply to the modified work as a whole. If
|
||||||
|
identifiable sections of that work are not derived from the Program,
|
||||||
|
and can be reasonably considered independent and separate works in
|
||||||
|
themselves, then this License, and its terms, do not apply to those
|
||||||
|
sections when you distribute them as separate works. But when you
|
||||||
|
distribute the same sections as part of a whole which is a work based
|
||||||
|
on the Program, the distribution of the whole must be on the terms of
|
||||||
|
this License, whose permissions for other licensees extend to the
|
||||||
|
entire whole, and thus to each and every part regardless of who wrote it.
|
||||||
|
|
||||||
|
Thus, it is not the intent of this section to claim rights or contest
|
||||||
|
your rights to work written entirely by you; rather, the intent is to
|
||||||
|
exercise the right to control the distribution of derivative or
|
||||||
|
collective works based on the Program.
|
||||||
|
|
||||||
|
In addition, mere aggregation of another work not based on the Program
|
||||||
|
with the Program (or with a work based on the Program) on a volume of
|
||||||
|
a storage or distribution medium does not bring the other work under
|
||||||
|
the scope of this License.
|
||||||
|
|
||||||
|
3. You may copy and distribute the Program (or a work based on it,
|
||||||
|
under Section 2) in object code or executable form under the terms of
|
||||||
|
Sections 1 and 2 above provided that you also do one of the following:
|
||||||
|
|
||||||
|
a) Accompany it with the complete corresponding machine-readable
|
||||||
|
source code, which must be distributed under the terms of Sections
|
||||||
|
1 and 2 above on a medium customarily used for software interchange; or,
|
||||||
|
|
||||||
|
b) Accompany it with a written offer, valid for at least three
|
||||||
|
years, to give any third party, for a charge no more than your
|
||||||
|
cost of physically performing source distribution, a complete
|
||||||
|
machine-readable copy of the corresponding source code, to be
|
||||||
|
distributed under the terms of Sections 1 and 2 above on a medium
|
||||||
|
customarily used for software interchange; or,
|
||||||
|
|
||||||
|
c) Accompany it with the information you received as to the offer
|
||||||
|
to distribute corresponding source code. (This alternative is
|
||||||
|
allowed only for noncommercial distribution and only if you
|
||||||
|
received the program in object code or executable form with such
|
||||||
|
an offer, in accord with Subsection b above.)
|
||||||
|
|
||||||
|
The source code for a work means the preferred form of the work for
|
||||||
|
making modifications to it. For an executable work, complete source
|
||||||
|
code means all the source code for all modules it contains, plus any
|
||||||
|
associated interface definition files, plus the scripts used to
|
||||||
|
control compilation and installation of the executable. However, as a
|
||||||
|
special exception, the source code distributed need not include
|
||||||
|
anything that is normally distributed (in either source or binary
|
||||||
|
form) with the major components (compiler, kernel, and so on) of the
|
||||||
|
operating system on which the executable runs, unless that component
|
||||||
|
itself accompanies the executable.
|
||||||
|
|
||||||
|
If distribution of executable or object code is made by offering
|
||||||
|
access to copy from a designated place, then offering equivalent
|
||||||
|
access to copy the source code from the same place counts as
|
||||||
|
distribution of the source code, even though third parties are not
|
||||||
|
compelled to copy the source along with the object code.
|
||||||
|
|
||||||
|
4. You may not copy, modify, sublicense, or distribute the Program
|
||||||
|
except as expressly provided under this License. Any attempt
|
||||||
|
otherwise to copy, modify, sublicense or distribute the Program is
|
||||||
|
void, and will automatically terminate your rights under this License.
|
||||||
|
However, parties who have received copies, or rights, from you under
|
||||||
|
this License will not have their licenses terminated so long as such
|
||||||
|
parties remain in full compliance.
|
||||||
|
|
||||||
|
5. You are not required to accept this License, since you have not
|
||||||
|
signed it. However, nothing else grants you permission to modify or
|
||||||
|
distribute the Program or its derivative works. These actions are
|
||||||
|
prohibited by law if you do not accept this License. Therefore, by
|
||||||
|
modifying or distributing the Program (or any work based on the
|
||||||
|
Program), you indicate your acceptance of this License to do so, and
|
||||||
|
all its terms and conditions for copying, distributing or modifying
|
||||||
|
the Program or works based on it.
|
||||||
|
|
||||||
|
6. Each time you redistribute the Program (or any work based on the
|
||||||
|
Program), the recipient automatically receives a license from the
|
||||||
|
original licensor to copy, distribute or modify the Program subject to
|
||||||
|
these terms and conditions. You may not impose any further
|
||||||
|
restrictions on the recipients' exercise of the rights granted herein.
|
||||||
|
You are not responsible for enforcing compliance by third parties to
|
||||||
|
this License.
|
||||||
|
|
||||||
|
7. If, as a consequence of a court judgment or allegation of patent
|
||||||
|
infringement or for any other reason (not limited to patent issues),
|
||||||
|
conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot
|
||||||
|
distribute so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you
|
||||||
|
may not distribute the Program at all. For example, if a patent
|
||||||
|
license would not permit royalty-free redistribution of the Program by
|
||||||
|
all those who receive copies directly or indirectly through you, then
|
||||||
|
the only way you could satisfy both it and this License would be to
|
||||||
|
refrain entirely from distribution of the Program.
|
||||||
|
|
||||||
|
If any portion of this section is held invalid or unenforceable under
|
||||||
|
any particular circumstance, the balance of the section is intended to
|
||||||
|
apply and the section as a whole is intended to apply in other
|
||||||
|
circumstances.
|
||||||
|
|
||||||
|
It is not the purpose of this section to induce you to infringe any
|
||||||
|
patents or other property right claims or to contest validity of any
|
||||||
|
such claims; this section has the sole purpose of protecting the
|
||||||
|
integrity of the free software distribution system, which is
|
||||||
|
implemented by public license practices. Many people have made
|
||||||
|
generous contributions to the wide range of software distributed
|
||||||
|
through that system in reliance on consistent application of that
|
||||||
|
system; it is up to the author/donor to decide if he or she is willing
|
||||||
|
to distribute software through any other system and a licensee cannot
|
||||||
|
impose that choice.
|
||||||
|
|
||||||
|
This section is intended to make thoroughly clear what is believed to
|
||||||
|
be a consequence of the rest of this License.
|
||||||
|
|
||||||
|
8. If the distribution and/or use of the Program is restricted in
|
||||||
|
certain countries either by patents or by copyrighted interfaces, the
|
||||||
|
original copyright holder who places the Program under this License
|
||||||
|
may add an explicit geographical distribution limitation excluding
|
||||||
|
those countries, so that distribution is permitted only in or among
|
||||||
|
countries not thus excluded. In such case, this License incorporates
|
||||||
|
the limitation as if written in the body of this License.
|
||||||
|
|
||||||
|
9. The Free Software Foundation may publish revised and/or new versions
|
||||||
|
of the General Public License from time to time. Such new versions will
|
||||||
|
be similar in spirit to the present version, but may differ in detail to
|
||||||
|
address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the Program
|
||||||
|
specifies a version number of this License which applies to it and "any
|
||||||
|
later version", you have the option of following the terms and conditions
|
||||||
|
either of that version or of any later version published by the Free
|
||||||
|
Software Foundation. If the Program does not specify a version number of
|
||||||
|
this License, you may choose any version ever published by the Free Software
|
||||||
|
Foundation.
|
||||||
|
|
||||||
|
10. If you wish to incorporate parts of the Program into other free
|
||||||
|
programs whose distribution conditions are different, write to the author
|
||||||
|
to ask for permission. For software which is copyrighted by the Free
|
||||||
|
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||||
|
make exceptions for this. Our decision will be guided by the two goals
|
||||||
|
of preserving the free status of all derivatives of our free software and
|
||||||
|
of promoting the sharing and reuse of software generally.
|
||||||
|
|
||||||
|
NO WARRANTY
|
||||||
|
|
||||||
|
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||||
|
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||||
|
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||||
|
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||||
|
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||||
|
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||||
|
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||||
|
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||||
|
REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||||
|
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||||
|
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||||
|
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||||
|
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||||
|
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||||
|
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||||
|
POSSIBILITY OF SUCH DAMAGES.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest
|
||||||
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
free software which everyone can redistribute and change under these terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest
|
||||||
|
to attach them to the start of each source file to most effectively
|
||||||
|
convey the exclusion of warranty; and each file should have at least
|
||||||
|
the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; either version 2 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License along
|
||||||
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If the program is interactive, make it output a short notice like this
|
||||||
|
when it starts in an interactive mode:
|
||||||
|
|
||||||
|
Gnomovision version 69, Copyright (C) year name of author
|
||||||
|
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
|
This is free software, and you are welcome to redistribute it
|
||||||
|
under certain conditions; type `show c' for details.
|
||||||
|
|
||||||
|
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||||
|
parts of the General Public License. Of course, the commands you use may
|
||||||
|
be called something other than `show w' and `show c'; they could even be
|
||||||
|
mouse-clicks or menu items--whatever suits your program.
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or your
|
||||||
|
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||||
|
necessary. Here is a sample; alter the names:
|
||||||
|
|
||||||
|
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||||
|
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||||
|
|
||||||
|
<signature of Ty Coon>, 1 April 1989
|
||||||
|
Ty Coon, President of Vice
|
||||||
|
|
||||||
|
This General Public License does not permit incorporating your program into
|
||||||
|
proprietary programs. If your program is a subroutine library, you may
|
||||||
|
consider it more useful to permit linking proprietary applications with the
|
||||||
|
library. If this is what you want to do, use the GNU Lesser General
|
||||||
|
Public License instead of this License.
|
|
@ -0,0 +1,30 @@
|
||||||
|
# DHBW-Sorting
|
||||||
|
C implementation and benchmarking of the sorting algorithms from class.
|
||||||
|
It contains code for benchmarking serveral sorting algorithms. The algorithms are not designed to be the best version of them but to be simple and easy to understand. They do not share common charasteristics such as working in-place or being stable. Focus for this project was on simplicity and getting a basic comparison done.
|
||||||
|
All of them sort the same array of integers which is filled with values between `[0..len]` with `len`being the size of the array. Depending on the case the order is swapped, randomized or already sorted.
|
||||||
|
The output will look something like this:
|
||||||
|
```
|
||||||
|
+--------------+---------+---------+---------+
|
||||||
|
| Algorithm | Best(ms)| Avg(ms) |Worst(ms)|
|
||||||
|
+--------------+---------+---------+---------+
|
||||||
|
| bubble | 94 | 236 | 221 |
|
||||||
|
| gnome | 0 | 172 | 355 |
|
||||||
|
| selection | 99 | 97 | 94 |
|
||||||
|
| quick | 221 | 2 | 165 |
|
||||||
|
| merge | 3 | 5 | 3 |
|
||||||
|
| shell | 0 | 2 | 0 |
|
||||||
|
| insertion | 0 | 103 | 214 |
|
||||||
|
| radix | 0 | 1 | 0 |
|
||||||
|
+--------------+---------+---------+---------+
|
||||||
|
```
|
||||||
|
Your will see a nice ascii table in which you can comapre each algorithms timings in the best (already sorted), average (randomized) and worst case (inverted).
|
||||||
|
Main confiuration can be found in `main.c`. You can alter `LEN` to specifiy the amount of elements in the array to be sorted and change the order of the algorithms to be benchmarked.
|
||||||
|
# Dependencies for quick build
|
||||||
|
Requires `cmake`and a C-compiler.
|
||||||
|
# Try yourself
|
||||||
|
Run the following commands:<br>
|
||||||
|
`git clone https://github.com/Servostar/DHBW-Sorting.git`<br>
|
||||||
|
`cd DHBW-Sorting`<br>
|
||||||
|
`cmake .`<br>
|
||||||
|
`cmake --build .`<br>
|
||||||
|
`./Main` or for Windows `Main.exe`
|
|
@ -0,0 +1,117 @@
|
||||||
|
#include "arrays.h"
|
||||||
|
#include "sort.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#ifdef VISUALIZE
|
||||||
|
|
||||||
|
static int *ARR;
|
||||||
|
static int LEN;
|
||||||
|
static char *NAME;
|
||||||
|
static int COUNT;
|
||||||
|
|
||||||
|
/* msleep(): Sleep for the requested number of milliseconds. */
|
||||||
|
int msleep(long msec) {
|
||||||
|
struct timespec ts;
|
||||||
|
int res;
|
||||||
|
|
||||||
|
if (msec < 0) {
|
||||||
|
errno = EINVAL;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ts.tv_sec = msec / 1000;
|
||||||
|
ts.tv_nsec = (msec % 1000) * 1000000;
|
||||||
|
|
||||||
|
do {
|
||||||
|
res = nanosleep(&ts, &ts);
|
||||||
|
} while (res && errno == EINTR);
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw(int *a, int *b) {
|
||||||
|
const char *SOLID_BLOCK = "\u2588";
|
||||||
|
const char *YELLOW = "\e[0;33m";
|
||||||
|
const char *CYAN = "\e[0;36m";
|
||||||
|
const char *RESET = "\e[0m";
|
||||||
|
|
||||||
|
printf("\e[1;1H\e[2J");
|
||||||
|
printf("Algorithm: %s Swaps: %d\n", NAME, COUNT);
|
||||||
|
for (int y = 0; y < LEN; y++) {
|
||||||
|
for (int x = 0; x < LEN; x++) {
|
||||||
|
int h = ARR[x];
|
||||||
|
|
||||||
|
char *color = (char *)RESET;
|
||||||
|
if (ARR + x == a) {
|
||||||
|
color = (char *)YELLOW;
|
||||||
|
} else if (ARR + x == b) {
|
||||||
|
color = (char *)CYAN;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s%s", color, LEN - h <= y ? SOLID_BLOCK : " ");
|
||||||
|
}
|
||||||
|
printf("%s\n", RESET);
|
||||||
|
}
|
||||||
|
msleep(50);
|
||||||
|
}
|
||||||
|
|
||||||
|
void init(int *arr, int len, const char *name) {
|
||||||
|
COUNT = 0;
|
||||||
|
ARR = arr;
|
||||||
|
LEN = len;
|
||||||
|
NAME = (char *)name;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void gen_worst(int *arr, int len) {
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
arr[i] = len - i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_array(int *arr, int len) {
|
||||||
|
for (int x = 0; x < len; x++) {
|
||||||
|
printf("%s%d", x == 0 ? "" : ", ", arr[x]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void populate(int *arr, int len) {
|
||||||
|
for (int x = 0; x < len; x++) {
|
||||||
|
arr[x] = x + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void shuffle(int *arr, int len) {
|
||||||
|
for (int x = 0; x < len; x++) {
|
||||||
|
int y = rand() % len;
|
||||||
|
|
||||||
|
int t = arr[x];
|
||||||
|
arr[x] = arr[y];
|
||||||
|
arr[y] = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void swap(int *a, int *b) {
|
||||||
|
int t = *a;
|
||||||
|
*a = *b;
|
||||||
|
*b = t;
|
||||||
|
|
||||||
|
#ifdef VISUALIZE
|
||||||
|
COUNT++;
|
||||||
|
draw(a, b);
|
||||||
|
#endif // VISUALIZE
|
||||||
|
}
|
||||||
|
|
||||||
|
bool is_sorted(int *arr, int len) {
|
||||||
|
int last = 0;
|
||||||
|
for (int x = 0; x < len; x++) {
|
||||||
|
if (last > arr[x]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
last = arr[x];
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
#ifndef _ARRAYS_H_
|
||||||
|
#define _ARRAYS_H_
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <Windows.h>
|
||||||
|
#else
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//#define VISUALIZE
|
||||||
|
|
||||||
|
void populate(int *, int);
|
||||||
|
|
||||||
|
void shuffle(int *, int);
|
||||||
|
|
||||||
|
void gen_worst(int *, int);
|
||||||
|
|
||||||
|
void swap(int *, int *);
|
||||||
|
|
||||||
|
bool is_sorted(int *, int);
|
||||||
|
|
||||||
|
void print_array(int *, int);
|
||||||
|
|
||||||
|
void init(int *, int, const char *);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,116 @@
|
||||||
|
#include "bench.h"
|
||||||
|
#include "arrays.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
long long millis();
|
||||||
|
|
||||||
|
long long benchmark_sort(void (*algorithm)(int *, int), const char *name,
|
||||||
|
int *arr, int len, Case _case) {
|
||||||
|
|
||||||
|
#ifdef VISUALIZE
|
||||||
|
init(arr, len, name);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// take start time
|
||||||
|
long long start = millis();
|
||||||
|
|
||||||
|
switch (_case) {
|
||||||
|
case Best:
|
||||||
|
populate(arr, len);
|
||||||
|
break;
|
||||||
|
case Average:
|
||||||
|
shuffle(arr, len);
|
||||||
|
break;
|
||||||
|
case Worst:
|
||||||
|
gen_worst(arr, len);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// run algorithm
|
||||||
|
algorithm(arr, len);
|
||||||
|
|
||||||
|
if (!is_sorted(arr, len)) {
|
||||||
|
printf("Algorithm %s didn't pass test", name);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// take end time and compute difference as passed time
|
||||||
|
long long delta = millis() - start;
|
||||||
|
|
||||||
|
return delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
long long millis() {
|
||||||
|
// stores current time
|
||||||
|
struct timespec now;
|
||||||
|
// get unix timestap
|
||||||
|
timespec_get(&now, TIME_UTC);
|
||||||
|
|
||||||
|
long long millis = 0;
|
||||||
|
|
||||||
|
// convert seconds to millis and sum
|
||||||
|
millis += ((long long)now.tv_sec) * 1000;
|
||||||
|
// convert nanos to millis and sum
|
||||||
|
millis += ((long long)now.tv_nsec) / 1000000;
|
||||||
|
|
||||||
|
return millis;
|
||||||
|
}
|
||||||
|
|
||||||
|
void col_adjust(const char *str, int col_width) {
|
||||||
|
printf("%s", str);
|
||||||
|
|
||||||
|
int left = col_width - strlen(str);
|
||||||
|
|
||||||
|
for (int k = 0; k < left; k++) {
|
||||||
|
printf(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("|");
|
||||||
|
}
|
||||||
|
|
||||||
|
void publish(const Sort *sorts, long long *data, int len) {
|
||||||
|
printf("+--------------+---------+---------+---------+\n");
|
||||||
|
printf("| Algorithm | Best(ms)| Avg(ms) |Worst(ms)|\n");
|
||||||
|
printf("+--------------+---------+---------+---------+\n");
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
printf("| ");
|
||||||
|
col_adjust(sorts[i].name, 13);
|
||||||
|
|
||||||
|
char buf[9] = {' '};
|
||||||
|
sprintf(buf, "% lld", data[i * 3]);
|
||||||
|
col_adjust(buf, 9);
|
||||||
|
|
||||||
|
sprintf(buf, "% lld", data[i * 3 + 1]);
|
||||||
|
col_adjust(buf, 9);
|
||||||
|
|
||||||
|
sprintf(buf, "% lld", data[i * 3 + 2]);
|
||||||
|
col_adjust(buf, 9);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
printf("+--------------+---------+---------+---------+\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void benchmark(const Sort *sorts, int len, int *arr, int arr_len) {
|
||||||
|
|
||||||
|
long long *data = (long long *)malloc(sizeof(long long) * len * 3);
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
data[i * 3] =
|
||||||
|
benchmark_sort(sorts[i].algorithm, sorts[i].name, arr, arr_len, Best);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
data[i * 3 + 1] = benchmark_sort(sorts[i].algorithm, sorts[i].name, arr,
|
||||||
|
arr_len, Average);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
data[i * 3 + 2] =
|
||||||
|
benchmark_sort(sorts[i].algorithm, sorts[i].name, arr, arr_len, Worst);
|
||||||
|
}
|
||||||
|
|
||||||
|
publish(sorts, data, len);
|
||||||
|
|
||||||
|
free(data);
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
#ifndef _BENCH_H_
|
||||||
|
#define _BENCH_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
typedef enum Cases { Best, Average, Worst } Case;
|
||||||
|
|
||||||
|
typedef void(sort)(int *, int);
|
||||||
|
|
||||||
|
typedef struct Sort {
|
||||||
|
const char *name;
|
||||||
|
sort *algorithm;
|
||||||
|
} Sort;
|
||||||
|
|
||||||
|
long long benchmark_sort(sort, const char *, int *, int, Case _case);
|
||||||
|
|
||||||
|
void benchmark(const Sort *, int len, int *, int);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,27 @@
|
||||||
|
#include "arrays.h"
|
||||||
|
#include "bench.h"
|
||||||
|
#include "sort.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
const int LEN = 10000;
|
||||||
|
|
||||||
|
const Sort TESTS[] = {
|
||||||
|
{"bubble", bubble}, {"gnome", gnome}, {"selection", selection},
|
||||||
|
{"quick", quick}, {"merge", merge}, {"shell", shell},
|
||||||
|
{"insertion", insertion}, {"radix", radix},
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
|
||||||
|
int *arr = (int *)malloc(sizeof(int) * LEN);
|
||||||
|
|
||||||
|
populate(arr, LEN);
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
benchmark(TESTS, sizeof(TESTS) / sizeof(Sort), arr, LEN);
|
||||||
|
|
||||||
|
free(arr);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,20 @@
|
||||||
|
#ifndef _SORT_H_
|
||||||
|
#define _SORT_H_
|
||||||
|
|
||||||
|
void bubble(int *, int);
|
||||||
|
void selection(int *, int);
|
||||||
|
void insertion(int *, int);
|
||||||
|
void shell(int *, int);
|
||||||
|
void quick(int *, int);
|
||||||
|
void gnome(int *, int);
|
||||||
|
// shuffle the entire array randomly until its sorted
|
||||||
|
void bogo(int *, int);
|
||||||
|
// like bogo sort, but it won't shuffle the entire array
|
||||||
|
// for each iteration but only swap two *random* values
|
||||||
|
void bozo(int *, int);
|
||||||
|
void merge(int *, int);
|
||||||
|
|
||||||
|
void radix(int *, int);
|
||||||
|
// void sleep(int*,int);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,8 @@
|
||||||
|
#include "../arrays.h"
|
||||||
|
#include "../sort.h"
|
||||||
|
|
||||||
|
void bogo(int *arr, int len) {
|
||||||
|
while(!is_sorted(arr, len)) {
|
||||||
|
shuffle(arr, len);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
#include "../arrays.h"
|
||||||
|
#include "../sort.h"
|
||||||
|
|
||||||
|
void bozo(int *arr, int len) {
|
||||||
|
while(!is_sorted(arr, len)) {
|
||||||
|
int x = rand() % len;
|
||||||
|
int y = rand() % len;
|
||||||
|
|
||||||
|
swap(arr + x, arr + y);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
#include "../arrays.h"
|
||||||
|
#include "../sort.h"
|
||||||
|
|
||||||
|
// always shift the biggest element up to the right
|
||||||
|
void bubble(int *arr, int len) {
|
||||||
|
|
||||||
|
for (int x = 0; x < len - 1; x++)
|
||||||
|
|
||||||
|
for (int y = 0; y < len - x - 1; y++)
|
||||||
|
if (arr[y] > arr[y + 1])
|
||||||
|
// current element is bigger, shift to the right
|
||||||
|
swap(arr + y, arr + y + 1);
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
#include "../arrays.h"
|
||||||
|
#include "../sort.h"
|
||||||
|
|
||||||
|
void gnome(int *arr, int len) {
|
||||||
|
|
||||||
|
for (int x = 1; x < len; x++) {
|
||||||
|
if (arr[x] < arr[x - 1]) {
|
||||||
|
swap(arr + x, arr + x - 1);
|
||||||
|
x -= 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,17 @@
|
||||||
|
#include "../arrays.h"
|
||||||
|
#include "../sort.h"
|
||||||
|
|
||||||
|
void insertion(int *arr, int len) {
|
||||||
|
for (int x = 1; x < len; x++) {
|
||||||
|
int c = arr[x];
|
||||||
|
|
||||||
|
for (int y = x - 1; y >= 0; y--) {
|
||||||
|
if (arr[y] > c) {
|
||||||
|
swap(arr + y, arr + y + 1);
|
||||||
|
} else {
|
||||||
|
arr[y + 1] = c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
#include "../arrays.h"
|
||||||
|
#include "../sort.h"
|
||||||
|
|
||||||
|
void merge(int *arr, int len) {
|
||||||
|
if (len < 2)
|
||||||
|
return;
|
||||||
|
|
||||||
|
int m = len / 2;
|
||||||
|
|
||||||
|
merge(arr, m);
|
||||||
|
merge(arr + m, len - m);
|
||||||
|
|
||||||
|
shell(arr, len);
|
||||||
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
#include "../arrays.h"
|
||||||
|
#include "../sort.h"
|
||||||
|
|
||||||
|
void quick(int *arr, int len) {
|
||||||
|
if (len < 1)
|
||||||
|
return;
|
||||||
|
|
||||||
|
int pivot = arr[len - 1];
|
||||||
|
|
||||||
|
int x = -1;
|
||||||
|
for (int y = 0; y < len - 1; y++) {
|
||||||
|
if (arr[y] < pivot) {
|
||||||
|
x ++;
|
||||||
|
swap(arr + x, arr + y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
x++;
|
||||||
|
swap(arr + x, arr + len - 1);
|
||||||
|
|
||||||
|
quick(arr, x);
|
||||||
|
quick(arr + x + 1, len - x - 1);
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
#include "../arrays.h"
|
||||||
|
#include "../sort.h"
|
||||||
|
|
||||||
|
void radix(int *arr, int len) {
|
||||||
|
|
||||||
|
int *tmp = (int *)malloc(len * sizeof(int));
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
int v = arr[i];
|
||||||
|
|
||||||
|
tmp[v] = v;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
arr[i] = tmp[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
free(tmp);
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
#include "../arrays.h"
|
||||||
|
#include "../sort.h"
|
||||||
|
|
||||||
|
void selection(int *arr, int len) {
|
||||||
|
for (int x = 0; x < len - 1; x++) {
|
||||||
|
int e = x;
|
||||||
|
|
||||||
|
for (int y = x + 1; y < len; y++)
|
||||||
|
if (arr[y] < arr[e])
|
||||||
|
e = y;
|
||||||
|
|
||||||
|
swap(arr + e, arr + x);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
#include "../arrays.h"
|
||||||
|
#include "../sort.h"
|
||||||
|
|
||||||
|
void shell(int *arr, int len) {
|
||||||
|
|
||||||
|
for (int x = len / 2; x > 0; x /= 2) {
|
||||||
|
|
||||||
|
for (int y = x; y < len; y++) {
|
||||||
|
int t = arr[y];
|
||||||
|
|
||||||
|
int i = y;
|
||||||
|
for (; i >= x && arr[i - x] > t; i -= x) {
|
||||||
|
arr[i] = arr[i - x];
|
||||||
|
swap(arr + i, arr + i - x);
|
||||||
|
}
|
||||||
|
arr[i] = t;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,13 @@
|
||||||
|
CMakeLists.txt.user
|
||||||
|
CMakeCache.txt
|
||||||
|
CMakeFiles
|
||||||
|
CMakeScripts
|
||||||
|
Testing
|
||||||
|
Makefile
|
||||||
|
cmake_install.cmake
|
||||||
|
install_manifest.txt
|
||||||
|
compile_commands.json
|
||||||
|
CTestTestfile.cmake
|
||||||
|
_deps
|
||||||
|
InterestCalc
|
||||||
|
info_export.txt
|
|
@ -0,0 +1,6 @@
|
||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
project(InterestCalculator)
|
||||||
|
# get all .c files recursesively from ./src
|
||||||
|
file(GLOB_RECURSE Sources RELATIVE ${CMAKE_SOURCE_DIR} "src/*.c")
|
||||||
|
# cramble all source into one binary
|
||||||
|
add_executable(InterestCalc ${Sources})
|
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2022 Teridax
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
|
@ -0,0 +1,2 @@
|
||||||
|
# DHBW-InterestCalc
|
||||||
|
Program desgin for C99 from school
|
|
@ -0,0 +1,82 @@
|
||||||
|
#include "calculator.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief
|
||||||
|
*
|
||||||
|
* @param interestRate
|
||||||
|
* @param growthRate
|
||||||
|
* @return true
|
||||||
|
* @return false
|
||||||
|
*/
|
||||||
|
bool input_rate_and_growth(double *interestRate, double *growthRate) {
|
||||||
|
|
||||||
|
do {
|
||||||
|
printf("\tEnter interest rate [0.01; 10]: ");
|
||||||
|
fscanf(stdin, "%lf", interestRate);
|
||||||
|
} while(*interestRate > 10.0 || *interestRate < 0.01);
|
||||||
|
|
||||||
|
do {
|
||||||
|
printf("\tEnter growth rate [0.01; 5]: ");
|
||||||
|
fscanf(stdin, "%lf", growthRate);
|
||||||
|
} while (*growthRate > 5.0 || *growthRate < 0.01);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
double calculate_interest_growth(double tab[][3], int duration, double investmentAmount) {
|
||||||
|
// declare variables
|
||||||
|
double interestRate;
|
||||||
|
double growthRate;
|
||||||
|
double totalInterests = 0;
|
||||||
|
|
||||||
|
if (!input_rate_and_growth(&interestRate, &growthRate)) {
|
||||||
|
return -1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialize 1. year
|
||||||
|
tab[0][0] = interestRate;
|
||||||
|
tab[0][1] = investmentAmount;
|
||||||
|
tab[0][2] = investmentAmount * interestRate * 1e-2;
|
||||||
|
|
||||||
|
for(unsigned int year = 1; year < duration; year++) {
|
||||||
|
unsigned int lastYear = year - 1;
|
||||||
|
|
||||||
|
// investment = investment form last year + interest
|
||||||
|
tab[year][1] = tab[lastYear][1] + tab[lastYear][2];
|
||||||
|
|
||||||
|
// interest rate = last interest rate + interest growth
|
||||||
|
tab[year][0] = tab[lastYear][0] + growthRate;
|
||||||
|
|
||||||
|
// interest = investmentAmount * interest rate
|
||||||
|
tab[year][2] = tab[year][1] * tab[year][0] * 1e-2;
|
||||||
|
|
||||||
|
totalInterests += tab[year][2];
|
||||||
|
}
|
||||||
|
|
||||||
|
return totalInterests + tab[0][2];
|
||||||
|
}
|
||||||
|
|
||||||
|
void output_table(double tab[][3], unsigned int rows, unsigned int columns) {
|
||||||
|
printf("\n\tinterest rate [%%] investment [€] annual interest [€]\n");
|
||||||
|
|
||||||
|
unsigned int year = 0;
|
||||||
|
|
||||||
|
for(unsigned int row = 0; row < rows; row++) {
|
||||||
|
year ++;
|
||||||
|
printf("year %2d:% 12.2lf\t% 12.2lf\t% 10.2lf\n", year, tab[row][0], tab[row][1], tab[row][2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int find_index(double arr[], unsigned int len) {
|
||||||
|
unsigned int current = 0;
|
||||||
|
double max = 0;
|
||||||
|
|
||||||
|
for (unsigned int x = 0; x < len; x++) {
|
||||||
|
if (arr[x] > max) {
|
||||||
|
current = x;
|
||||||
|
max = arr[x];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return current;
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
#ifndef _CALCULATOR_H_
|
||||||
|
#define _CALCULATOR_H_
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief fill tab with interest calculations. Each row will contain data for the next year.
|
||||||
|
* the columns will contain [0] := interest rate [1] := investment amount [2] := investment growth
|
||||||
|
*
|
||||||
|
* @param tab 3 column table
|
||||||
|
* @param duration number of years to calculate for
|
||||||
|
* @param investmentAmount start money
|
||||||
|
* @return double sum off total investments
|
||||||
|
*/
|
||||||
|
double calculate_interest_growth(double tab[][3], int duration, double investmentAmount);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief print the table tab as layouted for calculate_interest_growth() to stdout
|
||||||
|
*
|
||||||
|
* @param tab
|
||||||
|
* @param duration number of rows
|
||||||
|
* @param columns number of columns
|
||||||
|
*/
|
||||||
|
void output_table(double tab[][3], unsigned int duration, unsigned int columns);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief find the maximum value in arr
|
||||||
|
*
|
||||||
|
* @param arr
|
||||||
|
* @param len
|
||||||
|
* @return unsigned int index of the maximum value in arr
|
||||||
|
*/
|
||||||
|
unsigned int find_index(double arr[], unsigned int len);
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,74 @@
|
||||||
|
#include "options/options.h"
|
||||||
|
#include "calculator/calculator.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
|
||||||
|
const Student author = new_student("Sven", "Vogel", 1191225);
|
||||||
|
|
||||||
|
unsigned int offerCount;
|
||||||
|
unsigned int years;
|
||||||
|
double investmentAmount;
|
||||||
|
|
||||||
|
char info[100];
|
||||||
|
|
||||||
|
generate_string(info, &author);
|
||||||
|
|
||||||
|
if (argc > 1 && strcmp(argv[1], "--info") == 0) {
|
||||||
|
printf("%s", info);
|
||||||
|
|
||||||
|
if (argc > 2 && strcmp(argv[2], "--export") == 0) {
|
||||||
|
export_to_disk(info);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
|
||||||
|
do {
|
||||||
|
printf("Enter investment duration in years [1; 50]: ");
|
||||||
|
scanf("%u", &years);
|
||||||
|
} while (years > 50 || years < 1);
|
||||||
|
|
||||||
|
do {
|
||||||
|
printf("Enter investment amount in € [100; 10000]: ");
|
||||||
|
scanf("%lf", &investmentAmount);
|
||||||
|
} while (investmentAmount > 10000 || investmentAmount < 100);
|
||||||
|
|
||||||
|
do {
|
||||||
|
printf("Enter offer count [1; 3]: ");
|
||||||
|
scanf("%u", &offerCount);
|
||||||
|
} while (investmentAmount > 10000 || investmentAmount < 100);
|
||||||
|
|
||||||
|
// allocate table
|
||||||
|
double (*tab)[3] = calloc(years, sizeof(double[3]));
|
||||||
|
|
||||||
|
// alternative dynamic stack allocation: double totalInterests[offerCount];
|
||||||
|
// alternative static stack allocation: double totalInterests[50];
|
||||||
|
double *totalInterests = calloc(offerCount, sizeof(double));
|
||||||
|
|
||||||
|
for (int i = 0; i < offerCount; i++) {
|
||||||
|
printf("\n--------------------------------------------------------------\n");
|
||||||
|
|
||||||
|
double totalInterest = calculate_interest_growth(tab, years, investmentAmount);
|
||||||
|
|
||||||
|
totalInterests[i] = totalInterest;
|
||||||
|
|
||||||
|
output_table(tab, years, 3);
|
||||||
|
|
||||||
|
printf("\nTotal interest after 3 years: %.2lf€\n", totalInterest);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int bestOffer = find_index(totalInterests, offerCount);
|
||||||
|
|
||||||
|
printf("\n--------------------------------------------------------------\n");
|
||||||
|
printf("Offer %d has the best total interest with %.2lf€ after %d years\n\n", bestOffer + 1, totalInterests[bestOffer], years);
|
||||||
|
printf("\n--------------------------------------------------------------\n");
|
||||||
|
|
||||||
|
free(totalInterests);
|
||||||
|
free(tab);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
#include "options.h"
|
||||||
|
|
||||||
|
// name of the file to write to on --export
|
||||||
|
const char *FILE_NAME = "info_export.txt";
|
||||||
|
|
||||||
|
Student new_student(const char *name, const char *familyName, uint32_t matriculationNumber) {
|
||||||
|
Student new_stud = {
|
||||||
|
.name = name,
|
||||||
|
.familyName = familyName,
|
||||||
|
.matriculationNumber = matriculationNumber,
|
||||||
|
};
|
||||||
|
|
||||||
|
return new_stud;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief writes the format of student to the file specified by FILE_NAME.
|
||||||
|
*
|
||||||
|
* @param text
|
||||||
|
*/
|
||||||
|
void export_to_disk(const char* text) {
|
||||||
|
|
||||||
|
// open file in write mode only, truncating previous content to zero
|
||||||
|
FILE* handle = fopen(FILE_NAME, "w");
|
||||||
|
|
||||||
|
// file could not be opened
|
||||||
|
if (handle == NULL) {
|
||||||
|
fprintf(stderr, "could not open file to export\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fprintf(handle, "%s", text);
|
||||||
|
|
||||||
|
fclose(handle);
|
||||||
|
|
||||||
|
printf("Author exported to disk. Goodbye!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void generate_string(char *dest, const Student *student) {
|
||||||
|
sprintf(dest, "Name: %s %s, Matrikelnummer: %7d\n", student->name, student->familyName, student->matriculationNumber);
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
#ifndef _OPTIONS_H_
|
||||||
|
#define _OPTIONS_H_
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
|
typedef struct student {
|
||||||
|
const char* name;
|
||||||
|
const char* familyName;
|
||||||
|
// 7-Decimal-Digit UUID number
|
||||||
|
uint32_t matriculationNumber;
|
||||||
|
} Student;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief create a new stack allocated student
|
||||||
|
*
|
||||||
|
* @param name forename of the student
|
||||||
|
* @param familyName
|
||||||
|
* @param matriculationNumber 7-digit unsigned uuid
|
||||||
|
* @return Student
|
||||||
|
*/
|
||||||
|
Student new_student(const char *name, const char *familyName, uint32_t matriculationNumber);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief fill a destination string with the format "Name <NAME>, <FAMILYNAME> Matriekelnummer: <NUMBER>"
|
||||||
|
*
|
||||||
|
* @param dest string to store in, should be 100 bytes in size
|
||||||
|
* @param author student to take inforamtion from
|
||||||
|
*/
|
||||||
|
void generate_string(char *dest, const Student *author);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief export text to a file
|
||||||
|
*
|
||||||
|
* @param text
|
||||||
|
*/
|
||||||
|
void export_to_disk(const char* text);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue