int main(){ std::cout << std::boolalpha << std::endl; double firDoub{}; double secDoub{2014.0}; std::cout << "isSmaller(firDoub, secDoub): " << isSmaller(firDoub, secDoub) << std::endl; Account firAcc{}; Account secAcc{2014.0}; std::cout << "isSmaller(firAcc, secAcc): " << isSmaller(firAcc, secAcc) << std::endl; std::cout << std::endl; }
/* BEGIN TEMPLATE */ void bubbleSort2() { /* BEGIN SOLUTION */ int i,j; for (i = getValueCount()-1; i>=0; i--) { for (j = 0; j<i; j++) if (!isSmaller(j,j+1)) swap(j,j+1); } /* END SOLUTION */ }
/* BEGIN TEMPLATE */ void bubbleSort3() { /* BEGIN SOLUTION */ int i,j; for (i = getValueCount()-1; i>=0; i--) { int swapped = 0; for (j = 0; j<i; j++) { if (!isSmaller(j,j+1)) { swap(j,j+1); swapped=1; } } if (!swapped) { return; } } /* END SOLUTION */ }
void gcd(BigNumber n1,BigNumber n2) { long b=0,i; while(n1.len && n2.len) { if(n1.v[0]) { if(n2.v[0]) { if(isSmaller(n1,n2)) n2=minus(n2,n1); else n1=minus(n1,n2); } else n2=div2(n2); } else { if(n2.v[0]) n1=div2(n1); else { n1=div2(n1); n2=div2(n2); b++; } } } if(n2.len) for(i=n2.len-1;i>=0;i--) printf("%d",n2.v[i]); else for(i=n1.len-1;i>=0;i--) printf("%d",n1.v[i]); while(b--) printf("0"); printf("\n"); }
Relation * orderByRelationTwoPass(Relation *relation_ptr, vector<string> &field_name, vector<enum FIELD_TYPE> &field_type, bool returnRelation) { Block *block_ptr; // Every 9 blocks of the relation are sorted int sorted_blocks = 9; int num_relation_blocks = relation_ptr->getNumOfBlocks(); int num_groups = (num_relation_blocks - 1) / 9 + 1; int first_min_index; vector<int> blocks_used; Relation *ordered_relation_ptr = NULL; Schema schema; if (returnRelation) { schema = relation_ptr->getSchema(); ordered_relation_ptr = schema_manager.createRelation(relation_ptr->getRelationName() + "_ordered", Schema(schema.getFieldNames(), schema.getFieldTypes())); } // Bring first block of each group in memory for (int i = 0; i < num_groups; i++) { block_ptr = mem.getBlock(i); block_ptr->clear(); relation_ptr->getBlock(i * sorted_blocks, i); blocks_used.push_back(1); } while(1) { Block *min_tuple_block_ptr = NULL; int min_tuple_index = -1; for(int i = 0; i < num_groups; i++) { block_ptr = mem.getBlock(i); vector<Tuple> tuples = block_ptr->getTuples(); // Try to get the first unconsidered tuple from this sublist while ( (first_min_index = getFirstMinTuple(tuples)) == -1) { // Use 1 more block blocks_used[i] += 1; if ( (i == num_groups - 1) && (blocks_used[i] > ((num_relation_blocks - 1) % 9 + 1))) break; if (blocks_used[i] > sorted_blocks) break; // Bring another block from the sublist into the memory relation_ptr->getBlock(i * sorted_blocks + blocks_used[i] - 1, i); tuples = block_ptr->getTuples(); } // This sublist has been exhausted if (first_min_index == -1) continue; if (isSmaller(field_name, field_type, block_ptr->getTuple(first_min_index), min_tuple_block_ptr, min_tuple_index)) { min_tuple_block_ptr = block_ptr; min_tuple_index = first_min_index; } } // If no min tuple found, break if (!min_tuple_block_ptr) break; Tuple min_tuple = min_tuple_block_ptr->getTuple(min_tuple_index); if (!returnRelation) min_tuple.printTuple(); else appendTupleToRelation(ordered_relation_ptr, 9, min_tuple); min_tuple_block_ptr->nullTuple(min_tuple_index); } return ordered_relation_ptr; }