Merge commit '9d51b08a816693281b2890671e9b5fdcbded5b12'

* commit '9d51b08a816693281b2890671e9b5fdcbded5b12':
  Add return value in deque()
  Remove the white space
  Typo in variable name
  Add the return value in create _heap()
  updating DIRECTORY.md
  Fix #509
  Increased spead of Cocktail Sort
  Add new sorting algorithm (Cocktail Sort)
  Changed function name
  Add new sorting algorithm
  updating DIRECTORY.md
  dynamic array data structure
  Add syntax highlight
  index now starts from 1

# Conflicts:
#	client_server/client.c
#	sorting/Bubble_Sort_2.c
This commit is contained in:
Krishna Vedala 2020-04-22 08:19:16 -04:00
commit 18c79b203e
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7
14 changed files with 333 additions and 55 deletions

View File

@ -29,6 +29,9 @@
* Dictionary * Dictionary
* [Dict](https://github.com/TheAlgorithms/C/blob/master/data_structures/dictionary/dict.c) * [Dict](https://github.com/TheAlgorithms/C/blob/master/data_structures/dictionary/dict.c)
* [Test Program](https://github.com/TheAlgorithms/C/blob/master/data_structures/dictionary/test_program.c) * [Test Program](https://github.com/TheAlgorithms/C/blob/master/data_structures/dictionary/test_program.c)
* Dynamic Array
* [Dynamic Array](https://github.com/TheAlgorithms/C/blob/master/data_structures/dynamic_array/dynamic_array.c)
* [Main](https://github.com/TheAlgorithms/C/blob/master/data_structures/dynamic_array/main.c)
* Graphs * Graphs
* [Bellman-Ford](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/Bellman-Ford.c) * [Bellman-Ford](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/Bellman-Ford.c)
* [Bfs](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/BFS.c) * [Bfs](https://github.com/TheAlgorithms/C/blob/master/data_structures/graphs/BFS.c)
@ -285,6 +288,7 @@
* [Bubble Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Bubble_Sort.c) * [Bubble Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Bubble_Sort.c)
* [Bubble Sort 2](https://github.com/TheAlgorithms/C/blob/master/sorting/Bubble_Sort_2.c) * [Bubble Sort 2](https://github.com/TheAlgorithms/C/blob/master/sorting/Bubble_Sort_2.c)
* [Bucket Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Bucket_Sort.c) * [Bucket Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Bucket_Sort.c)
* [Cocktail Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Cocktail_Sort.c)
* [Comb Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/comb_sort.c) * [Comb Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/comb_sort.c)
* [Counting Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/counting_Sort.c) * [Counting Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/counting_Sort.c)
* [Cycle Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Cycle_Sort.c) * [Cycle Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Cycle_Sort.c)
@ -295,6 +299,7 @@
* [Multikey Quick Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/multikey_quick_sort.c) * [Multikey Quick Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/multikey_quick_sort.c)
* [Pancake Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Pancake_Sort.c) * [Pancake Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Pancake_Sort.c)
* [Partition Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/partition_Sort.c) * [Partition Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/partition_Sort.c)
* [Pigeonhole Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Pigeonhole_Sort.c)
* [Quick Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Quick_Sort.c) * [Quick Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/Quick_Sort.c)
* [Radix Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/radix_sort.c) * [Radix Sort](https://github.com/TheAlgorithms/C/blob/master/sorting/radix_sort.c)
* [Radix Sort 2](https://github.com/TheAlgorithms/C/blob/master/sorting/radix_sort_2.c) * [Radix Sort 2](https://github.com/TheAlgorithms/C/blob/master/sorting/radix_sort_2.c)

View File

@ -1,8 +1,7 @@
// Write CPP code here
// Write CPP code here #include <netdb.h>
#include <netdb.h> #include <stdio.h>
#include <stdio.h> #include <stdlib.h>
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <sys/socket.h> #include <sys/socket.h>

View File

@ -1,4 +1,3 @@
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <stdlib.h> #include <stdlib.h>

View File

@ -0,0 +1,13 @@
CC = gcc
CFLAGS = -g -Wall
all: main
main: main.o dynamic_array.o
$(CC) $(CFLAGS) $^ -o $@
dynamic_array.o: dynamic_array.c
$(CC) $(CFLAGS) -c $^
clean:
rm *.o main

View File

@ -0,0 +1,79 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "dynamic_array.h"
dynamic_array_t *init_dynamic_array()
{
dynamic_array_t *da = malloc(sizeof(dynamic_array_t));
da->items = calloc(DEFAULT_CAPACITY, sizeof(void *));
da->capacity = DEFAULT_CAPACITY;
return da;
}
void *add(dynamic_array_t *da, const void *value)
{
if (da->size >= da->capacity) {
void **newItems = realloc(da->items, (da->capacity <<= 1) * sizeof(void **));
free(da->items);
da->items = newItems;
}
void *copy_value = retrive_copy_of_value(value);
da->items[da->size++] = copy_value;
return copy_value;
}
void *put(dynamic_array_t *da, const void *value, const unsigned index)
{
if (!contains(da->size, index))
return INDEX_OUT_OF_BOUNDS;
free(da->items[index]);
void *copy_value = retrive_copy_of_value(value);
da->items[index] = copy_value;
return copy_value;
}
void *get(dynamic_array_t *da, const unsigned index)
{
if (!contains(da->size, index))
return INDEX_OUT_OF_BOUNDS;
return da->items[index];
}
void delete (dynamic_array_t *da, const unsigned index)
{
if (!contains(da->size, index))
return;
for (unsigned i = index; i < da->size; i++) {
da->items[i] = da->items[i + 1];
}
da->size--;
free(da->items[da->size]);
}
unsigned contains(const unsigned size, const unsigned index)
{
if (size >= 0 && index < size)
return 1;
printf("index [%d] out of bounds!\n", index);
return 0;
}
void *retrive_copy_of_value(const void *value)
{
void *value_copy = malloc(sizeof(void *));
memcpy(value_copy, value, sizeof(void *));
return value_copy;
}

View File

@ -0,0 +1,26 @@
#ifndef __DYNAMIC_ARRAY__
#define __DYNAMIC_ARRAY__
#define DEFAULT_CAPACITY 1 << 4
#define INDEX_OUT_OF_BOUNDS NULL
typedef struct dynamic_array {
void **items;
unsigned size;
unsigned capacity;
} dynamic_array_t;
extern dynamic_array_t *init_dynamic_array();
extern void *add(dynamic_array_t *da, const void *value);
extern void *put(dynamic_array_t *da, const void *value, unsigned index);
extern void *get(dynamic_array_t *da, const unsigned index);
extern void delete (dynamic_array_t *da, const unsigned index);
unsigned contains(const unsigned size, const unsigned index);
extern void *retrive_copy_of_value(const void *value);
#endif

View File

@ -0,0 +1,33 @@
#include "dynamic_array.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
dynamic_array_t *da = init_dynamic_array();
for (int i = 1; i <= 50; i++) {
add(da, &i);
}
delete (da, 10);
int value = 1000;
put(da, &value, 0);
value = 5000;
int another_value = 7000;
add(da, &another_value);
for (int i = 0; i < da->size; i++) {
printf("value %d\n", *(int *)get(da, i));
}
int value_for_invalid_index = 10000;
put(da, &value_for_invalid_index, 150);
return 0;
}

View File

@ -46,6 +46,7 @@ Heap* create_heap(Heap* heap){
heap->size = 1; heap->size = 1;
heap->p = (int *)malloc(heap->size*sizeof(int)); heap->p = (int *)malloc(heap->size*sizeof(int));
heap->count = 0; heap->count = 0;
return heap;
} }
void down_heapify(Heap* heap, int index){ void down_heapify(Heap* heap, int index){

View File

@ -46,6 +46,7 @@ Heap* create_heap(Heap* heap){
heap->size = 1; heap->size = 1;
heap->p = (int *)malloc(heap->size*sizeof(int)); heap->p = (int *)malloc(heap->size*sizeof(int));
heap->count = 0; heap->count = 0;
return heap;
} }
void down_heapify(Heap* heap, int index){ void down_heapify(Heap* heap, int index){

View File

@ -66,7 +66,7 @@ void enque(int x) {
* Takes the next item from the Queue. * Takes the next item from the Queue.
*/ */
int deque() { int deque() {
int returnData; int returnData = 0;
if(head == NULL) { if(head == NULL) {
printf("ERROR: Deque from empty queue.\n"); printf("ERROR: Deque from empty queue.\n");
exit(1); exit(1);
@ -78,6 +78,7 @@ int deque() {
head = head->pre; head = head->pre;
head->next = NULL; head->next = NULL;
} }
return returnData;
} }
/** /**
@ -85,41 +86,4 @@ int deque() {
*/ */
int size() { int size() {
return count; return count;
} }

View File

@ -13,25 +13,35 @@ You need to only import the **stack.h**
### Public interface ### Public interface
``` void initStack(); ``` ```c
void initStack();
```
Initializes the stack with a capacity of 10 elements. Initializes the stack with a capacity of 10 elements.
``` void push(void * object); ``` ```c
void push(void * object);
```
pushs the argument onto the stack pushs the argument onto the stack
``` void * pop(); ``` ```c
void * pop();
```
pop: pops the top element of the stack from the stack. pop: pops the top element of the stack from the stack.
assumes: stack not empty. assumes: stack not empty.
``` int size(); ``` ```c
int size();
```
gets the number of elements of the stack. gets the number of elements of the stack.
``` int isEmpty(); ``` ```c
int isEmpty();
```
returns 1 if stack is empty otherwise 0. returns 1 if stack is empty otherwise 0.

75
sorting/Cocktail_Sort.c Normal file
View File

@ -0,0 +1,75 @@
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
void cocktailSort(int arr[], int size)
{
int i, changed = TRUE, temp, start = 0, end = size - 1;
while (changed)
{
changed = FALSE;
for (i=start; i<end; i++)
{
if (arr[i] > arr[i+1])
{
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
changed = TRUE;
}
}
end--;
if (changed == FALSE)
{
break;
}
changed = FALSE;
for (i=end-1; i>=start; i--)
{
if (arr[i+1] < arr[i])
{
temp = arr[i+1];
arr[i+1] = arr[i];
arr[i] = temp;
changed = TRUE;
}
}
start++;
}
}
int main()
{
int i, n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int* arr = (int*)malloc(sizeof(int) * n);
for (i = 0; i < n; i++)
{
printf("Number #%d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("You entered: ");
for (i=0; i<n; i++)
{
printf("%d ", arr[i]);
}
cocktailSort(arr, n);
printf("\nSorted array: ");
for (i=0; i<n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
return 0;
}

73
sorting/Pigeonhole_Sort.c Normal file
View File

@ -0,0 +1,73 @@
#include <stdio.h>
#include <stdlib.h>
void pigeonholeSort(int arr[], int size)
{
int i, j, min = arr[0], max = arr[0], range;
// Getting range of the array using max and min
for (i=1; i<size; i++)
{
if (arr[i] < min)
min = arr[i];
if (arr[i] > max)
max = arr[i];
}
range = max - min + 1;
// Make 'holes' and put array's numbers in holes
int * holes = (int*)malloc(sizeof(int) * range);
for (i=0; i<range; i++)
{
holes[i] = 0;
}
for (i=0; i<size; i++)
{
holes[arr[i] - min]++;
}
// Copy the numbers back to the original array
j=0;
for (i=0; i<range; i++)
{
while (holes[i] > 0)
{
arr[j] = i + min;
holes[i]--;
j++;
}
}
free(holes);
}
int main()
{
int i, n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int * arr = (int*)malloc(sizeof(int) * n);
for (i = 0; i < n; i++)
{
printf("Number #%d: ", i + 1);
scanf("%d", &arr[i]);
}
printf("You entered: ");
for (i=0; i<n; i++)
{
printf("%d ", arr[i]);
}
pigeonholeSort(arr, n);
printf("\nSorted array: ");
for (i=0; i<n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
return 0;
}

View File

@ -6,17 +6,18 @@ void sort(int *numbers, int size)
int pos = 0; int pos = 0;
while (pos < size) while (pos < size)
{ {
if (numbers[pos] >= numbers[pos-1]) if (pos == 0)
pos = 1;
if (numbers[pos] >= numbers[pos-1] || pos == 0)
pos++; pos++;
else else
{ {
int tmp = numbers[pos-1]; int tmp = numbers[pos-1];
numbers[pos-1] = numbers[pos]; numbers[pos-1] = numbers[pos];
numbers[pos] = tmp; numbers[pos] = tmp;
pos--; pos--;
if (pos == 0)
pos = 1;
} }
} }
} }
@ -45,4 +46,3 @@ int main()
free(numbers); free(numbers);
return 0; return 0;
} }