From 19a2def4a9b6f24a9597bb326845c4b8e004fae5 Mon Sep 17 00:00:00 2001 From: koseokkyu Date: Sun, 12 Nov 2017 02:55:38 +0900 Subject: [PATCH 1/3] add heapsort --- Sorts/HeapSort.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Sorts/HeapSort.c diff --git a/Sorts/HeapSort.c b/Sorts/HeapSort.c new file mode 100644 index 0000000000..80ced37ac1 --- /dev/null +++ b/Sorts/HeapSort.c @@ -0,0 +1,67 @@ +#include + +void heapify(int *unsorted, int index, int heap_size); +void heap_sort(int *unsorted, int n); + +int main() { + int n = 0; + int i = 0; + char s[50]; + char oper; + + int* unsorted; + printf("Enter the size of the array you want\n"); + scanf("%d", &n); + unsorted = (int*)malloc(sizeof(int) * n); + while (getchar() != '\n'); + printf("Enter numbers separated by a comma:\n"); + gets(s); + + char *ptr = strtok(s, ","); + while (ptr != NULL && i < n) { + *(unsorted + i) = atoi(ptr); + i++; + ptr = strtok(NULL, ","); + } + heap_sort(unsorted, n); + + printf("["); + printf("%d", *(unsorted)); + for (int i = 1; i < n; i++) { + printf(", %d", *(unsorted + i)); + } + printf("]"); +} + +void heapify(int *unsorted, int index, int heap_size) { + int temp; + int largest = index; + int left_index = 2 * index; + int right_index = 2 * index + 1; + if (left_index < heap_size && *(unsorted + left_index) > *(unsorted + largest)) { + largest = left_index; + } + if (right_index < heap_size && *(unsorted + right_index) > *(unsorted + largest)) { + largest = right_index; + } + + if (largest != index) { + temp = *(unsorted + largest); + *(unsorted + largest) = *(unsorted + index); + *(unsorted + index) = temp; + heapify(unsorted, largest, heap_size); + } +} + +void heap_sort(int *unsorted, int n) { + int temp; + for (int i = n / 2 - 1; i > -1; i--) { + heapify(unsorted, i, n); + } + for (int i = n - 1; i > 0; i--) { + temp = *(unsorted); + *(unsorted) = *(unsorted + i); + *(unsorted + i) = temp; + heapify(unsorted, 0, i); + } +} From 04527dfc5605fe37ee243c499ac83f57a42e49ba Mon Sep 17 00:00:00 2001 From: koseokkyu Date: Mon, 20 Nov 2017 11:50:40 +0900 Subject: [PATCH 2/3] add jumpsearch --- Searches/Jump_Search.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Searches/Jump_Search.c diff --git a/Searches/Jump_Search.c b/Searches/Jump_Search.c new file mode 100644 index 0000000000..0afb8b77e9 --- /dev/null +++ b/Searches/Jump_Search.c @@ -0,0 +1,33 @@ +#include +#include +#define min(X,Y) ((X) < (Y) ? (X) : (Y)) +int jump_search(int* arr, int x); +int n; + +int main() { + int arr[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610 }; + n = sizeof(arr) / sizeof(int); + int x = 55; + int index = jump_search(arr, x); + printf("\nNumber %d is at index %d\n", x, index); +} + +int jump_search(int* arr, int x) { + int step = floor(sqrt(n)); + int prev = 0; + while (*(arr + (min(step, n) - 1)) < x) { + prev = step; + step += floor(sqrt(n)); + if (prev >= n) + return -1; + } + + while (*(arr + prev) < x) { + prev = prev + 1; + if (prev == fmin(step, n)) + return -1; + } + if (*(arr + prev) == x) + return prev; + return -1; +} From bb6582f12bdd55120199946ece9730098556d79d Mon Sep 17 00:00:00 2001 From: koseokkyu Date: Fri, 24 Nov 2017 14:09:36 +0900 Subject: [PATCH 3/3] modify heapsort --- Sorts/HeapSort.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/Sorts/HeapSort.c b/Sorts/HeapSort.c index 80ced37ac1..0010091a3e 100644 --- a/Sorts/HeapSort.c +++ b/Sorts/HeapSort.c @@ -6,7 +6,6 @@ void heap_sort(int *unsorted, int n); int main() { int n = 0; int i = 0; - char s[50]; char oper; int* unsorted; @@ -15,13 +14,9 @@ int main() { unsorted = (int*)malloc(sizeof(int) * n); while (getchar() != '\n'); printf("Enter numbers separated by a comma:\n"); - gets(s); - - char *ptr = strtok(s, ","); - while (ptr != NULL && i < n) { - *(unsorted + i) = atoi(ptr); + while (i != n) { + scanf("%d,", (unsorted + i)); i++; - ptr = strtok(NULL, ","); } heap_sort(unsorted, n);