I believe your teacher wants you to NOT sort the items, but yet make an ordered list.
You can do that, by sorting an index which is associated with the items. The items themselves are not sorted in any way - they stay in their original positions.
After sorting through an index, you can use the index to conduct your binary search, with no problem at all.
It will stretch your brain a bit, but that's the fun of programming anyway.
The first step is quite simple, you make an int array, the same size as the number of items you have in your list, and give each element of the array, an initial value:
Code:
int i;
int a[numItems]; //numItems might be 10, 50, or whatever
for(i = 0; i < numItems; i++)
a[i] = i;
so that was ez squeezy.
I've got a tree trimmer working a chain saw right outside my window, so I have to take a break. I'll be back when he's done giving me an earache.
Now the indexes are set, and we're ready to sort - but with this twist - we compare the original strings to each other, THROUGH THE INDEX array. We *MOVE* only the index.
This is a simple sort, which moves the original items:
Code:
for(i = 0; i < numItems - 1; i++) {
for(j = i+1; j < numItems; j++) {
if(a[i] > a[j] {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
//here's the same sort, but leaving the original items alone, and moving the index, instead:
for(i = 0; i < numItems - 1; i++) {
for(j = i+1; j < numItems; j++) {
if(a[index[i]] > a[index[j]] { //comparisons are made through the index array
temp = index[i];
index[i] = index[j];
index[j] = temp;
}
}
}
Does this sound like what you need?