crispy wrote: |
---|
My one question pertains to the sort for loop though. When you nested a for loop inside a nested for loop, the second and third are to sort every element from left to right, correct? While the first for loop says after sorting this row we stop and start over on the next.
|
Yes, this is exactly what it does. Row after row, I sort. Notice that in the third loop I'm starting from (j = i). (it has to be j = i +1, btw, I was just in a hurry).
No actually, this has nothing to do with merge sort. Merge sort is way faster, and has a sorting time of O(n log(n)) if I remember correctly (but harder to implement). The one I used here is the most typical-easy sorting algorithm (and is almost the slowest, I don't really know the name). Its sorting time scale is O(n^2). So if there are too many elements, the sorting becomes slower and slower in a quadratic behaviour.
crispy wrote: |
---|
Also, if I am correct that the first for loop sorts one row at a time, would adding a forth for loop for columns make it sort from top-left to bottom-right?
|
No. I don't think so. I think if you wanna do it that way it'll become really very expensive and it'll need an iterative approach.
The way to sort it from top-left to bottom-right is by going through all the numbers from top-left to bottom-right as if they were a single array.
I'm gonna explain how you could do that, but this is gonna be a little confusing. So concentrate, and read carefully:
Now, to sort a single row, you had a first variable
i
that holds (lets call it holder) at a single number, while another variable
j
slides (let's call it slider) through the other variables till it finishes one cycle, and then the holder proceeds by 1 element so that the slider could cycle again.
If you wanna sort them from top-left to bottom-right, you have to have a holder which consists of 2 variables, lets call them
ir
and
ic
(i row and i column), and a slider that consists of 2 variables,
jr
and
jc
.
With exactly the same concept, rather than having 2 loops to cycle through the holder and slider, you have to have 4 loops. First 2 are the holder, and second 2 are the slider. Those 2x2 loops together have to act exactly like if there were no rows and columns, but only a single "long" list of numbers from 0 to (8x8-1) = 63. So they have to run (hypothetically) from the frist top-left to the bottom-right, while they internally are going through rows and columns.
Got the idea? hope it helps. Good luck :)