Binary sort in large array.

Hey guys! First post. I'm having trouble with a binary sort with an enormous array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  for(int i = 0; i < used; i++)
	{
		sorted = false;
		while(sorted == false)
		{
			sorted = true;
			for(int j = 0; j < 5; j++)
			{
				if(array[j][i] < array[j-1][i])
				{
					temp = array[j][i];
					array[j][i] = array[j-1][i];
					array[j-1][i] = temp;
					sorted = false;
				}
			}
		}
	}


So my sort should (and does) order the data from least to greatest in each row of the array. However, used is 1500. The array is 6x2000, with only 1500 rows filled in. The sort ignores the last column. I've tried using the swap function, but that doesn't fix the issue.

Now, everything seems to work fine until around i = 1036. Everything is sorted and the numbers look good, although I haven't exactly scanned all the data. Right there it starts spitting out enormous numbers for j = 4 (the second to last column in the array, but the last one affected by the sort. If I change the columns sorted, the last one is always bugged.)

I've only taken the first level of a college C++ class, and don't even know anything about pointers yet, so I fear this bug may be beyond my level. It seems like the issue might be with memory locations or something, because it only occurs when my array gets too large. The numbers are also different each time I run the program, as though they are randomized and seeded with time. The issue only occurs when I apply the sort, as well. The numbers were fine before.

if anyone could help me understand/fix this issue, I would greatly appreciate it! Thank you in advance.
1
2
3
			for(int j = 0; j < 5; j++)
			{
				if(array[j][i] < array[j-1][i])
when j=0 you try to access array[-1], which is out of bounds.

Also, the algorithm is bubble sort
Oh my god, really? Well, thank you. I wonder why it was only affecting the numbers near the end. Perhaps it was just a coincidence.
Topic archived. No new replies allowed.