Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#102 pull request heap sort is modified at input process to receive an integer directly. #104

Merged
merged 3 commits into from
Nov 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Searches/Jump_Search.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <stdio.h>
#include <math.h>
#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;
}
62 changes: 62 additions & 0 deletions Sorts/HeapSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>

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 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");
while (i != n) {
scanf("%d,", (unsorted + i));
i++;
}
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);
}
}