/** * calculates the median between two nodes * uses the iterative quick select method */ struct kd_node_t* kdtree::find_median(struct kd_node_t *& start, struct kd_node_t * end, int idx) { if (end <= start) return NULL; if (end == start + 1) return start; struct kd_node_t *p, *store, *md = start + (end - start) / 2; double pivot; while (1) { pivot = md->x[idx]; struct kd_node_t * prev = end - 1; swap_node(md, prev); for (store = p = start; p < end; p++) { if (p->x[idx] < pivot) { if (p != store) swap_node(p, store); store++; } } swap_node(store, end - 1); /* median has duplicate values */ if (store->x[idx] == md->x[idx]) return md; if (store > md) end = store; else start = store; } }
int32_t h_heap_push(h_heap_st *hp, void *data) { uint32_t curr_index; if (hp->nbase >= hp->__size_base) { if (hp->__fixed) return -1; hp->__size_base = hp->__size_base ? hp->__size_base * 2 : DEFAULT_HEAP_SIZE; hp->bases = h_realloc(hp->bases, hp->__size_base * sizeof(void *)); if (!hp->bases) { return -1; } } curr_index = hp->nbase++; hp->bases[curr_index] = data; while (curr_index != 0) { uint32_t parent_index = (curr_index - 1) >> 1; if (hp->__cmp(hp->bases[curr_index], hp->bases[parent_index]) >= 0) break; swap_node(hp->bases[parent_index], hp->bases[curr_index]); curr_index = parent_index; } return 0; }
void Function::make_child(Function::node*& a, Function::node*& b) { swap_node(a,b); b->child[0] = a->child[0]; b->child[1] = a->child[1]; a->child[0] = b; a->child[1] = 0; }
int heap_propagate_up(binary_heap *a, int node_indx) { int i = node_indx, k = PARENT(i); while(i > 1 && compare_priority(&a->elements[i], &a->elements[k]) >= 0) { swap_node(a->elements, i, k); //a->elements[i] = a->elements[PARENT(i)]; i = PARENT(i); k = PARENT(i); } return i; }
void AdaptiveHuffman::insert(char sym) { Node* tba = NULL; if (!is_known(sym)) { Node* inner = new Node("", 1); Node* fresh = new Node(string(1, sym), 1); inner->set_left(nyt); inner->set_right(fresh); inner->set_parent(nyt->get_parent()); if (nyt->get_parent() != NULL) nyt->get_parent()->set_left(inner); else root = inner; nyt->set_parent(inner); fresh->set_parent(inner); nodes.insert(nodes.begin() + 1, inner); nodes.insert(nodes.begin() + 1, fresh); knownsym.push_back(sym); tba = inner->get_parent(); } else { tba = find_node(sym); } while (tba != NULL) { Node* bignode = find_bignode(tba->get_frequency()); if (tba != bignode && tba->get_parent() != bignode && bignode->get_parent() != tba) swap_node(tba, bignode); tba->set_frequency(tba->get_frequency() + 1); tba = tba->get_parent(); } }
static Node *delete_node(ScmTreeCore *tc, Node *n) { while (n->left && n->right) { /* N has both children. We swap N and its previous node. It would be easier just to swap key and value, but it could lead to a hard-to-track bug if a pointer to N is retained in somewhere (e.g. iterator). So we actually swap the node. */ swap_node(tc, n, prev_node(n)); } /* we have at most one child */ if (n->left) { delete_node1(tc, n, n->left); } else { delete_node1(tc, n, n->right); /* this covers no child case */ } clear_node(n); return n; }
ListNode* reverseKGroup(ListNode* head, int k){ ListNode *beg = head; ListNode *end = head; ListNode *last = NULL; ListNode *real_head = NULL; ListNode *next_beg; while(1) { int i=1; for(i=1; i<k && end; ++i) { end=end->next; } if(end){ next_beg = end->next; swap_node(beg, end); if(last) { last->next = end; } if(!real_head){ real_head = end; } last = beg; } else { if(last) { last->next = beg; } break ; } beg=next_beg; end=beg; } if(!real_head){ real_head = beg; } return real_head; };
void heapify(binary_heap *a, int i) { register int l, r, largest = i; l = LEFT(i); r = RIGHT(i); /* check the left child */ if((l <= a->heap_size && (compare_priority(&a->elements[l], &a->elements[i]) > 0))) largest = l; /* check the right child */ if(r <= a->heap_size && (compare_priority(&a->elements[r], &a->elements[largest]) > 0)) largest = r; if(largest != i) { /* swap nodes largest and i, then heapify */ swap_node(a->elements, i, largest); heapify(a, largest); } }
int32_t h_heap_pop(h_heap_st *hp, void **ret) { uint32_t curr_index; if (hp->nbase == 0) return -1; if (ret) *ret = hp->bases[0]; --hp->nbase; hp->bases[0] = hp->bases[hp->nbase]; curr_index = 0; while (curr_index < hp->nbase) { uint32_t c1 = (curr_index << 1) + 1; uint32_t c2 = (curr_index << 1) + 2; uint32_t child_index; if (c1 >= hp->nbase) break; if (c2 >= hp->nbase) { child_index = c1; } else { child_index = hp->__cmp(hp->bases[c1], hp->bases[c2]) < 0 ? c1 : c2; } if (hp->__cmp(hp->bases[curr_index], hp->bases[child_index]) < 0) { break; } swap_node(hp->bases[curr_index], hp->bases[child_index]); curr_index = child_index; } return 0; }
//排序 void sort_list(list L) { int i,rule,flag; int len=len_list(L); printf("输入两个数字选择排序方式和正倒序,两个数字用空格隔开\n1-学号, 2-姓名, 3-数学, 4-语文, 5-英语 ,6-总分\n1-正序, 2-倒序\n"); scanf("%d %d",&rule,&flag); rst_stdin(); L=L->next; //冒泡排序 for(i=len-1; i>0; i--) { list m=L; int j=0; for(; j<i; j++,m=m->next) { char *tmp1,*tmp2; switch(rule) { case 1: tmp1=m->num; tmp2=m->next->num; break; case 2: tmp1=m->name; tmp2=m->next->name; break; case 3: tmp1=m->Math; tmp2=m->next->Math; break; case 4: tmp1=m->Chinese; tmp2=m->next->Chinese; break; case 5: tmp1=m->English; tmp2=m->next->English; break; case 6: { char a_s[40],b_s[40]; double a=(atof(m->Math)+atof(m->Chinese)+atof(m->English)); double b=(atof(m->next->Math)+atof(m->next->Chinese)+atof(m->next->English)); sprintf(a_s,"%f",a); sprintf(b_s,"%f",b); tmp1=a_s; tmp2=b_s; } } if(rule==3||rule==4||rule==5||rule==6) { if((flag==1 && atof(tmp1)>atof(tmp2)) || (flag==2 && atof(tmp1)<atof(tmp2))) { swap_node(m,m->next); } } else { if((flag==1 && strcmp(tmp1,tmp2)>0) || (flag==2 && strcmp(tmp1,tmp2)<0)) { swap_node(m,m->next); } } } } }