Sort cards

First, sort playing cards

1, poke structure

typedef struct {
    char a;
    int b;
}poke;

Wherein a color memory cards A, B, C, D; b digital memory card 1 to 9.

2, an array type queue

queue<poke> color[5];
queue<poke> number[10];
queue<poke> sorted;

Use #include <queue> are array types established poke queue is a queue in each group.

Wherein the number of sequential digital memory card, a sequential color in color memory cards, number and color arrays is 0 queue are not (with or without all lines). sorted for the final sorting finished sorting queue (non-array-type).

Second, the idea of ​​the algorithm

First establish nine queue for storing the size of points, depending on the number of points the cards in their store into the queue, the queue then press 1 to 9 followed by a team queue.
For example, for the above results, after sequentially into the team with the following results:
Queue 1: C1; cohort 3: D3, queue 4: C4, A4
After which in turn is a team, the result is C1, D3, C4, A4
Then, the establishment of four queues for storing color. Stored by the card suit A ~ D 1 ~ 4 into the queue, the queue to a queue and then dequeued 4 sequentially.
For example, a sequence immediately above team C1, D3, C4, A4, which is turn into the team with the following results:
Queue 1: A4; cohort 3: C1, C4; cohort 4: D3
After which in turn is a team, the result is A4, C1, C4, D3, ordering an end.

Fake code

An input n;
 2 . Loop n times;
     2.1 Input pa, pb input;
     2.2 p-pb digit into the first team
 . 3 i from 1 to 9 cycles.
     3.1 cycle to empty the team numbers i
         3.1 . 1 p = Number [I] .front ();
         3.1 . 2 Color [PA - ' A ' + 1 ] .push (P);
         3.1 . . 3 Number [I] .pop ();
 4 .i from 1 to 4 cycle
     4.1 cycle to i team color empty
         4.1 . . 1 P = Color [i] .front ();
         4.1 . 2 sorted.push(p);
        4.1.3 color[i].pop();
5.输出sorted

Third, the time complexity & space complexity

1, time complexity

If the sort given sorted Well, then go through the input data n times, 9n times the data included in the digital team, 4n times the color data included in the team, and then output sorted, so the time complexity is O (n) .

If the sort of disorder is given, the process is the same as the positive sequence, still time complexity O (n).

2, space complexity

Space complexity is O (1).

3, compared to the bubble sort

(1) time complexity: best time to bubble sort complexity is O (n), the worst time complexity is O (n ^ 2), and the time complexity of the algorithm is O (n).

(2) the spatial complexity: are O (1).

Fourth, the code shows

 

#include<iostream>
#include<queue>

using namespace std;

typedef struct {
    char a;
    int b;
}poke;

int main ()
{
    queue<poke> color[5];
    queue<poke> number[10];
    poke p;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> p.a >> p.b;
        number[p.b].push(p);
    }
    for (int i = 1; i <= 9; i++) {
        cout << "Queue" << i << ":";
        while (!number[i].empty()) {
            p = number[i].front();
            cout << p.a << p.b << " ";
            color[p.a - 'A' + 1].push(p);
            number[i].pop();
        }
        cout << "\n";
    }
    queue<poke> sorted;
    for (int i = 1; i <= 4; i++) {
        cout << "Queue" << char('A' + i - 1) << ":";
        while (!color[i].empty()) {
            p = color[i].front();
            cout << p.a << p.b << " ";
            sorted.push(p);
            color[i].pop();
        }
        cout << "\n";
    }
    while (!sorted.empty()) {
        p = sorted.front();
        cout << p.a << p.b << " ";
        sorted.pop();
    }
    return 0;
}

If that case the input, output is shown:

 

 

 

 

V. Summary

Using an array-type queues greatly reduce the complexity of the problem, although the number can be reused to store an array of colors but confusing and difficult to use an array type is that each team is a team of the array, and the team structure elements are poke, not so clear when the initial understanding.

 

Guess you like

Origin www.cnblogs.com/Tvoimvyan/p/12601577.html