bubble sort animation
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #include <Windows.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #define MAXIMUM 20 + 1 struct Chart { int Data[MAXIMUM]; int Space[MAXIMUM][MAXIMUM]; }; /* Sort */ void Bubble_Sort(struct Chart* _chart, int Array_Size); void Swap(int* current, int* next); ////////// int Insert_Random_Numbers_In_A_Chart(struct Chart* _chart, int The_Number_Of); int Draw_chart(struct Chart* _chart); int Print_Space(struct Chart* _chart, int limit_horizon); int main(void) { struct Chart chart = { .Data = { 0, },.Space = { 0, } }; Insert_Random_Numbers_In_A_Chart(&chart, 20); // max = 20, MAXIMUM Bubble_Sort(&chart, 20); // max = 20, MAXIMUM //Draw_chart(&chart); //Print_Space(&chart, 50); return 0; } void Bubble_Sort(struct Chart* _chart, int Array_Size) { int All_Element = 0, Partial_Element = 0; for (All_Element = 0; All_Element < Array_Size; All_Element++) { for (Partial_Element = 0; Partial_Element < Array_Size - 1; Partial_Element++) { if (_chart->Data[Partial_Element] <= _chart->Data[Partial_Element + 1]) { /* animation */ Sleep(10); system("cls"); Draw_chart(_chart); Print_Space(_chart, Array_Size); /////////////// Swap(&_chart->Data[Partial_Element], &_chart->Data[Partial_Element + 1]); } } } return; } int Draw_chart(struct Chart* _chart) { // clear int row = 0, column = 0; for (column = 0; column < MAXIMUM; column++) { for (row = 0; row < MAXIMUM; row++) { _chart->Space[column][row] = 0; } } // draw int index = 0; for (index = 0; _chart->Data[index] != 0; index++) { _chart->Space[_chart->Data[index] - 1][index] = _chart->Data[index]; } return 0; } int Insert_Random_Numbers_In_A_Chart(struct Chart* _chart, int The_Number_Of) { srand((unsigned)time(NULL)); int loop = 0, Random_Number = 0, MAX = MAXIMUM; for (loop = 0; loop < The_Number_Of; loop++) { Random_Number = (rand() % (MAX - ((MAX / 3) - ((MAX / 3) % 10)))) + ((MAX / 3) - ((MAX / 3) % 10)); _chart->Data[loop] = Random_Number; } return 0; } int Print_Space(struct Chart* _chart, int limit_horizon) { int row = 0, column = 0; for (column = MAXIMUM - 1 - 1; column > 0; column--) { for (row = limit_horizon; row > 0; row--) { if (_chart->Space[column][row] == 0) { printf(" "); continue; } printf("%3d", _chart->Space[column][row]); } printf("\n"); } return 0; } void Swap(int* current, int* next) { int temporary = 0; temporary = *current; *current = *next; *next = temporary; return; } | cs |
0 개의 댓글:
댓글 쓰기