//Quicksort help from http://www.algolist.net/Algorithms/Sorting/Quicksort void SortingCompetition::quicksort2WithInsert(int start, int end, int passOff){ int pivot = (start+end)/2; int i = start; int j = end; char* temp; //move values greater than pivot to right //& less than pivot to left while(i <= j){ while(compareWords(words2[i],words2[pivot]) <= -1){ i++; } while(compareWords(words2[j],words2[pivot]) >= 1){ j--; } if (i <= j){ temp = words2[i]; words2[i] = words2[j]; words2[j] = temp; if(i == pivot){ //if pivot was swapped with j pivot = j; } else if (j == pivot){ //if pivot was swapped with i pivot = i; } i++; j--; } } //recursive call for left of pivot & right of pivot //or switch to insertion sort if(start < j){ if((j-start+1)>passOff ){ quicksort2(start, j); } else{ insertion_sort(start,j); } } if(i < end){ if((end-i+1)>passOff){ quicksort2(i, end); } else{ insertion_sort(i,end); } } }
bool addWords(int* result, T& dict, int nrows, const char** rows, const int* lens, int ANCHOR, int start) { // compare all rows. If all are equal, returns this if (compareWords(nrows, rows, lens, ANCHOR, start)) { for (int irow = 0; irow < nrows; irow++) { result[irow] = start; } return true; } // compare to known words for (int irow = 0; irow < nrows; irow++) { int len = lens[irow]; if (start + ANCHOR <= len) { const char* word = rows[irow] + start; Ints& pos = getByKey<T>(dict, nrows, word, ANCHOR); if (pos[irow] == -1) { pos[irow] = start; pos[nrows] += 1; if (pos[nrows] == nrows) { for (int irow = 0; irow < nrows; irow++) { result[irow] = pos[irow]; } return true; } } } } return false; }
int afficheMenu(char* dic[]) { switch (myMenu()) { case 1: printf("\nComparaison de 2 mots :\n\n"); wordIhm(); break; case 2: printf("Comparaison et tri\n\n"); sortMyArr(); case 3: printf("Rechercher un mot dans le dictionnaire\n\n"); compareWords(dic); break; case 4: printf("Ajouter un mot dans le dictionnaire\n\n"); addWord(dic); break; case 5: printf("Supprimer un mot\n\n"); removeWord(dic); break; case 6: printf("Trier un tableau\n\n"); sortAnArr(); case 7: printf("Quitter"); return 1; default: printf("Quitter"); return 1; } return 0; }
void SortingCompetition::quicksort2(int start, int end){ int pivot = rand()%(end-start)+start; //move values greater than pivot to right //& less than pivot to left int i = start; int j = end; char* temp; //move values greater than pivot to right //& less than pivot to left while(i <= j){ while(compareWords(words2[i],words2[pivot]) <= -1){ i++; } while(compareWords(words2[j],words2[pivot]) >= 1){ j--; } if (i <= j){ temp = words2[i]; words2[i] = words2[j]; words2[j] = temp; if(i == pivot){ //if pivot was swapped with j pivot = j; } else if (j == pivot){ //if pivot was swapped with i pivot = i; } i++; j--; } } if(start < j){ quicksort2(start, j); } if(i < end){ quicksort2(i, end); } }
void SortingCompetition::bubbleSort(){ for(int j = 0; j<getWordCount();j++){ for(int y = 0;y<(getWordCount()-1);y++){ if (compareWords(words2[y],words2[y+1])== 1){ char* temp1 =words2[y+1]; words2[y+1]=words2[y]; words2[y] = temp1; } } } }
void SortingCompetition::bubbleSort(){ //move greatest values to the end of array for(int j = 0; j<getWordCount();j++){ for(int y = 0; y < (getWordCount()-1); y++){ if (compareWords(words2[y],words2[y+1])>= 1){ char* temp1 = words2[y+1]; words2[y+1] = words2[y]; words2[y] = temp1; } } } }
void SortingCompetition::merge(int low, int middle, int high){ char** temp = new char*[high+1]; int kk; int leftCounter = low; int rightCounter = middle+1; int tempCounter = low; //put values into temporary array where comparing values in right and left //array and then populating temporary array according to lowest value while((leftCounter<=middle)&&(rightCounter<=high)){ if(compareWords(words2[leftCounter],words2[rightCounter])!=1){ temp[tempCounter] = words2[leftCounter]; leftCounter++; } else{ temp[tempCounter] = words2[rightCounter]; rightCounter++; } tempCounter++; } //fill rest of array with remainder of values from left or right array //that has not been fully iterated through yet if(leftCounter>middle){ for(kk = rightCounter; kk<=high; kk++){ temp[tempCounter] = words2[kk]; tempCounter++; } } else{ for(kk=leftCounter;kk<=middle;kk++){ temp[tempCounter] = words2[kk]; tempCounter++; } } //reassign passed array to the now ordered elements in temp for(int m = low;m<=high;m++){ words2[m] = temp[m]; } //free memory delete[] temp; }
//http://mycodinglab.com/insertion-sort-algorithm/ void SortingCompetition::insertion_sort(int start,int end){ int i, j; char* temp; int length = end - start + 1; for (i = start + 1; i < start + length; i++) { j = i; while (j > start && (compareWords(words2[j-1],words2[j])>= 1)){ temp = words2[j]; words2[j] = words2[j - 1]; words2[j - 1] = temp; j--; }//end of while loop } }
static int compareCommandName (const void *key, const void *item) { const char *name = key; const CommandDescriptor *descriptor = item; return compareWords(name, descriptor->entry->name); }
static int testWord (const char *suppliedWord, const char *desiredWord) { return compareWords(suppliedWord, desiredWord) == 0; }
int main(int argc, const char * argv[]) { mainFile = fopen( path, "r"); file = fopen( path, "r"); if (file == NULL) { return 1; } if (mainFile == NULL) { return 1; } int printedMainWord; int matchs; while(fgets(mainWord, sizeof(mainWord), mainFile) > 0) { printedMainWord = 0; long mainWordLength = strlen(mainWord); if( mainWord[mainWordLength-1] == '\n' ) { mainWord[mainWordLength-1] = 0; } matchs = 0; while(fgets(word, sizeof(word), file) > 0) { long wordLength = strlen(word); if( word[wordLength-1] == '\n' ) { word[wordLength-1] = 0; } if (mainWordLength == wordLength && strcmp(mainWord, word) != 0) { if (compareWords(mainWord, word) == 1) { matchs++; if (printedMainWord == 0) { printedMainWord = 1; printf("%s -> ", mainWord); } if(matchs == 1) { printf("%s", word); } else { printf(", %s", word); } } } } if (printedMainWord == 1) { printf("\n"); } rewind(file); } fclose(file); fclose(mainFile); return 0; }
bool compareNew() { clearWindow(nppData._scintillaMainHandle, true); clearWindow(nppData._scintillaSecondHandle, true); active = true; int doc1Length; int *lineNum1; char **doc1 = getAllLines(nppData._scintillaMainHandle, &doc1Length, &lineNum1); if(doc1Length < 1) { return true; } int doc2Length; int *lineNum2; char **doc2 = getAllLines(nppData._scintillaSecondHandle, &doc2Length, &lineNum2); if(doc2Length < 1) { return true; } int doc1Changed = 0; int doc2Changed = 0; diff_edit *doc1Changes = NULL; diff_edit *doc2Changes = NULL; unsigned int *doc1Hashes = computeHashes(doc1, doc1Length, Settings.IncludeSpace); unsigned int *doc2Hashes = computeHashes(doc2, doc2Length, Settings.IncludeSpace); /* make diff */ int sn; struct varray *ses = varray_new(sizeof(struct diff_edit), NULL); int result = (diff(doc1Hashes, 0, doc1Length, doc2Hashes, 0, doc2Length, (idx_fn)(getLineFromIndex), (cmp_fn)(compareLines), NULL, 0, ses, &sn, NULL)); int changeOffset = 0; shift_boundries(ses, sn, doc1Hashes, doc2Hashes, doc1Length, doc2Length); find_moves(ses, sn, doc1Hashes, doc2Hashes, Settings.DetectMove); /* * - insert empty lines * - count changed lines */ doc1Changed = 0; doc2Changed = 0; for (int i = 0; i < sn; i++) { struct diff_edit *e =(diff_edit*) varray_get(ses, i); if(e->op == DIFF_DELETE) { e->changeCount = 0; doc1Changed += e->len; struct diff_edit *e2 =(diff_edit*) varray_get(ses, i+1); e2->changeCount = 0; if(e2->op == DIFF_INSERT) { //see if the DELETE/INSERT COMBO includes changed lines or if its a completely new block if(compareWords(e, e2, doc1, doc2, Settings.IncludeSpace)) { e->op = DIFF_CHANGE1; e2->op = DIFF_CHANGE2; doc2Changed += e2->len; } } } else if(e->op == DIFF_INSERT) { e->changeCount = 0; doc2Changed += e->len; } } int doc1CurrentChange = 0; int doc2CurrentChange = 0; changeOffset = 0; doc1Changes = new diff_edit[doc1Changed]; doc2Changes = new diff_edit[doc2Changed]; int doc1Offset = 0; int doc2Offset = 0; //switch from blocks of lines to one change per line. Change CHANGE to DELETE or INSERT if there are no changes on that line int added; for (int i = 0; i < sn; i++) { struct diff_edit *e =(diff_edit*) varray_get(ses, i); e->set = i; switch(e->op) { case DIFF_CHANGE1: case DIFF_DELETE: added = setDiffLines(e, doc1Changes, &doc1CurrentChange, DIFF_DELETE, e->off + doc2Offset); doc2Offset -= added; doc1Offset += added; break; case DIFF_INSERT: case DIFF_CHANGE2: added = setDiffLines(e, doc2Changes, &doc2CurrentChange, DIFF_INSERT, e->off + doc1Offset); doc1Offset -= added; doc2Offset += added; break; } } if (result != -1) { int textIndex; different = (doc1Changed > 0) || (doc2Changed > 0); for(int i = 0; i < doc1Changed; i++) { switch(doc1Changes[i].op) { case DIFF_DELETE: markAsRemoved(nppData._scintillaMainHandle, doc1Changes[i].off); break; case DIFF_CHANGE1: markAsChanged(nppData._scintillaMainHandle, doc1Changes[i].off); textIndex = lineNum1[doc1Changes[i].off]; for(int k = 0; k < doc1Changes[i].changeCount; k++) { struct diff_change *change = (diff_change*)varray_get(doc1Changes[i].changes, k); markTextAsChanged(nppData._scintillaMainHandle, textIndex + change->off, change->len); } break; case DIFF_MOVE: markAsMoved(nppData._scintillaMainHandle, doc1Changes[i].off); break; } } for(int i = 0; i < doc2Changed; i++) { switch(doc2Changes[i].op) { case DIFF_INSERT: markAsAdded(nppData._scintillaSecondHandle, doc2Changes[i].off); break; case DIFF_CHANGE2: markAsChanged(nppData._scintillaSecondHandle, doc2Changes[i].off); textIndex = lineNum2[doc2Changes[i].off]; for(int k = 0; k < doc2Changes[i].changeCount; k++) { struct diff_change *change=(diff_change*)varray_get(doc2Changes[i].changes, k); markTextAsChanged(nppData._scintillaSecondHandle, textIndex+change->off, change->len); } break; case DIFF_MOVE: markAsMoved(nppData._scintillaSecondHandle,doc2Changes[i].off); break; } } doc1Offset = 0; doc2Offset = 0; if(Settings.AddLine) { int length = 0; int off = -1; for(int i = 0; i < doc1Changed; i++) { switch(doc1Changes[i].op) { case DIFF_DELETE: case DIFF_MOVE: if(doc1Changes[i].altLocation == off) { length++; } else { addEmptyLines(nppData._scintillaSecondHandle, off + doc2Offset, length); doc2Offset += length; off = doc1Changes[i].altLocation; length = 1; } break; } } addEmptyLines(nppData._scintillaSecondHandle, off + doc2Offset, length); if(doc2Offset > 0) { clearUndoBuffer(nppData._scintillaSecondHandle); } length = 0; off = 0; doc1Offset = 0; for(int i = 0; i < doc2Changed; i++) { switch(doc2Changes[i].op) { case DIFF_INSERT: case DIFF_MOVE: if(doc2Changes[i].altLocation == off) { length++; } else { addEmptyLines(nppData._scintillaMainHandle, off + doc1Offset, length); doc1Offset += length; off = doc2Changes[i].altLocation; length = 1; } break; } } addEmptyLines(nppData._scintillaMainHandle, off + doc1Offset, length); if(doc1Offset > 0) { clearUndoBuffer(nppData._scintillaMainHandle); } } //clean up resources #if CLEANUP for(int i = 0; i < doc1Length; i++) { if(*doc1[i] != 0) { delete[] doc1[i]; } } delete[] doc1; delete[] lineNum1; for(int i = 0; i < doc2Length; i++) { if(*doc2[i] != 0) { delete[] doc2[i]; } } delete[] doc2; delete lineNum2; delete[] doc1Hashes; delete[] doc2Hashes; clearEdits(ses, sn); for(int i = 0; i < doc1Changed; i++) { clearEdit(doc1Changes + (i)); } delete[] doc1Changes; for(int i = 0; i < doc2Changed; i++) { clearEdit(doc2Changes+(i)); } delete[] doc2Changes; #endif // CLEANUP if(!different) { ::MessageBox(nppData._nppHandle, TEXT("Files Match"), TEXT("Results :"), MB_OK); return true; } ::SendMessageA(nppData._scintillaMainHandle, SCI_SHOWLINES, 0, (LPARAM)1); ::SendMessageA(nppData._scintillaSecondHandle, SCI_SHOWLINES, 0, (LPARAM)1); return false; } return false; }
void SortingCompetition::multisort(int start, int end, int passOff){ int pivot = rand()%(end-start)+start; int i = start; int j = end; char* temp; //move values greater than pivot to right //& less than pivot to left while(i <= j){ while(compareWords(words2[i],words2[pivot]) <= -1){ i++; } while(compareWords(words2[j],words2[pivot]) >= 1){ j--; } if (i <= j){ temp = words2[i]; words2[i] = words2[j]; words2[j] = temp; if(i == pivot){ //if pivot was swapped with j pivot = j; } else if (j == pivot){ //if pivot was swapped with i pivot = i; } i++; j--; } } //recursive call for left of pivot & right of pivot //or switch to insertion sort if (((j-start+1) > passOff) || ((end-i+1) > passOff)) { if (start < j){ quicksort2(start, j); } if (i < end){ quicksort2(i, end); } } else { #pragma omp parallel num_threads(2) { int thread = omp_get_thread_num(); if(thread == 0){ insertion_sort(start, j); } else{ insertion_sort(i,end); } } } }