Insertion-Sort Progress Bar

Jul 10, 2010 at 5:15pm
Hi people!

I make my own insertion-sort algorithm and I put a progress bar to print in the screen the progress of sort for big numbers.

Can anyone help me to optimize my progress bar. Actually it print in the screen the progress (5% - 10% - 20% ...), but I would like to print one progressive bar like this (with actualize one unique progress bar):
[__________]
[|._________]
[|.|.________]
[|.|.|._______]

Thanks!!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

const int TAMANHO = 100000;

int insertionSort(int a[], int tam)
{
    for (int j = 1 ; j < tam ; j++)
    {
        int chave = a[j];
        int i = j - 1;
        while (i >= 0 && a[i] > chave)
        {
            a[i+1] = a[i];
            i = i - 1;
        }
        a[i+1] = chave;
        
        // Status de Progresso
        int progresso;
        int rep;
        progresso = j * 100 / TAMANHO;
        if (progresso % 5 == 0 && rep != progresso)
        {
            cout << progresso << " | ";
            rep = progresso;
        }
    }
}

int main(int NumberofArgs, char* pszArgs[])
{
    cout << "ALGORITMO INSERTION-SORT\n\n";
    
    int a[TAMANHO];
    int i;
    
    for (i = 0 ; i <= TAMANHO ; i++) 
    {
        a[i] = rand() % TAMANHO;
    }
    
    cout << "Lista nao ordenada\n";
    
    for (int x = 0 ; x < i ; x++) {
        cout << a[x] << "\n";
    }
    
    insertionSort(a,i);
    
    cout << "\nLista ordenada\n";
    
    for (int x = 0 ; x < i ; x++) {
        cout << a[x] << "\n";
    }
    
    // wait until user is ready before terminating program
    // to alow the user to see the program results
    system("PAUSE");
    return 0;
}


Jul 10, 2010 at 7:34pm
You can't rewrite over text using standard console features. If you absolutely must, you can get a library like curses.
Jul 10, 2010 at 7:46pm
Zhuge wrote:
You can't rewrite over text using standard console features.

That is unless the text you want to overwrite is in a single line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

const int TAMANHO = 100000;

int insertionSort(int a[], int tam)
{
    //21 whitespaces
    cout << "[                     ]";

    //20 backspaces
    cout << "\b\b\b\b\b\b\b\b\b\b\b";
    cout << "\b\b\b\b\b\b\b\b\b\b\b";

    for (int j = 1 ; j < tam ; j++)
    {
        int chave = a[j];
        int i = j - 1;
        while (i >= 0 && a[i] > chave)
        {
            a[i+1] = a[i];
            i = i - 1;
        }
        a[i+1] = chave;

        // Status de Progresso
        int progresso;
        int rep;
        progresso = j * 100 / TAMANHO;

        if (progresso % 5 == 0 && rep != progresso)
        {
            cout << '-';
            rep = progresso;
        }
    }

    cout << ']' << endl;
}

int main(int NumberofArgs, char* pszArgs[])
{
    cout << "ALGORITMO INSERTION-SORT\n\n";

    int a[TAMANHO];
    int i;

    for (i = 0 ; i <= TAMANHO ; i++)
    {
        a[i] = rand() % TAMANHO;
    }

/*
    cout << "Lista nao ordenada\n";

    for (int x = 0 ; x < i ; x++) {
        cout << a[x] << "\n";
    }
*/

    insertionSort(a,i);

/*
    cout << "\nLista ordenada\n";

    for (int x = 0 ; x < i ; x++) {
        cout << a[x] << "\n";
    }
*/

    // wait until user is ready before terminating program
    // to alow the user to see the program results
    system("PAUSE");
    return 0;
}
Jul 10, 2010 at 8:01pm
Not all consoles support \b though.
Jul 11, 2010 at 1:21pm
Exactly what I was looking for!

Thank you!
Topic archived. No new replies allowed.