Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need custom sorting algorithm
#11
This is crazy to order the columns most simialr to all the other columns???
You can only compare all the other columns to one of the columns for a proper compare of matches.

Otherwise a column thats all *'s will match perfectly any other column with random *'s if that's what you really want then order the columns by most *'s
b = b + ...
Reply
#12
(11-14-2023, 09:54 AM)bplus Wrote: This is crazy to order the columns most simialr to all the other columns???
You can only compare all the other columns to one of the columns for a proper compare of matches.

Otherwise a column thats all *'s will match perfectly any other column with random *'s if that's what you really want then order the columns by most *'s

Not really, I don't think.   Take a look at this example:

x----
x----
x----
x----
-xxxx

Now the first column has 4 stars, but it doesn't match any of the other columns.
The other columns only have 1 star, but they all match in the same row.

I'm thinking that it might be the 4 columns that we're looking for here, rather than the 1 column with all the stars?
Reply
#13
(11-14-2023, 10:35 AM)SMcNeill Wrote:
(11-14-2023, 09:54 AM)bplus Wrote: This is crazy to order the columns most simialr to all the other columns???
You can only compare all the other columns to one of the columns for a proper compare of matches.

Otherwise a column thats all *'s will match perfectly any other column with random *'s if that's what you really want then order the columns by most *'s

Not really, I don't think.   Take a look at this example:

x----
x----
x----
x----
-xxxx

Now the first column has 4 stars, but it doesn't match any of the other columns.
The other columns only have 1 star, but they all match in the same row.

I'm thinking that it might be the 4 columns that we're looking for here, rather than the 1 column with all the stars?

I not really followed this, but it seems like a normal binary number sort to me, instead of *--**-**, think of it as 10011011. That's a number and can be perfectly sorted with other numbers (similar bit patterns).
Reply
#14
(11-14-2023, 01:34 PM)RhoSigma Wrote:
(11-14-2023, 10:35 AM)SMcNeill Wrote:
(11-14-2023, 09:54 AM)bplus Wrote: This is crazy to order the columns most simialr to all the other columns???
You can only compare all the other columns to one of the columns for a proper compare of matches.

Otherwise a column thats all *'s will match perfectly any other column with random *'s if that's what you really want then order the columns by most *'s

Not really, I don't think.   Take a look at this example:

x----
x----
x----
x----
-xxxx

Now the first column has 4 stars, but it doesn't match any of the other columns.
The other columns only have 1 star, but they all match in the same row.

I'm thinking that it might be the 4 columns that we're looking for here, rather than the 1 column with all the stars?

I not really followed this, but it seems like a normal binary number sort to me, instead of *--**-**, think of it as 10011011. That's a number and can be perfectly sorted with other numbers (similar bit patterns).

I don't think it's quite that simple either, Rho.   Think of this set of data:

x----
x----
x----
x----
-xxxx
---xx

Now, if you just look at going down, column 1 has the most matches.
If you just look going right, row 4 has the most matches.

I think what he's looking for here is columns 4 and 5, which each have 2 stars matching between them.

The only way I see to get this is basically just as bplus said:  "You can only compare all the other columns to one of the columns for a proper compare of matches."

The trick is you just have to do a loop to compare ALL of those columns 1 by 1, against all of the other columns, and then shuffle the results of those matches.
Reply
#15
I tried Rho's idea of just sorting the columns as strings (same as sorting binary numbers) but that IS too simple.

I suppose you could take each column and compare BOTH *'s and -"s to every other column
and tally up the matched chars and I suppose it is possible that some will have higher matches than others.

Come to think of it, when you compare two columns and count matches, you never have to compare those 2 columns again, nothing changes they both have same amount of matches.

So
for i= 1 to columns-1
for j = i +1 to columns
'compare
'tally matches for column i AND column j
next
next
then sort by matches

This would how I setup a random data set:
Code: (Select All)
Const columns = 65, rows = 21

Dim cr$(1 To columns, 1 To rows)
For r = 1 To rows
    For c = 1 To columns
        If Int(Rnd * 2) Then cr$(c, r) = "*" Else cr$(c, r) = "-"
        Print cr$(c, r);
    Next
    Print
Next
as opposed to how Steve set this up.

I'd be more motivated to finish code if @random1 or anyone can reveal to me a practical purpose to this.
It'd probably make it easier to code or find a better appraoches for the practical problem.
b = b + ...
Reply
#16
Finally got it, took forever to get the results printed out in columns on the default screen.
I did that matches as I explained in previous reply, comparing each column to every other one and counting up the matches.

Here is a screen shot of matching one column with another yellow is a match, blue is a miss:
   

Here is code:
Code: (Select All)
Const columns = 65, rows = 21 ' this is based on OP's screen shot
DefLng A-Z
' create some data randomly for testing
Dim cols$(1 To columns) ' make cols$()
For c = 1 To columns
    For r = 1 To rows
        cols$(c) = cols$(c) + IfS$(Int(Rnd * 2), "*", "-")
    Next
Next

'display
Color 7
For m = 1 To rows
    For c = 1 To columns
        Locate m, c: Print Mid$(cols$(c), m, 1);
    Next
Next
'        count matches and watch a color show!
' matches
Dim matches(1 To columns)
For i = 1 To columns - 1
    For j = i + 1 To columns
        match = 0
        For r = 1 To rows
            If Asc(cols$(i), r) = Asc(cols$(j), r) Then
                matches(i) = matches(i) + 1: matches(j) = matches(j) + 1
                Color 14
                Locate r, i: Print Mid$(cols$(i), r, 1);
                Locate r, j: Print Mid$(cols$(j), r, 1);
            Else
                Color 9
                Locate r, i: Print Mid$(cols$(i), r, 1);
                Locate r, j: Print Mid$(cols$(j), r, 1);
            End If
        Next
        _Delay .1
        Color 7
        For m = 1 To rows
            For c = 1 To columns
                Locate m, c: Print Mid$(cols$(c), m, 1);
            Next
        Next
    Next
Next

Color 15
Cls
Dim Shared IDX(1 To columns) ' swap ID when swap values
For i = 1 To columns
    IDX(i) = i
Next
QuickSort 1, columns, matches()
Open "Matches column ID.txt" For Output As #1
For i = 1 To columns
    If i < 21 Then
        c = 0
    ElseIf i < 41 Then
        c = 1
    ElseIf i < 61 Then
        c = 2
    Else
        c = 3
    End If
    If i Mod 20 = 0 Then c = c + 1
    Locate (i Mod 20) + 1, c * 20 + 1
    Print matches(i); IDX(i);
    Print #1, matches(i); IDX(i)
Next
Close #1
Locate 22, 1: Print


Sub QuickSort (start As Long, finish As Long, array() As Long)
    Dim Hi As Long, Lo As Long, Middle As Single
    Hi = finish: Lo = start
    Middle = array((Lo + Hi) / 2) 'find middle of array
    Do
        Do While array(Lo) < Middle: Lo = Lo + 1: Loop
        Do While array(Hi) > Middle: Hi = Hi - 1: Loop
        If Lo <= Hi Then
            Swap array(Lo), array(Hi)
            Swap IDX(Lo), IDX(Hi) ' track the IDX of the matches
            Lo = Lo + 1: Hi = Hi - 1
        End If
    Loop Until Lo > Hi
    If Hi > start Then Call QuickSort(start, Hi, array())
    If Lo < finish Then Call QuickSort(Lo, finish, array())
End Sub

Function IfS$ (Bool&, tru$, fals$) ' IF Boolean Return True$ else False$
    If Bool& Then IfS$ = tru$ Else IfS$ = fals$
End Function

Here is the results with increasing matches going down and to the right. The big number on left the match count the number on right the column number:
   

I even printed the results into a txt file just to make sure they were ordered correctly while I fiddled around trying to get the results listed right on the default screen:
Code: (Select All)
629  37
633  14
635  63
640  33
652  35
654  40
655  49
655  45
656  3
656  54
656  57
660  17
661  11
662  65
662  18
662  58
663  44
663  34
665  9
666  26
666  4
668  43
670  25
670  13
671  19
672  46
673  32
674  22
674  24
675  21
675  31
676  10
676  55
676  2
677  5
677  48
678  12
678  15
679  6
680  62
680  60
681  36
682  28
683  39
684  23
684  29
684  64
685  47
686  1
686  41
686  7
686  59
688  27
688  50
689  61
689  30
689  51
691  53
696  42
697  56
698  52
698  38
708  20
714  16
714  8
b = b + ...
Reply




Users browsing this thread: 2 Guest(s)