コード例 #1
0
ファイル: dsort.c プロジェクト: jnjcc/yaseek
int *dhsorti(darray *pvd, int *psze, int (*cmpfn)(const void *, const void *)) {
    darray_t *pda = (darray_t *)pvd; 
    if (pda->elem_num_ <= 0) {
        return NULL;
    }

    int *idxs = (int *)malloc(sizeof(int) * pda->elem_num_);
    *psze = 0;
    if (idxs == NULL) {
        return NULL;
    }
    int i;
    for (i = 0; i < pda->elem_num_; ++i) {
        idxs[i] = i;
    }

    int sze = pda->elem_num_;
    for (i = (sze / 2); i >= 0; --i) {
        sift_down(pda, idxs, i, sze-1, cmpfn);
    }
    for (i = sze - 1; i >= 1; --i) {
        int temp = idxs[0];
        idxs[0] = idxs[i];
        idxs[i] = temp;
        sift_down(pda, idxs, 0, i-1, cmpfn);
    }

    *psze = pda->elem_num_;
    return idxs;
}
コード例 #2
0
ファイル: heap_sort.c プロジェクト: forsaken1/ice
void heap_sort(int *a, int count)
{
    int start, end;

    for (start = (count - 2) / 2; start >= 0; start--) {
        sift_down(a, start, count);
    }

    for (end = count - 1; end > 0; end--) {
        SWAP(a[end], a[0]);
        sift_down(a, 0, end);
    }
}
コード例 #3
0
ファイル: heapsort.c プロジェクト: prsdp/metodos-ordenacao
void heap_sort(int array[], int size) {

    int start, end;

    for (start = (size-2)/2; start >= 0; start--) {
        sift_down(array, start, size);
    }

    for (end = size-1; end > 0; end--) {
        heap_swap(&array[end], &array[0]);
        sift_down(array, 0, end);
    }

}
コード例 #4
0
ファイル: submission.c プロジェクト: gypsydave5/c-algorithms
static void sift_down(thread array[], unsigned long long size,
                      unsigned long long index) {
  unsigned long long max_index, left, right;
  thread *left_t, *right_t, *index_t;
  left = left_child(index);
  right = right_child(index);
  left_t = &array[left - 1];
  right_t = &array[right - 1];
  index_t = &array[index - 1];
  max_index = index;

  if (right <= size && t_less_than(right_t, index_t)) {
    max_index = right;
  }

  if (left <= size && t_less_than(left_t, index_t)) {
    max_index = left;
  }

  if (left <= size && right <= size) {
    if (t_less_than(left_t, index_t) && t_less_than(left_t, right_t)) {
      max_index = left;
    }
    if (t_less_than(right_t, index_t) && t_less_than(right_t, left_t)) {
      max_index = right;
    }
  }

  if (index != max_index) {
    swap(&array[index - 1], &array[max_index - 1]);
    sift_down(array, size, max_index);
  }
}
コード例 #5
0
ファイル: heap.c プロジェクト: ajrisi/algorithms
void *heap_extract(heap *h)
{
  void *minitem;

  /* check to make sure we got a heap */
  if(h == NULL) {
    /* we were not given a heap? */
    return NULL;
  }

  /* check to make sure that the heap isnt empty */
  if(heap_isempty(h)) {
    return NULL;
  }

  /* grab the pointer of the item to return */
  minitem = h->heap_items[1];

  h->next_free_idx--;

  /* swap the last item in the heap into the first items location */
  h->heap_items[1] = h->heap_items[h->next_free_idx];
  h->heap_items[h->next_free_idx] = NULL;
    
  /* now, sift down the item at the top of the heap */
  sift_down(h, 1);

  return minitem;
}
コード例 #6
0
ファイル: heap.c プロジェクト: NaokiEto/CS11_CPlusPlus
/* Returns the first (i.e. smallest) value in the heap. */
float get_first_value(float_heap *pHeap)
{
  float result;

  assert(pHeap != NULL);

  /* There needs to be at least one value left in the heap! */
  assert(pHeap->num_values > 0);

  /* Smallest value is at the root - index 0. */
  result = pHeap->values[0];

  /* Decrease the count of how many values are in the heap.
   * NOTE that if there was more than one value in the heap,
   * the last value is still at values[num_values], so we have
   * to move it to the new vacancy that opened up at the root.
   * If there was only one value in the heap when "get-first"
   * was called, we're done.
   */
  pHeap->num_values--;
  if (pHeap->num_values != 0)
  {
    /* Move the last value in the heap to the root. */
    pHeap->values[0] = pHeap->values[pHeap->num_values];

    /* Sift down the new value to position it properly
     * in the heap.
     */
    sift_down(pHeap, 0);
  }

  return result;
}
コード例 #7
0
ファイル: eheap.c プロジェクト: MicBosi/GTS
/**
 * gts_eheap_remove_top:
 * @heap: a #GtsEHeap.
 * @key: a pointer on a gdouble or %NULL.
 *
 * Removes the element at the top of the heap and optionally (if @key is not
 * %NULL) returns the value of its key.
 *
 * Returns: the element at the top of the heap.
 */
gpointer gts_eheap_remove_top (GtsEHeap * heap, gdouble * key)
{
  gpointer root;
  GPtrArray * elts;
  guint len;
  GtsEHeapPair * pair;

  g_return_val_if_fail (heap != NULL, NULL);

  elts = heap->elts; 
  len = elts->len;

  if (len == 0)
    return NULL;
  if (len == 1) {
    pair = g_ptr_array_remove_index (elts, 0);
    root = pair->data;
    if (key) 
      *key = pair->key;
    g_free (pair);
    return root;
  }

  pair = elts->pdata[0];
  root = pair->data;
  if (key) 
    *key = pair->key;
  g_free (pair);
  pair = g_ptr_array_remove_index (elts, len - 1);
  elts->pdata[0] = pair;
  pair->pos = 1;
  sift_down (heap, 1);
  return root;
}
コード例 #8
0
int main()
{
    char command;
    int b;

    while( scanf("%c", &command))
    {
        if(command == 'Q') break;

        if(command == 'A')
        {
            scanf("%d", &b);
            sift_up(b);
        }

        if(command == 'R')
        {
            if(n == 0) printf("Not available\n");
            else {printf("%d\n", sift_down());}
        }

        if(command == 'L')
        {
            if(n == 0) printf("Not available");
            else {printf("%d\n", h[0]);}
        }
    }

    return 0;
}
コード例 #9
0
ファイル: heapsort.c プロジェクト: LinusMelb/C
void
build_heap(data_t A[], int n) {
	int i;
	for (i=n/2-1; i>=0; i--) {
		sift_down(A, i, n);
	}
}
コード例 #10
0
ファイル: heap_sort.c プロジェクト: thodde/heap_sort
void build_max_heap(int* arr, int len) {
	heapsize = len;
	int i;
	for(i = len/2; i >= 0; i--) {
		// invariant: arr[k], i < k <= n are roots of proper heaps
		sift_down(arr, i);
	}
}
コード例 #11
0
ファイル: heap.cpp プロジェクト: mftb/mc458_lab01
// builds the heap bottom-up using sift_down
void heapify(int* vector,int count){
    int start;
    // start is the index of the last parent node
    start=(count-2)/2;
    // creates the heap
    while(start>=0)
        sift_down(vector,start--,count-1);
}
コード例 #12
0
ファイル: heap_sort.hpp プロジェクト: xykivo/percipio
 void heapify(T* array, const int count) {
   int end = count - 1;
   int start = heap_parent(end);
   while (start >= 0) {
     sift_down(array, start, end);
     --start;
   }
 }  // end of heapify
コード例 #13
0
void heap_sort(buf *buf, int low, int high) {
	repair_heap(buf, low, high);
	
	for (int i = high; i > low; i--) {
		swap(buf, i, low);
		sift_down(buf, low, i-1, low);
	}
}
コード例 #14
0
ファイル: usort.c プロジェクト: jbuonagurio/lrose-core
void 
usort( double *array, int size )
{
  double temp;
  int i;

  for ( i=(size/2); i>=0; --i )
    sift_down(array, i, size-1);

  for ( i=size-1; i>=1; --i )
    {
      temp = array[0];
      array[0] = array[i];
      array[i] = temp;
      sift_down(array, 0, i-1);
    }
}
コード例 #15
0
ファイル: main.cpp プロジェクト: charlesq/Euthenia
/* a heap sort algorithm implementation */
void heap_sort(value_type *a, size_t s, compare c)
{
      heapify(a, s, c);// max heap 
      for (size_t i = s -1; i != 0; i --)
      {
          std::swap(a[0], a[i]);
          sift_down(a, i, c, 0);
      }
}
コード例 #16
0
ファイル: heapsort.c プロジェクト: LinusMelb/C
void
heap_sort(data_t A[], int n) {
	int active;
	build_heap(A, n);
	for (active=n-1; active>0; active--) {
		swap_data(A+0, A+active);
		sift_down(A, 0, active);
	}
}
コード例 #17
0
ファイル: binaryheap.c プロジェクト: amulsul/postgres
/*
 * binaryheap_build
 *
 * Assembles a valid heap in O(n) from the nodes added by
 * binaryheap_add_unordered(). Not needed otherwise.
 */
void
binaryheap_build(binaryheap *heap)
{
	int			i;

	for (i = parent_offset(heap->bh_size - 1); i >= 0; i--)
		sift_down(heap, i);
	heap->bh_has_heap_property = true;
}
コード例 #18
0
ファイル: heap.c プロジェクト: ajrisi/algorithms
static void sift_down(heap *h, int insert_index)
{
  void *insert_item;
  int child1_index;
  int child2_index;
  int minchild_index;
  void *minchild_item;

  if(h == NULL) {
    /* no heap */
    return;
  }

  if(insert_index >= h->next_free_idx-1) {
    /* index out of range, or last in heap, so no sift necessary */
    return;
  }

  insert_item = h->heap_items[insert_index];

  /* initialize the child indexes */
  child1_index = insert_index << 1;
  child2_index = child1_index + 1;

  if(child1_index < h->next_free_idx) {
    /* there is a child to compare and possibly swap with */
    if(child2_index < h->next_free_idx) {
      /* find the smallest, if it is less than insert item, then swap */
      if(h->orderitem_fn(h->heap_items[child1_index],
			 h->heap_items[child2_index])) {
	/* child 1 preferred at the top of the heap */
	minchild_index = child1_index;
      } else {
	minchild_index = child2_index;
      }
    } else {
      /* only child1 is valid, so that is possible location */
      minchild_index = child1_index;
    }
  } else {
    /* there were no children to try swapping with, so we are done */
    return;
  }

  minchild_item = h->heap_items[minchild_index];

  /* is the target child better than insert_item? */
  if(h->orderitem_fn(minchild_item, insert_item)) {
    /* the child was less than the parent, so swap the two */
    h->heap_items[insert_index] = minchild_item;
    h->heap_items[minchild_index] = insert_item;
    insert_index = minchild_index;
    /* if we did some work, try to sift down again */
    sift_down(h, insert_index);
  }

}
コード例 #19
0
ファイル: introsort.c プロジェクト: Sergio0694/List_T
// Trasnforms a vector into a heap structure, in-place
static void heapify(T* vector, int n, comparation(*expression)(T, T))
{
	int start = (n - 2) / 2;
	while (start >= 0)
	{
		sift_down(vector, start, n - 1, expression);
		start--;
	}
}
コード例 #20
0
ファイル: submission.c プロジェクト: gypsydave5/c-algorithms
void pp_heap(thread t[], unsigned long long t_count, unsigned long long jobs[],
             unsigned long long j_count, thread log[]) {
  unsigned long long i;
  for (i = 0; i < j_count; i++) {
    log[i].finish_time = t[0].finish_time;
    log[i].index = t[0].index;
    t[0].finish_time += jobs[i];
    sift_down(t, t_count, 1);
  }
}
コード例 #21
0
ファイル: heap_sort.hpp プロジェクト: xykivo/percipio
 void operator()(T* first, T* last) {
   T* array = first;
   int end = heap_index(first, last);
   heapify(array, end + 1);
   while (0 < end) {
     std::swap(array[end], array[0]);
     --end;
     sift_down(array, 0, end);
   }
 }  // end of operator()
コード例 #22
0
ファイル: heap.c プロジェクト: Ivoz/C-Sorting
// Build a max-heap with the root at 0
void heapify(int arr[], int len)
{
	// Start from the first parent with a child
	int start = len / 2 - 1;
	// Sift each element down, if it is not in heap order
	for (; start >= 0; start--)
	{
		sift_down(arr, start, len);
	}
}
コード例 #23
0
ファイル: binaryheap.c プロジェクト: amulsul/postgres
/*
 * binaryheap_replace_first
 *
 * Replace the topmost element of a non-empty heap, preserving the heap
 * property.  O(1) in the best case, or O(log n) if it must fall back to
 * sifting the new node down.
 */
void
binaryheap_replace_first(binaryheap *heap, Datum d)
{
	Assert(!binaryheap_empty(heap) && heap->bh_has_heap_property);

	heap->bh_nodes[0] = d;

	if (heap->bh_size > 1)
		sift_down(heap, 0);
}
コード例 #24
0
void heapify(DArray *array, DArray_compare cmp)
{
  int last_index = array->end - 1;
  int start = (last_index - 1) / 2;

  while (start >= 0) {
    sift_down(array, start, last_index, cmp);
    start--;
  }
}
コード例 #25
0
ファイル: heap.c プロジェクト: Ivoz/C-Sorting
 /*
  * Heap sort is an adaption of selection sort, but using a max-heap
  * to select and place the max element of the unsorted array, 
  * rather than naively searching the array for it. First the array
  * is 'heapified', so that the max element resides at the root of the
  * heap. It is then swapped to the end, and the array sifted to restore
  * its heap property, so the next largest element can be swapped out.
  * It is an O(n log(n)) in-place algorithm, and can be used for 
  * smaller sections of quick sort to avoid its pivot selection problem.
  */
void heap_sort(int arr[], int len)
{
	heapify(arr, len);
	for (len--; len > 0; len--)
	{
		// swap the max value to the end
		swap(arr, arr + len); 
		// sift the new root into heap order
		sift_down(arr, 0, len); 
	}
}
コード例 #26
0
ファイル: main.cpp プロジェクト: charlesq/Euthenia
/* key procedure in heap sort to maintain heap property for the subtree at node i in the tree
   with s size */
void sift_down(value_type *a, size_t s, compare c, size_t i)
{
    size_t k = 2 * i + 1, j = k + 1;
    if (k >= s)
       return;
    if (j >= s)
    {
        if (c(a[i], a[k]))
        {
            std::swap(a[i], a[k]);
            sift_down(a, s, c, k);
        }
        return;
    }
    k = c(a[j], a[k])? k: j; 
    if (c(a[i], a[k]))
    {
        std::swap(a[i], a[k]);
        sift_down(a, s, c, k);
    }
}
コード例 #27
0
ファイル: heap.c プロジェクト: hitchiker42/my-code
//works by modifying memory, but returns a value for convience
binary_heap *heap_sort(binary_heap *heap) {
    if(heap->len <= 1) {
        return heap;
    }
    int end = heap->len-1;
    heapify(heap);
    do {
        SWAP(heap->heap[end], heap->heap[0]);
        sift_down(heap, 0, end);
    } while(--end > 0);
    return heap;
}
コード例 #28
0
ファイル: heap.c プロジェクト: deedeethan/Practice
/*
 * heap_rem_elem - Removes an arbitrary element in the heap by finding
 *                 its index with linear search and then swapping
 *                 and deleting with the bottom-most, right-most element.
 */
void heap_rem_elem(heap H, elem x)
{
  REQUIRES(is_heap(H) && !heap_empty(H));
  int idx = find_elem(H, x);
  H->next--;

  if (H->next > 1) {
    H->data[idx] = H->data[H->next];
    sift_down(H, idx);
  }

  ENSURES(is_heap(H));
}
コード例 #29
0
ファイル: saucy.cpp プロジェクト: KULeuven-KRR/IDP
static void
heap_sort(int *a, int n)
{
	int i;
	for (i = 1; i < n; ++i) {
		sift_up(a-1, i+1);
	}
	--i;
	while (i > 0) {
		swap(a, 0, i);
		sift_down(a-1, i--);
	}
}
コード例 #30
0
int DArray_heapsort(DArray *array, DArray_compare cmp)
{
  heapify(array, cmp);

  int end = array->end - 1;
  while (end > 0) {
    swap(array, end, 0);
    end--;
    sift_down(array, 0, end, cmp);
  }

  return 0;
}