Skip to content

Instantly share code, notes, and snippets.

@shkesar
Created October 12, 2016 18:45
Show Gist options
  • Save shkesar/0f8c2ac74c13535a7d0fd061d5795f9d to your computer and use it in GitHub Desktop.
Save shkesar/0f8c2ac74c13535a7d0fd061d5795f9d to your computer and use it in GitHub Desktop.
// GLEW
#define GLEW_STATIC
#include <GL/glew.h>
// GLFW#include <stdio.h>
#include <stdlib.h>
#define HEAPSIZE 1000
struct heap_t {
int *S;
size_t heapsize;
size_t length;
};
int left(int index) {
return index * 2 + 1;
}
int right(int index) {
return index * 2 + 2;
}
int parent(int index) {
return index / 2;
}
struct heap_t *create_heap(size_t size) {
struct heap_t *heap = malloc(sizeof(struct heap_t));
heap->S = malloc(sizeof(int) * size);
heap->length = size-1;
return heap;
}
void max_heapify(struct heap_t *heap, int index) {
int largest = index;
int left_idx = left(index);
int right_idx = right(index);
if ((heap->S[index] < heap->S[left_idx]) && (left_idx <= heap->heapsize))
largest = left_idx;
if ((heap->S[index] < heap->S[right_idx]) && (right_idx <= heap->heapsize))
largest = right_idx;
if (largest != index) {
int temp = heap->S[index];
heap->S[index] = heap->S[largest];
heap->S[largest] = temp;
max_heapify(heap, largest);
}
}
void build_max_heap(struct heap_t *heap) {
heap->heapsize = heap->length;
for (int i = (heap->heapsize / 2); i >= 0; i--)
max_heapify(heap, i);
}
void heap_sort(struct heap_t *heap) {
build_max_heap(heap);
for (int i = heap->length; i > 0; i--) {
int temp = heap->S[0];
heap->S[0] = heap->S[i];
heap->S[i] = temp;
heap->heapsize -= 1;
max_heapify(heap, 0);
}
}
int main(int argc, char **argv) {
int size;
printf("Enter size: ");
scanf("%d", &size);
struct heap_t *heap = create_heap(size);
for (int i = 0; i <= heap->length; i++)
scanf("%d", &(heap->S[i]));
heap_sort(heap);
for (int i = 0; i <= heap->length; i++)
printf("%d ", heap->S[i]);
return 0;
}
#include <GLFW/glfw3.h>
#include <stdio.h>
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_ANY_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
printf("Failed to create GLFW window\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
printf("Failed to initialize GLEW\n");
return -1;
}
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
while(!glfwWindowShouldClose(window))
{
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment