Sorting an array of strings

I am getting an error for my sort function and I don't understand why. It's also not sorting the array at all. I'm trying to sort an array of random strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82


#include<iostream>
#include<string>
using namespace std;

void setupStrings(string[], int);
void bubbleSort(string[], int);

int main()
{
	string str1000[1000], str2000[2000], str3000[3000];
	string str4000[4000], str5000[5000], str6000[6000];
	
	setupStrings(str1000, 1000);
	setupStrings(str2000, 2000);
	setupStrings(str3000, 3000);
	setupStrings(str4000, 4000);
	setupStrings(str5000, 5000);
	setupStrings(str6000, 6000);

	for (int i = 0; i < 100; i++)
	{
		cout << i << " " << str1000[i] << endl;
	}

	cout << endl;
	bubbleSort(str1000, 1000);//does not sort
	
	for (int i = 0; i < 100; i++)
	{
		cout <<i<<" "<< str1000[i] << endl;
	}
	

	return 0;
}

void setupStrings(string array[], int size)
{

	char charset[52] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
		'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
		'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
	string randomstr;
	int randomdx;
	for (int x = 0; x < size; x++)
	{
		for (int i = 1; i <= 25; i++)
		{
			randomdx = rand() % 52;
			randomstr = randomstr + charset[randomdx];
		}
		array[x] = randomstr;
		randomstr.erase(0, 25);
	}


}

void bubbleSort(string array[], int size)
{
	string temp;
	int swap=0;
	int i = 0;
	for (i = 0; i < size;)
	{
		
		if (array[i] > array[i + 1])
		{
			temp = array[i];
			array[i] = array[i + 1];
			array[i + 1] = temp;
			
		}
		temp.erase(0, 25);
		i++;
	}

	return;

}
Line 76 is the problem. That's not how you clear a string. You need to use .clear(). The runtime error is because you blindly assume the string has 25 characters, but if it doesn't then it will generate an out of range error.

But... there's no reason to clear the string at all, because you never use its value after that point (you assign to it and overwrite the old value on line 71).
Last edited on
I took that out but it still doesn't fix the problem of it not sorting at all on both of the couts of the array it is the exact same before and after the sort so why doesn't the code sort the array?
I also still get the same error
http://ideone.com/Wfg75z - they are not exactly the same, the second is partially sorted. The reason is because you only loop through the array once, but bubble sort uses nested loops.

By the way, your loop convention is weird - this is what you wrote:
65
66
67
68
69
70
int i = 0;
for(i = 0; i < size;)
{
    //...
    i++;
}
That's pretty unusual, why do you not do it the traditional way?
65
66
67
68
for(int i = 0; i < size; ++i)
{
    //...
}
Last edited on
I was going by what my teacher said, but I did it that way and it works. I get an error if I do i < size, but if I do i < size-1 I do not get an error. Is that sorting the entire array, or is it an off by one error?
mtbrooks1993 wrote:
I get an error if I do i < size, but if I do i < size-1 I do not get an error. Is that sorting the entire array, or is it an off by one error?
On lines 69, 72, and 73, you access array index i+1 - what do you think happens when you get to the end of the array?
It goes over the array bounds and compares it with something not in the array. Also I see what you where saying about it being unusual on the for loop. I did it that way because I was trying to make a while loop. It would be more efficient for it to break out of the loop if there wasn't a swap. I just never moved the increment.
mtbrooks1993 wrote:
It would be more efficient for it to break out of the loop if there wasn't a swap.
That's a good optimization, but you should try to make the program work first and then optimize after it works. That way you can always revert back to a working state if an optimization you make breaks the program.
Last edited on
Topic archived. No new replies allowed.