├── .gitignore ├── Chapter 10 ├── Makefile ├── list.c ├── list.h ├── list_test.c ├── queue.c ├── queue.h ├── queue_test.c ├── stack.c ├── stack.h └── stack_test.c ├── Chapter 12 ├── Makefile ├── tree.c ├── tree.h └── tree_test.c ├── Chapter 2 ├── Makefile ├── Sort.c ├── Sort.h └── main.c ├── Chapter 4 ├── Makefile ├── matrix.c ├── matrix.h ├── matrix_test.c ├── maxarray.c ├── maxarray.h └── maxarray_test.c ├── Chapter 5 ├── Makefile ├── random.c ├── random.h └── random_test.c ├── Chapter 6 ├── Makefile ├── heap.c ├── heap.h └── heap_test.c ├── Chapter 7 ├── Makefile ├── quicksort.c ├── quicksort.h └── quicksort_test.c ├── Chapter 8 ├── Makefile ├── linear.c ├── linear.h └── linear_test.c ├── Chapter 9 ├── Makefile ├── medians.c ├── medians.h └── medians_test.c ├── LICENSE.txt └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | *.com 2 | *.class 3 | *.dll 4 | *.exe 5 | *.o 6 | *.so 7 | *.save 8 | *.7z 9 | *.dmg 10 | *.gz 11 | *.iso 12 | *.jar 13 | *.rar 14 | *.tar 15 | *.zip 16 | *.log 17 | *.sql 18 | *.sqlite 19 | .DS_Store 20 | .DS_Store? 21 | ._* 22 | .Spotlight-V100 23 | .Trashes 24 | Icon? 25 | ehthumbs.db 26 | Thumbs.db 27 | *.txt 28 | \html 29 | \latex 30 | Doxyfile 31 | \Doxygen 32 | *~ 33 | -------------------------------------------------------------------------------- /Chapter 10/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Werror -std=c99 -pedantic 3 | OBJ_LIST = list.o list_test.o 4 | OBJ_QUEUE = queue.o queue_test.o 5 | OBJ_STACK = stack.o stack_test.o 6 | 7 | all : list queue stack 8 | 9 | list : $(OBJ_LIST) 10 | 11 | list_test.o : list_test.c list.h 12 | $(CC) -c list_test.c $(CFLAGS) 13 | 14 | list.o : list.c list.h 15 | $(CC) -c list.c $(CFLAGS) 16 | 17 | queue : $(OBJ_QUEUE) 18 | 19 | queue_test.o : queue_test.c queue.h 20 | $(CC) -c queue_test.c $(CFLAGS) 21 | 22 | queue.o : queue.c queue.h 23 | $(CC) -c queue.c $(CFLAGS) 24 | 25 | stack : $(OBJ_STACK) 26 | 27 | stack_test.o : stack_test.c stack.h 28 | $(CC) -c stack_test.c $(CFLAGS) 29 | 30 | stack.o : stack.c stack.h 31 | $(CC) -c stack.c $(CFLAGS) 32 | 33 | .PHONY : clean 34 | 35 | clean : 36 | rm list queue stack $(OBJ_LIST) $(OBJ_QUEUE) $(OBJ_STACK) 37 | -------------------------------------------------------------------------------- /Chapter 10/list.c: -------------------------------------------------------------------------------- 1 | #include "list.h" 2 | 3 | Node* ListInit(int key){ 4 | Node* Head = NULL; 5 | if ((Head = malloc(sizeof(Node))) == NULL){ 6 | fprintf(stderr, "Failed to initialize memory\n"); 7 | exit(EXIT_FAILURE); 8 | } 9 | Head->key = key; 10 | Head->next = NULL; 11 | Head->prev = NULL; 12 | return Head; 13 | } 14 | 15 | int NodeInsert(Node** HeadPtr,int key){ 16 | Node* New_Head = NULL; 17 | 18 | if (*HeadPtr == NULL){ // Empty List 19 | fprintf(stderr,"List has not been initialized\n"); 20 | return 0; 21 | } 22 | 23 | if ((New_Head = malloc(sizeof(Node))) == NULL){ // Malloc check 24 | fprintf(stderr, "Failed to initialize memory for new node, %d not inserted\n",key); 25 | return 0; 26 | } 27 | else { // Create new node 28 | New_Head->key = key; 29 | New_Head->prev = NULL; 30 | New_Head->next = *HeadPtr; 31 | (*HeadPtr)->prev = New_Head; 32 | *HeadPtr = New_Head; 33 | return 1; 34 | } 35 | } 36 | 37 | int NodeDelete(Node** HeadPtr,int key){ 38 | 39 | Node* Scan=(*HeadPtr)->next; 40 | 41 | if (*HeadPtr == NULL){ //Checks for empty list 42 | fprintf(stderr,"List has not been initialized\n"); 43 | return 0; 44 | } 45 | if ((*HeadPtr)->key == key){ //HeadPtr must be changed since the head node must be deleted 46 | Scan = *HeadPtr; 47 | *HeadPtr = (*HeadPtr)->next; 48 | (*HeadPtr)->prev = NULL; //First node's ->prev is set to NULL 49 | free(Scan); //Free deleted Head 50 | return 1; 51 | } 52 | 53 | while(Scan != NULL && Scan->key != key){//Scans list 54 | Scan = Scan->next; 55 | } 56 | 57 | if (Scan){ // Key found 58 | (Scan->prev)->next = Scan->next;//Scan->Prev node points to Scan->next 59 | (Scan->next)->prev = Scan->prev;//Scan->next node points to Scan->prev 60 | free(Scan); 61 | return 1; 62 | } 63 | else { 64 | printf("%d not found in list\n",key); 65 | return 0; 66 | } 67 | } 68 | 69 | Node* ListSearch(Node* Head,int key){ 70 | 71 | Node* Scan=Head; 72 | 73 | while (Scan != NULL && Scan->key != key){ 74 | Scan = Scan->next; 75 | } 76 | 77 | if(Scan)//Key found, address returned 78 | return Scan; 79 | else { // Key not found 80 | printf("%d not found in list\n",key); 81 | return 0; 82 | } 83 | } 84 | 85 | 86 | void PrintList(Node* Head){ 87 | 88 | Node* Scan = Head; 89 | 90 | while (Scan != NULL){ 91 | printf("%d ", Scan->key); 92 | Scan = Scan->next; 93 | } 94 | return; 95 | } 96 | 97 | void ListDelete(Node** HeadPtr){ 98 | 99 | Node* Scan = (*HeadPtr)->next; 100 | Node* Temp = NULL; 101 | while(Scan != NULL){ // Free every allocated node 102 | Temp = Scan->next; 103 | free(Scan); 104 | Scan = Temp; 105 | } 106 | return; 107 | } 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | -------------------------------------------------------------------------------- /Chapter 10/list.h: -------------------------------------------------------------------------------- 1 | /** \file list.h 2 | \author Ruggero Dalo 3 | \brief Linked list Algorithms 4 | */ 5 | 6 | /** \fn Node* ListInit(int key) 7 | \brief Initializes list checking for malloc failure 8 | \param key Key to be stored in the head node 9 | \return 10 | - Address of a new Node 11 | - exit(EXIT_FAILURE) if malloc fails 12 | */ 13 | 14 | /** \fn int NodeInsert(Node** HeadPtr,int key) 15 | \brief Add a new node with key value to the head of the list 16 | \param HeadPtr Address of the pointer that stores the head of the list 17 | \param key Key of the new node 18 | \return 19 | - 1 If malloc and insertion is successful 20 | - 0 If malloc fails or list is empty 21 | */ 22 | 23 | /** \fn int NodeDelete(Node** HeadPtr,int key) 24 | \brief Delets node with key value if it is present in the list 25 | \param HeadPtr Address of the pointer that stores the head of the list 26 | \param key Key of the node that has to be deleted. 27 | \return 28 | \arg 1 If free and deletion is successful 29 | \arg 0 If list is empty 30 | */ 31 | 32 | /** \fn Node* ListSearch(Node* Head,int key) 33 | \brief Linear search of the linked list 34 | \param Head Address of the head of the linked list 35 | \param key Searched integer 36 | \return 37 | - Address of the node with key value if it is saved in the list 38 | - 0 if key is not present in the list 39 | */ 40 | 41 | /** \fn void PrintList(Node* Head) 42 | \brief Prints linked list 43 | \param Head Address of the head of the linked list 44 | */ 45 | 46 | /** \fn void ListDelete(Node** HeadPtr) 47 | \brief Free's memory 48 | \param HeadPtr Address of the pointer that stores the head of the list 49 | \note *Headptr is set to NULL (Empty List), use ListInit to create a new list 50 | */ 51 | 52 | #include 53 | #include 54 | 55 | /** \struct Node_struct list.h "Chapter 10/list.h" 56 | \brief Double linked list struct 57 | */ 58 | 59 | typedef struct Node_struct Node; /**< Needed for Node* pointers */ 60 | struct Node_struct 61 | { 62 | int key; /**< Node key */ 63 | Node* next; /**< Pointer to the next node */ 64 | Node* prev; /**< Pointer to the previous node */ 65 | }; 66 | 67 | Node* ListInit(int key); 68 | int NodeInsert(Node** HeadPtr,int key); 69 | int NodeDelete(Node** HeadPtr,int key); 70 | Node* ListSearch(Node* Head,int key); 71 | void PrintList(Node* Head); 72 | void ListDelete(Node** HeadPtr); 73 | 74 | -------------------------------------------------------------------------------- /Chapter 10/list_test.c: -------------------------------------------------------------------------------- 1 | #include "list.h" 2 | #include 3 | 4 | int main(void){ 5 | 6 | Node* List=NULL; 7 | Node* Temp=NULL; 8 | int i=0; 9 | 10 | List = ListInit(10); 11 | for (i=0;i<10;++i){ 12 | NodeInsert(&List,i); 13 | } 14 | 15 | PrintList(List); 16 | 17 | putchar('\n'); 18 | 19 | NodeDelete(&List, 2); 20 | 21 | if ((Temp = ListSearch(List,3))){ 22 | printf("Returned address:%p\n", (void*)Temp); 23 | } 24 | 25 | PrintList(List); 26 | 27 | putchar('\n'); 28 | 29 | ListDelete(&List); 30 | 31 | getchar(); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Chapter 10/queue.c: -------------------------------------------------------------------------------- 1 | #include "queue.h" 2 | 3 | int QueueInit(queue* Q,int size){ 4 | if ((Q->array = malloc(sizeof(int)*size)) != NULL){ 5 | Q->tail = size-1; 6 | Q->head = size-1; 7 | Q->size = size; 8 | Q->elements = 0; 9 | return 0; 10 | } 11 | else { 12 | fprintf(stderr, "Failed to initialize memory\n"); 13 | exit(EXIT_FAILURE); 14 | } 15 | } 16 | 17 | void Enqueue(queue* Q,int x){ 18 | if (Q->elements == Q->size){ // Array is Full 19 | fprintf(stderr,"\nQueue Overflow\n"); 20 | return; 21 | } 22 | else { 23 | Q->array[Q->tail] = x; 24 | Q->elements++; 25 | 26 | if (Q->tail+1 == Q->size){ // Wrap around 27 | Q->tail = 0; 28 | return; 29 | } 30 | else { 31 | Q->tail++; 32 | return; 33 | } 34 | } 35 | } 36 | 37 | int Dequeue(queue* Q){ 38 | int x=0; 39 | if (Q->elements == 0){ // Empty array 40 | fprintf(stderr,"\nQueue Underflow (0 Will be returned)\n"); 41 | return 0; 42 | } 43 | else { 44 | if (Q->head+1 == Q->size){ // Wrap around 45 | x = Q->array[Q->head]; 46 | Q->head = 0; 47 | Q->elements--; 48 | return x; 49 | } 50 | else { 51 | Q->elements--; 52 | return Q->array[Q->head++]; 53 | } 54 | } 55 | } 56 | 57 | void QueueDestroy(queue* Q){ 58 | free(Q->array); 59 | Q->head = Q->tail = Q->elements = 0; 60 | return; 61 | } 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /Chapter 10/queue.h: -------------------------------------------------------------------------------- 1 | /** \file queue.h 2 | \author Ruggero Dalo 3 | \brief queue Algorithms 4 | */ 5 | 6 | /** \fn int QueueInit(queue* Q,int size) 7 | \brief Initializes queue structure 8 | \arg Checks for malloc failure 9 | \param Q Pointer to an already allocated queue struct 10 | \param size Size of new the dynamically allocated array 11 | \return \arg 0 if memory is correctly allocated 12 | \arg exit(EXIT_FAILURE) if malloc fails 13 | */ 14 | 15 | /** \fn void Enqueue(queue* Q,int x) 16 | \brief Adds elements to the tail of queue 17 | \arg Checks for queue overflow and prints queue Overflow to stderr. 18 | \arg If necessary the tail index is wrapped around to the first element of the array. 19 | \param Q Pointer to a queue struct initialized with QueueInit 20 | \param x Integer to be added to the queue 21 | */ 22 | 23 | /** \fn int Dequeue(queue* Q) 24 | \brief Returns element from the head of the queue 25 | \arg Checks for queue underflow and prints queue Underflow to stderr. 26 | \arg If necessary the tail index is wrapped around to the first element of the array. 27 | \param Q Pointer to a queue struct initialized with QueueInit 28 | \return \arg element in array[head] 29 | \arg 0 if an underflow in found 30 | */ 31 | 32 | /** \fn void QueueDestroy(queue* Q); 33 | \brief Free's memory 34 | \param Q Pointer to a queue struct initialized with QueueInit 35 | */ 36 | 37 | #include 38 | #include 39 | 40 | /** \struct queue queue.h "Chapter 10/queue.h" 41 | \brief queue struct 42 | */ 43 | typedef struct 44 | { 45 | int* array; /**< Pointer to dynamically allocted \c int array */ 46 | int tail; /**< Index of the tail of the queue; */ 47 | int head; /**< Index of the head of the queue; */ 48 | int size; /**< Size of the queue */ 49 | int elements; /**< Number of elements inserted in the queue */ 50 | }queue; 51 | 52 | int QueueInit(queue* Q,int size); 53 | void Enqueue(queue* Q,int x); 54 | int Dequeue(queue* Q); 55 | void QueueDestroy(queue* Q); 56 | 57 | 58 | -------------------------------------------------------------------------------- /Chapter 10/queue_test.c: -------------------------------------------------------------------------------- 1 | #include "queue.h" 2 | 3 | #define SIZE 8 4 | 5 | int main(void){ 6 | queue Queue; 7 | int i=0; 8 | 9 | QueueInit(&Queue,SIZE); 10 | 11 | for (i=0;iarray = malloc(sizeof(int)*size)) != NULL){ 5 | S->index = -1; 6 | S->size = size; 7 | return 0; 8 | } 9 | else { 10 | fprintf(stderr, "Failed to initialize memory"); 11 | exit(EXIT_FAILURE); 12 | } 13 | } 14 | 15 | int StackEmpty(stack* S){ 16 | if (S->index < 0) 17 | return 1; 18 | else return 0; 19 | } 20 | 21 | int StackFull(stack* S){ 22 | if (S->index+1 >= S->size) 23 | return 1; 24 | else return 0; 25 | } 26 | 27 | int StackRealloc(stack* S){ 28 | 29 | if ((S->array = realloc(S->array,sizeof(int)*(S->size)*2)) != NULL){ 30 | S->size = (S->size)*2; 31 | return 1; 32 | } 33 | else return 0; 34 | } 35 | 36 | int push(stack* S,int x){ 37 | if (StackFull(S)){ 38 | if (StackRealloc(S)){ 39 | S->index++; 40 | S->array[S->index] = x; 41 | return 0; 42 | } 43 | else{ 44 | fprintf(stderr, "Failed to re-initialize memory"); 45 | exit(EXIT_FAILURE); 46 | } 47 | } 48 | else { 49 | S->index++; 50 | S->array[S->index] = x; 51 | return 0; 52 | } 53 | } 54 | 55 | int pop(stack* S){ 56 | if (StackEmpty(S)){ 57 | fprintf(stderr,"Stack Underflow (0 will be returned)"); 58 | return 0; 59 | } 60 | else { 61 | return S->array[S->index--]; 62 | } 63 | } 64 | 65 | void StackDestroy(stack* S){ 66 | free(S->array); 67 | S->size = 0; 68 | S->index = -1; 69 | return; 70 | } 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /Chapter 10/stack.h: -------------------------------------------------------------------------------- 1 | /** \file stack.h 2 | \author Ruggero Dalo 3 | \brief stack Algorithms 4 | */ 5 | 6 | /** \fn int StackInit(stack* S,int size) 7 | \brief Initializes stack structure 8 | \arg Checks for malloc failure 9 | \param S Pointer to an already allocated stack struct 10 | \param size Size of new the dynamically allocated array 11 | \return \arg 0 if memory is correctly allocated 12 | \arg exit(EXIT_FAILURE) if malloc fails 13 | */ 14 | 15 | /** \fn int StackEmpty(stack* S) 16 | \brief Checks for empty stack 17 | \param S Pointer to a stack struct initialized with stackInit 18 | \return \arg 1 if stack is empty 19 | \arg 0 if stack is not empty 20 | */ 21 | 22 | /** \fn int StackFull(stack* S) 23 | \brief Checks for full stack 24 | \param S Pointer to a stack struct initialized with stackInit 25 | \return \arg 1 if stack is full 26 | \arg 0 if stack is not full 27 | */ 28 | 29 | /** \fn int StackRealloc(stack* S) 30 | \brief Array is realloc'd so that final size = 2*initial size 31 | \param S Pointer to a stack struct initialized with StackInit 32 | \return \arg 0 if memory is correctly allocated 33 | \arg exit(EXIT_FAILURE) if realloc fails 34 | */ 35 | 36 | /** \fn int push(stack* S,int x) 37 | \brief Adds elements to the top of the stack 38 | \arg If stack is full the array is realloced 39 | \param S Pointer to a stack struct initialized with stackInit 40 | \param x Integer to be added to the stack 41 | \return \arg 0 if x is pushed 42 | \arg exit(EXIT_FAILURE) if realloc fails 43 | */ 44 | 45 | /** \fn int pop(stack* S) 46 | \brief Pops last element of the stack 47 | \arg Checks for empty stack and if necessary prints stack Underflow to stderr. 48 | \param S Pointer to a stack struct initialized with stackInit 49 | \return \arg Last element 50 | \arg 0 if stack is empty 51 | */ 52 | 53 | /** \fn void StackDestroy(stack* Q); 54 | \brief Free's memory 55 | \param S Pointer to a stack struct initialized with StackInit 56 | */ 57 | 58 | #include 59 | #include 60 | 61 | /** \struct stack stack.h "Chapter 10/stack.h" 62 | \brief stack struct 63 | */ 64 | 65 | typedef struct 66 | { 67 | int* array; /**< Pointer to dynamically allocted \c int array */ 68 | int index; /**< Index of the top of the stack */ 69 | int size; /**< Size of the stack */ 70 | }stack; 71 | 72 | int StackInit(stack* S,int size); 73 | int StackEmpty(stack* S); 74 | int StackFull(stack* S); 75 | int StackRealloc(stack* S); 76 | int push(stack* S,int x); 77 | int pop(stack* S); 78 | void StackDestroy(stack* S); 79 | 80 | -------------------------------------------------------------------------------- /Chapter 10/stack_test.c: -------------------------------------------------------------------------------- 1 | #include "stack.h" 2 | 3 | #define SIZE 5 4 | 5 | int main(void){ 6 | stack Stack; 7 | int i=0; 8 | 9 | StackInit(&Stack,SIZE); 10 | for (i=0;ikey = key; 10 | Head->right = NULL; 11 | Head->left = NULL; 12 | return Head; 13 | } 14 | 15 | Node* NodeInsert(Node* Head,int key){ 16 | if(Head == NULL){ 17 | return (TreeInit(key)); 18 | } 19 | else { 20 | if (Head->key > key) 21 | Head->left = NodeInsert(Head->left, key); 22 | else 23 | Head->right = NodeInsert(Head->right,key); 24 | return Head; 25 | } 26 | } 27 | 28 | Node* TreeSearch(Node* Head,int key){ 29 | if(Head == NULL) 30 | return 0; 31 | else { 32 | if (Head->key == key) return Head; //Key found 33 | else { 34 | if (Head->key > key) 35 | return TreeSearch(Head->left,key); 36 | else 37 | return TreeSearch(Head->right,key); 38 | } 39 | } 40 | } 41 | 42 | void InorderPrintTree(Node* Head){ 43 | if (Head == NULL) return; 44 | InorderPrintTree(Head->left); 45 | printf("%d ",Head->key); 46 | InorderPrintTree(Head->right); 47 | return; 48 | } 49 | 50 | void PostorderPrintTree(Node* Head){ 51 | if (Head == NULL) return; 52 | PostorderPrintTree(Head->left); 53 | PostorderPrintTree(Head->right); 54 | printf("%d ", Head->key); 55 | return; 56 | } 57 | Node* TreeMinimum(Node* Head){ 58 | while(Head->left != NULL){ 59 | Head = Head->left; 60 | } 61 | return Head; 62 | } 63 | 64 | Node* TreeMaximum(Node* Head){ 65 | while(Head->right != NULL){ 66 | Head = Head->right; 67 | } 68 | return Head; 69 | } 70 | 71 | void TreeDelete_Recur(Node* Head){ 72 | if(Head == NULL) return; 73 | TreeDelete_Recur(Head->left); 74 | TreeDelete_Recur(Head->right); 75 | free(Head->left); 76 | free(Head->right); 77 | return; 78 | } 79 | 80 | void TreeDelete(Node* Head){ 81 | TreeDelete_Recur(Head); 82 | free(Head); 83 | return; 84 | } 85 | 86 | int TreeSize(Node* Head){ 87 | if (Head == NULL) return 0; 88 | else return 1 + TreeSize(Head->left) + TreeSize(Head->right); 89 | } 90 | 91 | int MaxDepth(Node* Head){ 92 | if (Head == NULL) return 0; 93 | 94 | if (MaxDepth(Head->left) >= MaxDepth(Head->right)) 95 | return MaxDepth(Head->left) + 1; 96 | else return (MaxDepth(Head->right) + 1); 97 | } 98 | -------------------------------------------------------------------------------- /Chapter 12/tree.h: -------------------------------------------------------------------------------- 1 | /** \file tree.h 2 | \author Ruggero Dalo 3 | \brief Binary tree Algorithms 4 | */ 5 | 6 | /** \fn Node* TreeInit(int key); 7 | \brief Initializes tree checking for malloc failure 8 | \param key Key to be stored in the head node 9 | \return 10 | \arg Address of a new Node 11 | \arg exit(EXIT_FAILURE) if malloc fails 12 | */ 13 | 14 | /** \fn Node* NodeInsert(Node* Head,int key); 15 | \brief Add a new leef with key value 16 | \param Head Head of the tree 17 | \param key Key of the new node 18 | \return 19 | \arg Head of the tree 20 | */ 21 | 22 | /** \fn Node* TreeSearch(Node* Head,int key); 23 | \brief Searches tree for leed with key stored 24 | \param Head Head of the tree 25 | \param key Key that has to be searched. 26 | \return 27 | \arg Address of the searched node if found 28 | \arg 0 if the key isn't stored in the tree 29 | */ 30 | 31 | /** \fn void InorderPrintTree(Node* Head); 32 | \brief Prints tree in order (lower to higher values) 33 | \param Head Head of the tree 34 | */ 35 | 36 | /** \fn Node* TreeMinimum(Node* Head); 37 | \brief Returns the address of the node with minimum key 38 | \param Head Address of the head of the linked tree 39 | \return 40 | \arg Address of the node with the smallest key 41 | */ 42 | 43 | /** \fn Node* TreeMaximum(Node* Head); 44 | \brief Returns the address of the node with maximum key 45 | \param Head Address of the head of the linked tree 46 | \return 47 | \arg Address of the node with the largest key 48 | */ 49 | 50 | /** \fn void TreeDelete_Recur(Node* Head) 51 | \brief Free's memory 52 | \param Head Address of the pointer that stores the head of the tree 53 | \note If Head is NULL (Empty Tree), use TreeInit to create a new tree 54 | */ 55 | 56 | /** \fn void TreeDelete(Node* Head) 57 | \brief Wrapper function that also free's the head of the tree 58 | \param Head Address of the pointer that stores the head of the tree 59 | \note If Head is NULL (Empty Tree), use TreeInit to create a new tree 60 | */ 61 | 62 | #include 63 | #include 64 | 65 | /** \struct Node_struct tree.h "Chapter 12/tree.h" 66 | \brief Binary search tree struct 67 | */ 68 | 69 | typedef struct Tree_struct Node; /**< Needed for Node* pointers */ 70 | struct Tree_struct 71 | { 72 | int key; /**< Node key */ 73 | Node* left; /**< Pointer to the left node */ 74 | Node* right; /**< Pointer to the right node */ 75 | }; 76 | 77 | Node* TreeInit(int key); 78 | Node* NodeInsert(Node* Head,int key); 79 | Node* TreeSearch(Node* Head,int key); 80 | void InorderPrintTree(Node* Head); 81 | void PostorderPrintTree(Node* Head); 82 | Node* TreeMinimum(Node* Head); 83 | Node* TreeMaximum(Node* Head); 84 | void TreeDelete_Recur(Node* Head); 85 | void TreeDelete(Node* Head); 86 | int TreeSize(Node* Head); 87 | int MaxDepth(Node* Head); 88 | -------------------------------------------------------------------------------- /Chapter 12/tree_test.c: -------------------------------------------------------------------------------- 1 | #include "tree.h" 2 | #include 3 | 4 | int main(void){ 5 | 6 | int i=0, array[]={34,12,65,23,2,100,120}; 7 | Node* root = NULL; 8 | root = TreeInit(49); 9 | 10 | for (i=0;i<7;++i){ 11 | NodeInsert(root,array[i]); 12 | } 13 | 14 | InorderPrintTree(root); 15 | 16 | putchar('\n'); 17 | 18 | PostorderPrintTree(root); 19 | 20 | printf("\nAddress of node with key=13 is %p\n",(void *)TreeSearch(root,13)); 21 | 22 | printf("Minimum : %d \n", (TreeMinimum(root))->key); 23 | 24 | printf("Maximum : %d \n", (TreeMaximum(root))->key); 25 | 26 | printf("Treesize : %d \n", TreeSize(root)); 27 | 28 | printf("Max depth : %d \n", MaxDepth(root)); 29 | 30 | TreeDelete(root); 31 | 32 | getchar(); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /Chapter 2/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Werror -std=c99 -pedantic 3 | OBJ = Sort.o main.o 4 | 5 | Sort : $(OBJ) 6 | 7 | Sort.o : Sort.c Sort.h 8 | $(CC) -c Sort.c $(CFLAGS) 9 | 10 | main.o : main.c Sort.h 11 | $(CC) -c main.c $(CFLAGS) 12 | 13 | .PHONY : clean 14 | 15 | clean : 16 | rm Sort $(OBJ) 17 | -------------------------------------------------------------------------------- /Chapter 2/Sort.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 2/Sort.c -------------------------------------------------------------------------------- /Chapter 2/Sort.h: -------------------------------------------------------------------------------- 1 | /** \file Sort.h 2 | \author Ruggero Dalo 3 | \brief Sorting Algorithms 4 | */ 5 | 6 | /** \fn void insertion_sort(int * array,int size) 7 | \brief Insertion Sort 8 | \param array Array to be sorted 9 | \param size Elements in the array 10 | */ 11 | 12 | /** \fn void reverse_insertion_sort(int * array,int size) 13 | \brief Insertion Sort in decreasing order 14 | \param array Array to be sorted 15 | \param size Elements in the array 16 | */ 17 | 18 | /** \fn void selection_sort(int * array, int size) 19 | \brief Selection Sort 20 | \param array Array to be sorted 21 | \param size Elements in the array 22 | */ 23 | 24 | /** \fn void merge(int *array, int l,int m,int h) 25 | \brief Merges and sorts 2 subarrays 26 | \param array Array to be sorted 27 | \param l Low/Left index 28 | \param m Medium/Middle index 29 | \param h High/Right index 30 | */ 31 | 32 | /** \fn merge_sort(int *array,int l, int h) 33 | \brief Merge Sort 34 | \param array Array to be sorted 35 | \param l Low/Left index 36 | \param h High/Right index 37 | */ 38 | 39 | 40 | #ifndef SORT_H_INCLUDED 41 | #define SORT_H_INCLUDED 42 | 43 | void insertion_sort(int * array,int size); 44 | void reverse_insertion_sort(int * array,int size); 45 | void selection_sort(int * array, int size); 46 | void merge(int *array, int l,int m,int h); 47 | void merge_sort(int *array,int l, int h); 48 | 49 | #endif /* SORT_H_INCLUDED */ 50 | -------------------------------------------------------------------------------- /Chapter 2/main.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 2/main.c -------------------------------------------------------------------------------- /Chapter 4/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Werror -std=c99 -pedantic 3 | OBJ_MATRIX = matrix.o matrix_test.o 4 | OBJ_MAXARRAY = maxarray.o maxarray_test.o 5 | 6 | all : matrix maxarray 7 | 8 | matrix : $(OBJ_MATRIX) 9 | 10 | matrix_test.o : matrix_test.c matrix.h 11 | $(CC) -c matrix_test.c $(CFLAGS) 12 | 13 | matrix.o : matrix.c matrix.h 14 | $(CC) -c matrix.c $(CFLAGS) 15 | 16 | maxarray : $(OBJ_MAXARRAY) 17 | 18 | maxarray_test.o : maxarray_test.c maxarray.h 19 | $(CC) -c maxarray_test.c $(CFLAGS) 20 | 21 | maxarray.o : maxarray.c maxarray.h 22 | $(CC) -c maxarray.c $(CFLAGS) 23 | 24 | 25 | .PHONY : clean 26 | 27 | clean : 28 | rm matrix maxarray $(OBJ_MATRIX) $(OBJ_MAXARRAY) 29 | -------------------------------------------------------------------------------- /Chapter 4/matrix.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 4/matrix.c -------------------------------------------------------------------------------- /Chapter 4/matrix.h: -------------------------------------------------------------------------------- 1 | /** \file matrix.h 2 | \author Ruggero Dalo 3 | \brief Matrix Multiplication 4 | */ 5 | 6 | /** \fn void square_matrix_multiply(int *A,int *B,int *C,int size) 7 | \brief Square matrix multiplication 8 | \param A First Matrix 9 | \param B Second Matrix 10 | \param C Multiplication result matrix 11 | \param size Number of colomns/rows of matrix A and B 12 | */ 13 | 14 | 15 | #ifndef MATRIX_H_INCLUDED 16 | #define MATRIX_H_INCLUDED 17 | 18 | void square_matrix_multiply(int *A,int *B,int *C,int size); 19 | 20 | #endif // MATRIX_H_INCLUDED 21 | -------------------------------------------------------------------------------- /Chapter 4/matrix_test.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 4/matrix_test.c -------------------------------------------------------------------------------- /Chapter 4/maxarray.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 4/maxarray.c -------------------------------------------------------------------------------- /Chapter 4/maxarray.h: -------------------------------------------------------------------------------- 1 | /** \file maxarray.h 2 | \author Ruggero Dalo 3 | \brief Maximum subarray Algorithms 4 | */ 5 | 6 | /** \fn Max * max_crossing_sub(int *array, int l,int h) 7 | \brief Finds maximum sum of elements crossing the (l+h)/2 index 8 | \param array Integer Array 9 | \param l Low/Left index 10 | \param h High/Right index 11 | \return Pointer to a Max struct containing index and sum of the maximum subarray found in array[l]-array[h] 12 | \warning The Returned memory address must be free'd to avoid memory leaks 13 | */ 14 | 15 | /** \fn Max * max_subarray(int *array,int l, int h) 16 | \brief Recursive funtion 17 | \param array Integer Array 18 | \param l Low/Left index 19 | \param h High/Right index 20 | \return Pointer to a Max struct containing index and sum of the maximum subarray found in array[l]-array[h] 21 | \warning The Returned memory address must be free'd to avoid memory leaks 22 | */ 23 | 24 | /** \fn void max_subarray_quadratic(int *array,int size,Max *result) 25 | \brief Brute force solution to the max sub array problem 26 | \param array Integer Array 27 | \param size Elements in the array 28 | \param *result Pointer to an already allocated structure where the result is stored 29 | */ 30 | 31 | /** \fn void max_subarray_linear(int *array,int size,Max *result); 32 | \brief Kadane's algorithm 33 | \param array Integer Array 34 | \param size Elements in the array 35 | \param *result Pointer to an already allocated structure where the result is stored 36 | */ 37 | 38 | #ifndef MAXARRAY_H_INCLUDED 39 | #define MAXARRAY_H_INCLUDED 40 | 41 | #include 42 | #include 43 | 44 | /** \struct Max maxarray.h "Chapter 4/maxarray.h" 45 | \brief Struct containing 2 indexs and the sum of array[maxlf] to array[maxrt] 46 | */ 47 | 48 | typedef struct{ 49 | int maxlf; /**< Left index of the maximum subarray */ 50 | int maxrt; /**< Right index of the maximum subarray */ 51 | int maxsum; /**< Sum of the elements from \c array[maxlf] to \c array[maxrt] */ 52 | }Max; 53 | 54 | Max * max_crossing_sub(int *array, int l,int h); 55 | Max * max_subarray(int *array,int l, int h); 56 | void max_subarray_quadratic(int *array,int size,Max *result); 57 | void max_subarray_linear(int *array,int size,Max *result); 58 | 59 | #endif // MAXARRAY_H_INCLUDED 60 | -------------------------------------------------------------------------------- /Chapter 4/maxarray_test.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 4/maxarray_test.c -------------------------------------------------------------------------------- /Chapter 5/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Werror -std=c99 -pedantic 3 | OBJ = random.o random_test.o 4 | 5 | random : $(OBJ) 6 | 7 | random_test.o : random_test.c random.h 8 | $(CC) -c random_test.c $(CFLAGS) 9 | 10 | random.o : random.c random.h 11 | $(CC) -c random.c $(CFLAGS) 12 | 13 | .PHONY : clean 14 | 15 | clean : 16 | rm random $(OBJ) 17 | -------------------------------------------------------------------------------- /Chapter 5/random.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 5/random.c -------------------------------------------------------------------------------- /Chapter 5/random.h: -------------------------------------------------------------------------------- 1 | /** \file random.h 2 | \author Ruggero Dalo 3 | \brief Randomize Array 4 | */ 5 | 6 | /** \fn void randomized_hire_assistant (int *array, int size) 7 | A new array (B) will be initialized with random values. 8 | B[i] = key, array[i] = value. 9 | B is then sorted and array is modified accordingly. 10 | \param array input array 11 | \param size Elements in the array 12 | */ 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | #ifndef RANDOM_H_INCLUDED 19 | #define RANDOM_H_INCLUDED 20 | 21 | void randomized_hire_assistant (int *array, int size); 22 | 23 | #endif /* RANDOM_H_INCLUDED */ 24 | -------------------------------------------------------------------------------- /Chapter 5/random_test.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 5/random_test.c -------------------------------------------------------------------------------- /Chapter 6/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Werror -std=c99 -pedantic 3 | OBJ = heap.o heap_test.o 4 | 5 | heap : $(OBJ) 6 | 7 | heap_test.o : heap_test.c heap.h 8 | $(CC) -c heap_test.c $(CFLAGS) 9 | 10 | heap.o : heap.c heap.h 11 | $(CC) -c heap.c $(CFLAGS) 12 | 13 | .PHONY : clean 14 | 15 | clean : 16 | rm heap $(OBJ) 17 | -------------------------------------------------------------------------------- /Chapter 6/heap.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 6/heap.c -------------------------------------------------------------------------------- /Chapter 6/heap.h: -------------------------------------------------------------------------------- 1 | /** \file heap.h 2 | \author Ruggero Dalo 3 | \brief Heap/Heapsort related algorithms 4 | */ 5 | 6 | /** \fn void max_heapify (int *array,int heap_size,int index) 7 | Checks parent and children nodes and the biggest element is saved in the parent node. Recursively calls it self on the exchanged children. 8 | \param array Array to be Heapifyed 9 | \param heap_size Heap size 10 | \param index Position in the array 11 | */ 12 | 13 | /** \fn void min_heapify (int *array,int heap_size,int index) 14 | Checks parent and children nodes and the smallest element is saved in the parent node. Recursively calls it self on the exchanged children. 15 | \param array Array to be Heapifyed 16 | \param heap_size Heap size 17 | \param index Position in the array 18 | */ 19 | 20 | /** \fn void build_max_heap(int *array,int size) 21 | Starting at the last parent node it heapifyes the array 22 | \param array Array to be Heapifyed 23 | \param size Elements in the array 24 | */ 25 | 26 | /** \fn void heapsort (int *array,int size) 27 | Heapsort 28 | \param array Array to be Heapifyed 29 | \param size Elements in the array 30 | */ 31 | 32 | #ifndef HEAP_H_INCLUDED 33 | #define HEAP_H_INCLUDED 34 | 35 | #include 36 | 37 | void max_heapify (int *array,int heap_size,int index); 38 | void min_heapify (int *array, int heap_size,int index); 39 | void build_max_heap(int *array,int size); 40 | void heapsort (int *array,int size); 41 | 42 | #endif /* HEAP_H_INCLUDED*/ 43 | -------------------------------------------------------------------------------- /Chapter 6/heap_test.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 6/heap_test.c -------------------------------------------------------------------------------- /Chapter 7/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Werror -std=c99 -pedantic 3 | OBJ = quicksort.o quicksort_test.o 4 | 5 | quicksort : $(OBJ) 6 | 7 | quicksort_test.o : quicksort_test.c quicksort.h 8 | $(CC) -c quicksort_test.c $(CFLAGS) 9 | 10 | quicksort.o : quicksort.c quicksort.h 11 | $(CC) -c quicksort.c $(CFLAGS) 12 | 13 | .PHONY : clean 14 | 15 | clean : 16 | rm quicksort $(OBJ) 17 | -------------------------------------------------------------------------------- /Chapter 7/quicksort.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 7/quicksort.c -------------------------------------------------------------------------------- /Chapter 7/quicksort.h: -------------------------------------------------------------------------------- 1 | /** \file quicksort.h 2 | \author Ruggero Dalo 3 | \brief Quicksort Algorithms 4 | */ 5 | 6 | /** \fn int partition(int *array, int l,int h) 7 | \brief Partions the array around the pivot 8 | \param array Array to be sorted 9 | \param l Low/Left index 10 | \param h High/Right index 11 | \return Returns index of the pivot 12 | */ 13 | 14 | /** \fn void quicksort_algo(int *array,int l,int h) 15 | \brief Quicksort 16 | \param array Array to be sorted 17 | \param l Low/Left index 18 | \param h High/Right index 19 | \note Using the wrapper funtion, quicksort, is recommended. 20 | */ 21 | 22 | /** \fn void quicksort(int *array,int size) 23 | \brief Quicksort wrapper funtion 24 | \param array Array to be sorted 25 | \param size Elements in the array 26 | */ 27 | 28 | /** \fn int randomized_partition(int *array,int l,int h) 29 | \brief Partions the array around a random pivot 30 | \param array Array to be sorted 31 | \param l Low/Left index 32 | \param h High/Right index 33 | \return Returns index of the pivot 34 | */ 35 | 36 | /** \fn void randomized_quicksort_algo(int *array,int l,int h) 37 | \brief Randomized Quicksort 38 | \param array Array to be sorted 39 | \param l Low/Left index 40 | \param h High/Right index 41 | \note Using the wrapper funtion, randomized_quicksort, is recommended. 42 | */ 43 | 44 | /** \fn void randomized_quicksort(int *array,int size) 45 | \brief Randomized Quicksort wrapper funtion 46 | \param array Array to be sorted 47 | \param size Elements in the array 48 | */ 49 | 50 | #ifndef QUICKSORT_H_INCLUDED 51 | #define QUICKSORT_H_INCLUDED 52 | 53 | #include 54 | #include 55 | #include 56 | 57 | int partition(int *array, int l,int h); 58 | void quicksort_algo(int *array,int l,int h); 59 | void quicksort(int *array,int size); 60 | int randomized_partition(int *array,int l,int h); 61 | void randomized_quicksort_algo(int *array,int l,int h); 62 | void randomized_quicksort(int *array,int size); 63 | 64 | #endif /* QUICKSORT_H_INCLUDED*/ 65 | -------------------------------------------------------------------------------- /Chapter 7/quicksort_test.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 7/quicksort_test.c -------------------------------------------------------------------------------- /Chapter 8/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | CFLAGS=-Wall -Wextra -Werror -std=c99 -pedantic 3 | OBJ = linear.o linear_test.o 4 | 5 | linear : $(OBJ) 6 | 7 | linear_test.o : linear_test.c linear.h 8 | $(CC) -c linear_test.c $(CFLAGS) 9 | 10 | linear.o : linear.c linear.h 11 | $(CC) -c linear.c $(CFLAGS) 12 | 13 | .PHONY : clean 14 | 15 | clean : 16 | rm linear $(OBJ) 17 | -------------------------------------------------------------------------------- /Chapter 8/linear.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 8/linear.c -------------------------------------------------------------------------------- /Chapter 8/linear.h: -------------------------------------------------------------------------------- 1 | /** \file linear.h 2 | \author Ruggero Dalo 3 | \brief Sorting in linear time algorithms 4 | */ 5 | 6 | /** \fn int findMaxForCountingSort(int* array,int size) 7 | Finds maximum integer in the array 8 | \param array Array to be scanned 9 | \param size Elements in the array 10 | \return Maximum element 11 | */ 12 | 13 | /** \fn void countingSort(int* array,int* sorted,int size,int max) 14 | Sorts tha array with help from an auxiliary array of size equal to the maximum element in array 15 | \param array Array to be sorted 16 | \param sorted Pointer to an already initialized array of size equal to the one to be sorted 17 | \param size Elements in the array 18 | \param max Maximum element in the array 19 | */ 20 | 21 | #ifndef LINEAR_H_INCLUDED 22 | #define LINEAR_H_INCLUDED 23 | 24 | #include 25 | 26 | int findMaxForCountingSort(int* array,int size); 27 | void countingSort(int* array,int* sorted,int size,int max); 28 | 29 | #endif /* LINEAR_H_INCLUDED*/ 30 | -------------------------------------------------------------------------------- /Chapter 8/linear_test.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 8/linear_test.c -------------------------------------------------------------------------------- /Chapter 9/Makefile: -------------------------------------------------------------------------------- 1 | INCLUDE = -I "../Chapter 7/" 2 | CC=gcc 3 | CFLAGS=-Wall -Wextra -Werror -std=c99 -pedantic 4 | OBJ = medians_test.o medians.o quicksort.o 5 | QSPATH = ../Chapter\ 7/ 6 | 7 | medians : $(OBJ) 8 | 9 | medians_test.o : medians_test.c medians.h 10 | $(CC) -c medians_test.c $(FLAGS) 11 | 12 | medians.o : medians.c medians.h $(QSPATH)quicksort.h 13 | $(CC) -c medians.c $(FLAGS) $(INCLUDE) 14 | 15 | quicksort.o :$(QSPATH)quicksort.c $(QSPATH)quicksort.h 16 | $(CC) -c $(QSPATH)quicksort.c $(FLAGS) $(INCLUDE) 17 | 18 | .PHONY : clean 19 | 20 | clean : 21 | rm medians *.o 22 | -------------------------------------------------------------------------------- /Chapter 9/medians.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 9/medians.c -------------------------------------------------------------------------------- /Chapter 9/medians.h: -------------------------------------------------------------------------------- 1 | /** \file medians.h 2 | \author Ruggero Dalo 3 | \brief Select element in array according to rank in linear time 4 | */ 5 | 6 | /** \fn int selectf_algo(int* array,int l,int h,int i) 7 | Uses partion algorithm from quicksort 8 | \param array Array to be scanned 9 | \param l Low/Left index 10 | \param h High/Right index 11 | \param i Desired rank 12 | \return Integer of rank i 13 | \note Using the wrapper funtion, selectf, is recommended. 14 | */ 15 | /** \fn int selectf(int* array,int size,int rank) 16 | Rank 1 = smallest element in the array\n 17 | Rank Size = biggest element in the array\n 18 | Rank < 1 or Rank > size is not defined. 19 | \param array Array to be scanned 20 | \param size Elements in the array 21 | \param rank Desired rank 22 | \return Integer with the specified rank 23 | */ 24 | 25 | /** \fn int randomized_selectf_algo(int* array,int l,int h,int i) 26 | Uses randomized_partion algorithm from quicksort 27 | \param array Array to be scanned 28 | \param l Low/Left index 29 | \param h High/Right index 30 | \param i Desired rank 31 | \return Integer of rank i 32 | \note Using the wrapper funtion, randomized_selectf, is recommended. 33 | */ 34 | /** \fn int randomized_selectf(int* array,int size,int rank) 35 | Rank 1 = smallest element in the array\n 36 | Rank Size = biggest element in the array\n 37 | Rank < 1 or Rank > size is not defined. 38 | \param array Array to be scanned 39 | \param size Elements in the array 40 | \param rank Desired rank 41 | \return Integer with the specified rank 42 | */ 43 | 44 | 45 | 46 | 47 | #ifndef MEDIANS_H_INCLUDED 48 | #define MEDIANS_H_INCLUDED 49 | 50 | int selectf_algo(int* array,int l,int h,int i); 51 | int selectf(int* array,int size,int rank); 52 | int randomized_selectf_algo(int* array,int l,int h,int i); 53 | int randomized_selectf(int* array,int size,int rank); 54 | 55 | #endif /* MEDIANS_H_INCLUDED*/ 56 | -------------------------------------------------------------------------------- /Chapter 9/medians_test.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arguggi/Introduction_To_Algorithms/cdfd496262acb77818952d0cf36fde59e3632c20/Chapter 9/medians_test.c -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (C) 2013 Ruggero D'Alò 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a 4 | copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included 12 | in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction to Algorithms # 2 | The Algorithms are all written in C. 3 | 4 | Compilation with these flags is error/warning free: 5 | > -Wall -Wextra -Werror -std=c99 -pedantic 6 | 7 | ## Algorithms not yet implemented ## 8 | + Chapter 4 9 | + Strassen multiplication 10 | + Non square matrix multiplication 11 | 12 | + Chapter 6: 13 | + Priority queue 14 | 15 | + Chapter 7: 16 | + Hoare Partition 17 | 18 | + Chapter 8: 19 | + Radix sort 20 | + Bucket sort 21 | 22 | + Chapter 12: 23 | + Delete tree node 24 | 25 | ## Chapter Contents ## 26 | + Chapter 2: 27 | + Selection Sort 28 | + Insertion Sort 29 | + Merge Sort 30 | + Chapter 4: 31 | + Square Matrix Multiplication 32 | + Max Subarray 33 | + Chapter 5: 34 | + Randomize Array 35 | + Chapter 6: 36 | + Heapsort 37 | + Chapter 7: 38 | + Quicksort 39 | + Chapter 8: 40 | + Counting Sort 41 | + Chapter 9: 42 | + Rank select 43 | + Chapter 10: 44 | + Double linked list 45 | + Queue structure 46 | + Stack structure 47 | + Chapter 12: 48 | + Tree structure 49 | 50 | ## Extra ## 51 | 52 | + Doxygen documentation *.h 53 | + Makefile in every chapter 54 | --------------------------------------------------------------------------------