What is this method of sorting called?

Hi, I just made a program from an algorithm I recall seeing somewhere. What is it called?


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
#include <cstdlib>
#include <iostream>
#include <windows.h>
enum Colors{
	black,          //  0 text color - multiply by 16, for background colors
	dark_blue,      //  1
	dark_green,     //  2
	dark_cyan,      //  3
	dark_red,       //  4
	dark_magenta,   //  5
	dark_yellow,    //  6
	light_gray,     //  7
	dark_gray,      //  8
	light_blue,     //  9
	light_green,    // 10
	light_cyan,     // 11
	light_red,      // 12
	light_magenta,  // 13
	light_yellow,   // 14
	white           // 15
};

using namespace std;

#define on , // So I can use the function - void text(text_color on background_color)
             // To more easily remember which is text color vs background color

// My text color function. Use it if you wish.
void text(int text_color = 15 on int paper_color = 0)
{
	// defaults to light_gray on black
	int color_total = (text_color+(paper_color*16));
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color_total );
}



Actual algorithm:
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
int Numbers[8];
int tries = 0;
int temp = 0;
char Redo = 'Y';
void Input()
{
     for (int i = 0; i < 8; i++)
     {
     cout << "What is your number?" << endl;
     cin >> Numbers[i];
     }
}
void Compute()
{
     while (Redo = 'Y')
     {
     for (int i = 0; i < 8; i++)
     {
         if (Numbers[i] < Numbers[i + 1])
         {
             temp = Numbers[i];
             Numbers[i] = Numbers[i + 1];
             Numbers[i + 1] = temp;
         }
     for (int i = 0; i < 8; i++)
     {
         if (Numbers[i] == temp)
         {
         text(white on light_blue);
         cout<< Numbers[i] << ", ";
         text(white on black);
         }
         else
         {
         text(white on light_red);
         cout<< Numbers[i] << ", ";
         }
     }
         cout<< "    Tries: " << tries << "\n";
         Sleep(1000);   
     }
     Redo = 'Y';
     tries++;
     cout << "\n";
     //cout << "Redo? Y/N: ";
     //cin >> Redo;
     }
}

int main(int argc, char *argv[])
{
    while (true)
    {
          cout << "Sort!" << endl;
          Input();
          Compute();
    }
}
I see bubble sort on lines 20 -23, then below that, it prints the last smallest number it found that just got sorted on to the screen then every other number is printed also using fancy colouring. Then it sorts again and repeats.

To answer your question, the sorting method is bubble sort
http://en.wikipedia.org/wiki/Bubble_sort
Topic archived. No new replies allowed.