PDA

View Full Version : array of strings sorting


Brooklyn1126
04-22-2004, 09:58 PM
I have the algorithms i need to sort a list of numbers with the insertion,heap,and quick sorts, But i need to sort an array of words. What must i do to fix the code? Thanks

Brooklyn1126
04-22-2004, 10:00 PM
Here is the code...

//INSERTION SORT

#include <iostream.h>
#include <iomanip.h>


int main()

{
char c;

static int A[] = {9,8,7,6,5,4,3,2,1,0};
int key;
for (int j = 1; j < 10 ; j++)
{
key = A[j];
int i = j - 1;
while ((i >=0 ) && (A[i] > key))
{
A[i+1] = A[i];
i--;
}
A[i+1] = key;
//display array each time j moves over one position...
for (i = 0; i < 10; i++)
cout << A[i] << " " ;
cout << endl;
}

cin>>c;
return 0;
}

Stack Overflow
04-22-2004, 10:36 PM
Hi,

Quick question, does this code have to work as moving j one position at a time, or not?

- Stack Overflow

Stack Overflow
04-22-2004, 11:11 PM
Hi,

Well I don't know if this is called a bubble sort or whatever, but it works:

#include <string.h>
#include <iostream.h>
#include <iomanip.h>

void mysort(char *strings[]) {
int i, j;
char *temp;

for (i = 0; i < (int)strlen(strings[i]) - 1; i++) {
for (j = 0; j < (int)strlen(strings[j]) - 1 - i; j++) {
if (strcmp(strings[j], strings[j + 1]) > 0) {
temp = strings[j];
strings[j] = strings[j + 1];
strings[j + 1] = temp;
}
}
}
}

int main() {
int i;
char *words[255] = {"Hello", "Goodbye", "Haha", "Yikes", "Hehe"}; // 5 words

mysort(words); // Do sort

for( i = 0; i < 5; i++ ) { // show 5 words now
cout << words[i] << endl;
}

return 0;
}


Hope this helps,
- Stack Overflow

sans-hubris
04-23-2004, 04:58 AM
I don't understand what problem you are having. Your insertion sort works just fine. You just need to change it from using numbers to using (I suggest std::string).

Brooklyn1126
04-23-2004, 08:26 AM
yeah i know and im not sure how to do that.

gold_dragon
04-23-2004, 10:08 AM
You have to remember that you can do greater than and less than with char and char is really just a number. Unless you use wstring, then you use wchar.

How do you want to sort the string? By first letter word value or do you just want to conver the string by abc..? I have some code that may help you.

Brooklyn1126
04-23-2004, 10:36 AM
i need to sort it by first letter than second etc,, like dog comes before dot.

gold_dragon
04-23-2004, 12:55 PM
I'll be back with some code later, if you haven't solved it by then. Which you probably will or someone else would.

Should just delete this and I will when I post code.

Brooklyn1126
04-23-2004, 01:55 PM
ok thanks

stuka
04-23-2004, 02:26 PM
If I'm reading your code right (my record hasn't been great lately), all you'd really need to do is replace your comparison A[i] > key with strcmp(A[i].c_str(), key.c_str()) > 0) This assumes you're using std::string for your data rather than char*, since it will make your assignments much easier.

sans-hubris
04-24-2004, 06:08 AM
Can you not use std::string (http://www.cppreference.com/cppstring_details.html)?