example quick sort
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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | #include <stdio.h> #include <stdlib.h> #include <time.h> struct _Score { int total; float average; int ranking; }; typedef struct _Score SCORE; struct _Student { char name[30]; short int Korean; short int Mathematics; short int English; SCORE score; }; struct _Student* Create_Class (int _Number_Of_Student); void Delete_Class (struct _Student* _Class); void Insert_Random_Score (struct _Student* __Student, int _Number_Of_Student); void Display_Class (struct _Student* __Student, int _Number_Of_Student); void Score_Calculation (struct _Student* __Student, int _Number_Of_Student); void Ranking (struct _Student* __Student, int _Number_Of_Student); void Quick_Sort (struct _Student* __Student, int low, int high); int partition (struct _Student* __Student, int low, int high); void swapping (struct _Student* current, struct _Student* next); int main (int argc, char* argv[]) { int Number_Of_Student = 30; struct _Student* Pointer_Class = Create_Class (Number_Of_Student); // create() Insert_Random_Score ((struct _Student*) Pointer_Class, Number_Of_Student); Score_Calculation ((struct _Student*) Pointer_Class, Number_Of_Student); Ranking((struct _Student*) Pointer_Class, Number_Of_Student); printf ("Name Subject score ranking\n"); Display_Class ((struct _Student*) Pointer_Class, Number_Of_Student); Quick_Sort ((struct _Student*) Pointer_Class, 0, Number_Of_Student - 1); printf ("\n\nName Subject score ranking\n"); Display_Class ((struct _Student*) Pointer_Class, Number_Of_Student); Delete_Class ((struct _Student*) Pointer_Class); // free() return 0; } void Quick_Sort (struct _Student* __Student, int low, int high) { if (low >= high) return; int Partitioning_Index = partition ((struct _Student*) __Student, low, high); Quick_Sort ((struct _Student*) __Student, low, Partitioning_Index - 1); Quick_Sort ((struct _Student*) __Student, Partitioning_Index + 1, high); return; } int partition (struct _Student* __Student, int low, int high) { int pivot = (int) (*(__Student + high)).score.ranking; int Index_Of_Smaller_Element = (low - 1); int Index_Of_Current_Element = 0; for (Index_Of_Current_Element = low; Index_Of_Current_Element <= high - 1; Index_Of_Current_Element++) { int current = (*(__Student + Index_Of_Current_Element)).score.ranking; if (current < pivot) swapping (__Student + (++Index_Of_Smaller_Element), __Student + Index_Of_Current_Element); } swapping (__Student + Index_Of_Smaller_Element + 1, __Student + high); return (Index_Of_Smaller_Element + 1); } void swapping (struct _Student* current, struct _Student* next) { struct _Student temporary = *current; *current = *next; *next = temporary; return; } void Ranking (struct _Student* __Student, int _Number_Of_Student) { int rank = 0; int Students = 0, Ranking = 0; for (Ranking = 0; Ranking < _Number_Of_Student; Ranking++) { for (Students = 0; Students < _Number_Of_Student; Students++) { if ((*(__Student + Ranking)).score.total < (*(__Student + Students)).score.total) (*(__Student + Ranking)).score.ranking += 1; } } return; } void Score_Calculation (struct _Student* __Student, int _Number_Of_Student) { int index = 0; for (index = 0; index < _Number_Of_Student; index++) { int _Korean = (*(__Student + index)).Korean; int _Mathematics = (*(__Student + index)).Mathematics; int _English = (*(__Student + index)).English; (*(__Student + index)).score.total = _Korean + _Mathematics + _English; (*(__Student + index)).score.average = (*(__Student + index)).score.total / 3.f; } return; } void Display_Class (struct _Student* __Student, int _Number_Of_Student) { int index = 0; for (index = 0; index < _Number_Of_Student; index++) { printf ("%4s ", (*(__Student + index)).name); printf ("%4d", (*(__Student + index)).Korean); printf ("%4d", (*(__Student + index)).Mathematics); printf ("%4d", (*(__Student + index)).English); printf ("%5d", (*(__Student + index)).score.total); printf ("%7.2f", (*(__Student + index)).score.average); printf ("%4d\n", (*(__Student + index)).score.ranking + 1); } return; } void Insert_Random_Score (struct _Student* __Student, int _Number_Of_Student) { (void) srand ((unsigned int) time ((char) 0)); int Increase_Capital = 0, Increase_Small = 0; short int Capital_Letter = 65, Small_Letter = 97; int index = 0; for (index = 0; index < _Number_Of_Student; index++) { // name algorithm (*(__Student + index)).name[0] = (char) (Capital_Letter + Increase_Capital); (*(__Student + index)).name[1] = (char) (Small_Letter + (Increase_Small++)); if (Increase_Small > 26 - 1) { Increase_Small = 0; Increase_Capital++; } if (Increase_Capital > 26 - 1) { printf ("Error!! Insert_Random_Score()\n"); exit(1); // Force quit } // Subject (*(__Student + index)).Korean = (rand() % 70) + 30 + 1; (*(__Student + index)).Mathematics = (rand() % 70) + 30 + 1; (*(__Student + index)).English = (rand() % 70) + 30 + 1; } return; } void Delete_Class (struct _Student* _Class) { free ((struct _Student*) _Class); return; } struct _Student* Create_Class (int _Number_Of_Student) { if (_Number_Of_Student < 1) { printf ("Error! The minimum number of people was not reached. Create_Class()\n"); return (char) 0; } struct _Student* Return_Pointer = (struct _Student*) calloc (_Number_Of_Student, sizeof (struct _Student)); if (Return_Pointer == (char) 0) { printf ("Error! Dynamic memory allocation, Create_Class()\n"); return (char) 0; } return (struct _Student*) Return_Pointer; } | cs |
0 개의 댓글:
댓글 쓰기