I need some help designing a better approach to a problem. The following is the problem I'm working on:

FIRST STEP:

The data I have:

Column A: P1, P2, P3,....., Pn

Column B: A1, A2, A3,.....,An

And, P1 corresponds to A1 and P2 corresponds to A2 and so on. In other words, they are linked in some way.

In Column B, for every consecutive Aa, Ab, if there is a change then the corresponding Pa and Pb belong to the same set Ca.

So for example, consider the following:

Column A: 12, 14, 16, 17, 32, 45, 56, 65

Column B: 1, 1, 1, 2, 2, 2, 3, 3

In this case, 16, 17 come under the same group, 45 and 56 come under the same group and we write them as {16,17}, {45,56}.

SECOND STEP:

Now consider many such columns. For example, Column C and Column D linked up in same way. In similar fashion, consider other columns taking two columns to be linked with each other in the following way:

Column A: 12, 14, 16, 17, 32, 45, 56, 65

Column B: 1, 1, 1, 2, 2, 2, 3, 3

Output from First Step: {16,17}, {45,56}

Column C: 12, 14, 16, 18, 32, 45, 566, 65

Column D: 1, 1, 2, 2, 2, 2, 3, 4

Output from First Step: {14,16}, {45,566}

Column E: 12, 14, 16, 57, 32, 45, 256, 65

Column F: 1, 1, 1, 2, 2, 3, 3, 4

Output from First Step: {16,57}, {32,45}, {256,65}

Column G: 12, 14, 16, 12, 32, 45, 576, 65

Column H: 1, 1, 1, 2, 2, 3, 4, 4

Output from First Step: {16,12}, {32,45},{45,576}

In the second step, we need to compare the results of each of the above outputs with each other. Take a set {a1,b1} from Columns A & B and {a2,b2} from Columns C & D, then if a1 = a2, the both of these form the same set and should be written as {{a1,b1},{a2,b2}} and similarly others.

So logically speaking, we need to take each and every element and compare with the rest of the elements and see if they're equal and if a match is found, we need to dump them into a group.

I want to calculate the total number of steps it requires to sort it in the said manner and the order of complexity of the algorithm and ofcourse, a meaningful approach. Can someone help with get started with this please?

I've attached an image too...