Cannot find the item in the sorted array.

Sorting algorithm for strings:

AnsiString *words = new AnsiString[150]; AnsiString sortArray[150][150]; int *firstLetter = new int[150]; int *counts = new int[150]; for (int i = 0; i < n; i++) { counts[i] = 0; words[i] = StringGrid1->Cells[i][0]; firstLetter[i] = words[i][1]; for(int j = 122; j >= 97; j--) { if(firstLetter[i] == j) { sortArray[j][counts[j]] = words[i]; counts[j]++; } } } for (int i = 122; i >= 97; i--) { for(int j = 0; j < counts[i]; j++) for(int k = 0; k < counts[i] - 1; k++) { char* s1 = sortArray[i][k].c_str(); char* s2 = sortArray[i][k+1].c_str(); if(strcmp(s1,s2) > 0) { AnsiString temp = sortArray[i][k]; sortArray[i][k] = sortArray[i][k + 1]; sortArray[i][k + 1] = temp; } } } int currentMemo = 0; for (int i = 122; i >=97; i--) { for(int j = counts[i] - 1; j > -1; j--) { StringGrid1->Cells[currentMemo][0] = sortArray[i][j]; currentMemo++; } } 

Algorithm search.

  int binary_search(AnsiString* strlist, AnsiString key) int L = 0; int R = n; int m = L; do { if (strcmp(strlist[m].c_str(), key.c_str()) == 0) return m; if (strcmp(strlist[m].c_str(), key.c_str()) > 0) R = m; if (strcmp(strlist[m].c_str(), key.c_str()) < 0) L = m; m = (L + R)/2; } while ((R - L) != 1); return -1; } 
  • To begin with, it’s not clear why you reassign the strings to yourself: for (int i = 0; i < n; i++) strlist[i] = strlist[i].c_str(); while leaving the str array, which you then work with, empty. By the way, why bother with this at all? why not to work with the main array? - Harry
  • Error from old code. I did it quickly, so I forgot. - Maxim

0