void SelectionSort(int *a, int n) { for(int i = 0; i < n; ++i) { int k = findSmallest(a, i, n); Swap(a, i, k); } }
void LeastCorrect::repopulateIndices(const vector<Flashcard> & cards) { currLowest = findSmallest(cards); for (usInt i = 0; i < cards.size(); i++) { if (cards[i].data.getNumCorrect() == currLowest) leastCorrectIndices.push_back(i); } }
// Driver program to test above function int main() { int val; int arr1[] = {1, 3, 4, 5}; int n1 = sizeof(arr1)/sizeof(arr1[0]); val = findSmallest(arr1, n1); printf("%d",val); return 0; }
// Find the best choice in the searching tree. Node* findBest(Node *root, int depth) { int i; Node* best = NULL; Node* cur = NULL; switch (depth) { case 1: best = findGreatest(root); break; case 2: cur = findGreatest(root); best = findSmallest(cur); break; case 3: cur = findGreatest(root); best = findSmallest(cur); best = findGreatest(best); break; } return best; }
Node * buildHuffmanTree(Node *nodes[]) { int i; for(i=0; i<LETTERS_COUNT-1; i++) { int indexOfSmallest = findSmallest(nodes, -1); int indexOfSecondSmallest = findSmallest(nodes, indexOfSmallest); printf("smallest: %d\n", indexOfSmallest); printf("second smallest: %d\n", indexOfSecondSmallest); Node *smallest = nodes[indexOfSmallest]; Node *secondSmallest = nodes[indexOfSecondSmallest]; // merge nodes Node *tree = malloc(sizeof(Node)); tree->value = smallest->value + secondSmallest->value; tree->letterIndex = 127; tree->left = smallest; tree->right = secondSmallest; nodes[indexOfSmallest] = tree; secondSmallest->value = -1; printNodeValues(nodes); } Node * tree; for(i=0; i<LETTERS_COUNT-1; i++) { if(nodes[i]->value >= 0) { tree = nodes[i]; break; } } return tree; }
WordData * parseAndHuff(char * reviews_path, int * word_count) { int i = 0; FILE * reviews_stream = fopen(reviews_path, "r"); if (reviews_stream == NULL) { printf("reviews stream failed to open\n"); return NULL; } //create BST of WordData structs by name int num_unique_words = 0; int word_buffer_position = 0; int max_word_size = 200; WordData * w = NULL; char * word_buffer = malloc(max_word_size * sizeof(char)); while(!feof(reviews_stream)) { int current = fgetc(reviews_stream); //fputc((char)current, stderr); //Test: is current a ' ', '\t' or '\n' (word separators): insert the buffer, then insert the current character if (current == ' ' || current == '\t' || current == '\n' || current == '.' || current == ',') { //insert word up untill current character unless the last word was a word separator if (word_buffer[0] != ' ' && word_buffer[0] != '\t' && word_buffer[0] != '\n' && word_buffer[0] != '.' && word_buffer[0] != ',') { //printf("word inserted: (%s) \tcurent character is: (%c)\n",word_buffer, current); w = WordData_insert(w, word_buffer, &num_unique_words); //printf("w->word: %s, w->frequency: %d, w->left: %p, w->right: %p\n",w->word, w->frequency, w->left, w->right); } //reset the buffer position, add current character to the buffer, and insert word word_buffer_position = 0; word_buffer[word_buffer_position] = (char)current; word_buffer[word_buffer_position+1] = '\0'; //printf("word inserted: (%s) \n",word_buffer); w = WordData_insert(w, word_buffer, &num_unique_words); } else { word_buffer[word_buffer_position] = (char)current; word_buffer[word_buffer_position+1] = '\0'; word_buffer_position++; } i++; } free(word_buffer); fclose(reviews_stream); //printTree(w); *word_count = num_unique_words; //printf("num of unique words is: %d\n",num_unique_words); //create array from BST and sort it by frequency int index = 0; WordData ** array = (WordData**)malloc(num_unique_words * sizeof(WordData *)); arrayBuild(array, w, &index, num_unique_words); qsort(array, (size_t)num_unique_words, sizeof(WordData *), comparFrequency); //printf("printing sorted array\n"); /*for (i = 0; i < num_unique_words; i++) { printf("%d) frequency: %d, leaf: %d, word: (%s)\n",i, array[i]->frequency, array[i]->leaf, array[i]->word ); }*/ //printf("number of unique words is: %d\n", num_unique_words); //build the huffman tree int max_non_leaves = 1000; int num_non_leaves = 0; WordData ** non_leaves = malloc(max_non_leaves * sizeof(WordData *)); int remaining_nodes = num_unique_words-1; int combined_frequency; int f_smallest_ind; int s_smallest_ind; while(remaining_nodes >= 1) { //ensure there is enough space in the array of non leaves if (num_non_leaves >= max_non_leaves-1) { max_non_leaves *= 2; non_leaves = realloc(non_leaves, max_non_leaves * sizeof(WordData *)); } //take 2 lowest frequency nodes, combine them f_smallest_ind = findSmallest(array, remaining_nodes); if (remaining_nodes == 1) { s_smallest_ind = f_smallest_ind? 0: 1; } else if (f_smallest_ind != remaining_nodes && array[f_smallest_ind-1]->frequency > array[remaining_nodes]->frequency) { s_smallest_ind = remaining_nodes; } else { s_smallest_ind = f_smallest_ind-1; } WordData * f_smallest = array[f_smallest_ind]; WordData * s_smallest = array[s_smallest_ind]; //ensure all leaf's children point to null if (f_smallest->leaf == 1) { f_smallest->left = NULL; f_smallest->right = NULL; } if (s_smallest->leaf == 1) { s_smallest->left = NULL; s_smallest->right = NULL; } //strcat(f_smallest->word,s_smallest->word) //printf("first smallest(%d)(%d): %s, second smallest(%d)(%d): %s\n",f_smallest_ind, f_smallest->frequency, f_smallest->word, // s_smallest_ind, s_smallest->frequency, s_smallest->word); combined_frequency = f_smallest->frequency + s_smallest->frequency; non_leaves[num_non_leaves] = WordData_create(0, "non leaf" , combined_frequency, f_smallest, s_smallest); //add that node back into the array, decrement array size arrayInsert(array, non_leaves[num_non_leaves], remaining_nodes, s_smallest_ind, f_smallest_ind); remaining_nodes--; num_non_leaves++; } WordData * huffman_tree = array[0]; free(array); free(non_leaves); //printTree(huffman_tree); return huffman_tree; }
Relation *twoPassNaturalJoin(Relation *rptr1, Relation *rptr2, string r3, vector<string> fields1, vector<string> fields2, vector<string> op) { int i1,j1,k,l,m,c1,c2; i1=j1=k=l=m=c1=c2 = 0; Schema s1, s2; bool flag = 1; vector <string> fields_r1, fields_r2, fields_r3; vector<enum FIELD_TYPE> field_type1, field_type2, types_r3; Block *block_ptr1, *block_ptr2; //Relation *rptr1, *rptr2; vector<string> values_r3; int num_blocks_r1, num_blocks_r2, max_blocks; num_blocks_r1 = rptr1->getNumOfBlocks(); num_blocks_r2 = rptr2->getNumOfBlocks(); max_blocks = mem.getMemorySize(); s1 = rptr1->getSchema(); s2 = rptr2->getSchema(); fields_r1 = s1.getFieldNames(); fields_r2 = s2.getFieldNames(); for(int i=0; i< fields1.size(); i++) field_type1.push_back(s1.getFieldType(fields1[i])); //Create sorted sublists of size <M sortRelation(rptr1, s1, fields1, field_type1); for(int i=0; i< fields2.size(); i++) field_type2.push_back(s2.getFieldType(fields2[i])); //Create sorted sublists of size <M sortRelation(rptr2, s2, fields2, field_type2); for(int i=0; i<fields_r1.size(); i++){ fields_r3.push_back(rptr1->getRelationName() + "." + fields_r1[i]); types_r3.push_back(s1.getFieldType(fields_r1[i])); } //Find common attributes of both relations for(int i=0; i<fields_r2.size(); i++) { flag = 1; for(int k=0; k<op.size(); k++) { if(op[k] == "=" && fields_r2[i] == fields2[k] ) { flag = 0; break; } } if(flag) { fields_r3.push_back(rptr2->getRelationName() + "." + fields_r2[i]); types_r3.push_back(s2.getFieldType(fields_r2[i])); } } Schema s3(fields_r3,types_r3); //string r3 = rptr1->getRelationName() + "." + rptr2->getRelationName() + ".NaturalJoin"; Relation *rptr3 = schema_manager.createRelation(r3, s3); if(disp) { //displayRelationInfo(rptr3); //cout<<s3<<endl; cout<<*rptr1<<endl; cout<<*rptr2<<endl; } int num_sublists_r1 = num_blocks_r1/(max_blocks - 1)+((num_blocks_r1%(max_blocks - 1) > 0)?1:0); int num_sublists_r2 = num_blocks_r2/(max_blocks - 1)+((num_blocks_r2%(max_blocks - 1) > 0)?1:0); vector<int> block_used_r1, block_used_r2, disk_block_index_r1, disk_block_index_r2; if(disp) { cout<<"number of sublists in r1: "<<num_sublists_r1<<endl; cout<<"number of sublists in r2: "<<num_sublists_r2<<endl; } //Get one block from each sublist into main memory for(k = 0; k < num_sublists_r1; k++) { if(disp) { cout<<"k: "<<k<<endl; cout<<"disk block index: "<<k*(max_blocks-1)<<endl; } block_ptr1 = mem.getBlock(k); rptr1->getBlock(k*(max_blocks-1), k); disk_block_index_r1.push_back(1); block_used_r1.push_back(block_ptr1->getNumTuples()); //block_ptr1->clear(); } for(l = 0; l < num_sublists_r2; l++) { if(disp) { cout<<"l: "<<l<<endl; cout<<"disk block index: "<<l*(max_blocks - 1)<<endl; } block_ptr2 = mem.getBlock(l+num_sublists_r1); rptr2->getBlock(l*(max_blocks-1), l + num_sublists_r1); disk_block_index_r2.push_back(1); block_used_r2.push_back(block_ptr2->getNumTuples()); //block_ptr2->clear(); } if(disp) { cout<<"Block Used 1: "<<block_used_r1.size()<<endl; cout<<"Block Used 2: "<<block_used_r2.size()<<endl; } vector<Tuple> tuples_r3; //Read one block of relation into Main memory and combine each tuple in the block //with all the tuples in the other //Block *block_r1 = mem.getBlock(max_blocks - 2); Block *block_r3 = mem.getBlock(max_blocks - 1); block_r3->clear(); i1 = j1 = 1; bool done = false; int block_id, tuple_offset; while(!done) { vector<Tuple> tuples_r1 = mem.getTuples(0, num_sublists_r1); vector<Tuple> tuples_r2 = mem.getTuples(num_sublists_r1, num_sublists_r2); Tuple tuple = rptr3->createTuple(); int rel_no = 0; int id = findSmallest(tuples_r1, tuples_r2, fields1, fields2, rel_no); if(disp) cout<<"id: "<<id<<"rel: "<<rel_no<<endl; if(rel_no == 1) { if(disp) cout<<"Smallest Tuple: "<<tuples_r1[id]<<endl; for(m = 0; m < tuples_r2.size(); m++) { if(isJoinTuple(tuples_r1[id], tuples_r2[m], fields1, fields2, op)) { if(disp) { cout<<"\n********************\nJoining:"<<endl; cout<<tuples_r1[id]<<endl; cout<<tuples_r2[m]<<endl; } for(int l =0; l < fields_r1.size(); l++) { if(s3.getFieldType(rptr1->getRelationName() + "." + fields_r1[l]) == INT) tuple.setField(rptr1->getRelationName() + "." + fields_r1[l],tuples_r1[id].getField(fields_r1[l]).integer); else tuple.setField(rptr1->getRelationName() + "." + fields_r1[l],*(tuples_r1[id].getField(fields_r1[l]).str)); } for(int l =0; l < fields_r2.size(); l++) { int flag = 1; for(int n = 0; n < op.size(); n++) { if(op[n] == "=" && fields_r2[l].compare(fields2[n]) == 0) { flag = 0; break; } } if(flag) { if(s3.getFieldType(rptr2->getRelationName() + "." + fields_r2[l]) == INT) tuple.setField(rptr2->getRelationName() + "." + fields_r2[l],tuples_r2[m].getField(fields_r2[l]).integer); else tuple.setField(rptr2->getRelationName() + "." + fields_r2[l],*(tuples_r2[m].getField(fields_r2[l]).str)); } } if(disp) cout<<"New Tuple:"<<tuple<<endl; tuples_r3.push_back(tuple); block_r3->appendTuple(tuple); //If main memory block is full, write it to disk and clear that block if(tuples_r3.size() == s3.getTuplesPerBlock()) { rptr3->setBlock(rptr3->getNumOfBlocks(),max_blocks - 1); tuples_r3.clear(); block_r3->clear(); } } } //block_ptr1->clear(); if(s1.getTuplesPerBlock() == 1) { block_id = id; tuple_offset = 0; } else { block_id = id/num_sublists_r1; tuple_offset = id%num_sublists_r1; } block_used_r1[block_id]--; block_ptr1 = mem.getBlock(block_id); block_ptr1->nullTuple(tuple_offset); //tuples_r1[id].null(); //If a particular block of tuples has been used in the main memory, replenish from disk if(block_used_r1[block_id] == 0) { if(disp) cout<<"Block list consumed: "<<block_id<<endl; //If we have used up all the blocks in this sublist if(exhaustedAllSublists(block_used_r1)) done = true; else { cout<<disk_block_index_r1[block_id]<<endl; if(disk_block_index_r1[block_id] == ((max_blocks-1 < num_blocks_r1) ? (max_blocks - 1) : num_blocks_r1)) { continue; } else { block_ptr1->clear(); block_ptr1 = mem.getBlock(block_id); rptr1->getBlock(disk_block_index_r1[block_id] + (block_id)*(max_blocks-1), block_id); block_used_r1[block_id] = block_ptr1->getNumTuples(); ++disk_block_index_r1[block_id]; } } } } else if(rel_no == 2) { if(disp) cout<<"Smallest Tuple: "<<tuples_r2[id]<<endl; for(m = 0; m < tuples_r1.size(); m++) { if(isJoinTuple(tuples_r1[m], tuples_r2[id], fields1, fields2, op)) { if(disp) { cout<<"\n********************\nJoining:"<<endl; cout<<tuples_r1[m]<<endl; cout<<tuples_r2[id]<<endl; } for(int l =0; l < fields_r1.size(); l++) { if(s3.getFieldType(rptr1->getRelationName() + "." + fields_r1[l]) == INT) tuple.setField(rptr1->getRelationName() + "." + fields_r1[l],tuples_r1[m].getField(fields_r1[l]).integer); else tuple.setField(rptr1->getRelationName() + "." + fields_r1[l],*(tuples_r1[m].getField(fields_r1[l]).str)); } for(int l =0; l < fields_r2.size(); l++) { int flag = 1; for(int n = 0; n < op.size(); n++) { if(op[n] == "=" && fields_r2[l].compare(fields2[n]) == 0) { flag = 0; break; } } if(flag) { if(s3.getFieldType(rptr2->getRelationName() + "." + fields_r2[l]) == INT) tuple.setField(rptr2->getRelationName() + "." + fields_r2[l],tuples_r2[id].getField(fields_r2[l]).integer); else tuple.setField(rptr2->getRelationName() + "." + fields_r2[l],*(tuples_r2[id].getField(fields_r2[l]).str)); } } if(disp) cout<<"New Tuple:"<<tuple<<endl; tuples_r3.push_back(tuple); block_r3->appendTuple(tuple); //If main memory block is full, write it to disk and clear that block if(tuples_r3.size() == s3.getTuplesPerBlock()) { rptr3->setBlock(rptr3->getNumOfBlocks(),max_blocks - 1); tuples_r3.clear(); block_r3->clear(); } } } //block_ptr2->clear(); if(s2.getTuplesPerBlock() == 1) { block_id = id; tuple_offset = 0; } else { block_id = id/num_sublists_r2; tuple_offset = id%num_sublists_r2; } block_used_r2[block_id]--; block_ptr2 = mem.getBlock(num_sublists_r1 + block_id); block_ptr2->nullTuple(tuple_offset); //If a particular block of tuples has been used in the main memory, replenish from disk if(block_used_r2[block_id] == 0) { if(disp) cout<<"Block list consumed: "<<block_id<<endl; //If we have used up all the blocks in this sublist if(exhaustedAllSublists(block_used_r2)) done = true; else { cout<<disk_block_index_r2[block_id]<<endl; if(disk_block_index_r2[block_id] == ((max_blocks-1<< num_blocks_r2) ? (max_blocks - 1) : num_blocks_r2)) { continue; } else { block_ptr2->clear(); block_ptr2 = mem.getBlock(num_sublists_r1 + block_id); rptr2->getBlock(disk_block_index_r2[block_id] + (block_id)*(max_blocks-1), num_sublists_r1 + block_id); block_used_r2[block_id] = block_ptr2->getNumTuples(); ++disk_block_index_r2[block_id]; } } } } if(disp) { cout<<"#r3: "<<rptr3->getNumOfTuples()<<endl; //cout<<*rptr3<<endl; //cin.get(); } } //For the last tuple which might not be full, need to write that to disk too if(tuples_r3.size() !=0) rptr3->setBlock(rptr3->getNumOfBlocks(),max_blocks - 1); if(disp) { cout<<*rptr3<<endl; } return rptr3; }
/* * Delete the first node found with key. */ void deleteTreeNode(binTreeNode_t **ppTree, binTreeNode_t *pDelNode, binTreeNode_t *pDelParent, int bLeftChild) { binTreeNode_t *leftChild, *rightChild; binTreeNode_t *pSmallest = NULL, *pSmallestParent = NULL; int bSmallestLeftChild = 0; if (pDelNode != NULL) { /* remove node then update subtrees */ leftChild = pDelNode->left; rightChild = pDelNode->right; /* update */ /* no children */ if (leftChild == NULL && rightChild == NULL) { /* root node to be deleted */ if (pDelNode == *ppTree) { destroyTreeNode(pDelNode); *ppTree = NULL; } /* not root node, so can just deallocate memory */ else { destroyTreeNode(pDelNode); if (bLeftChild) { pDelParent->left = NULL; } else { pDelParent->right = NULL; } } } /* two children */ else if (leftChild != NULL && rightChild != NULL) { pSmallest = findSmallest(rightChild, &pSmallestParent, &bSmallestLeftChild); /* exchange pSmallest with deleted node */ swapNodes(ppTree, pDelNode, &pDelParent, &bLeftChild, pSmallest, &pSmallestParent, &bSmallestLeftChild); /* delete the pDelNode now */ deleteTreeNode(ppTree, pDelNode, pDelParent, bLeftChild); } /* one child */ else if (leftChild != NULL) { assert(rightChild == NULL); /* root node to be deleted */ if (pDelNode == *ppTree) { destroyTreeNode(pDelNode); pDelNode = NULL; *ppTree = leftChild; } /* not root node */ else { destroyTreeNode(pDelNode); if (bLeftChild) { pDelParent->left = leftChild; } else { pDelParent->right = leftChild; } } } else if (rightChild != NULL) { assert(leftChild == NULL); /* root node to be deleted */ if (pDelNode == *ppTree) { destroyTreeNode(pDelNode); pDelNode = NULL; *ppTree = rightChild; } /* not root node */ else { destroyTreeNode(pDelNode); if (bLeftChild) { pDelParent->left = rightChild; } else { pDelParent->right = rightChild; } } } } } /* end of deleteNode() */