sort strings
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 | #include <stdio.h> #include <stdlib.h> int String_Compare(const char* _First_String_, const char* _Second_String_); char* String_Copy(char* destination, const char* source); void Bubble_Sort(char array[][10], int size); void swap(char array[][10], int Partial_Element); void display(char array[][10], int size); int main(void) { char _String[10][10] = { "apple", "banana", "orange", "pineapple", "melon", "mango", "papaya", "fig", "coconut", "pear" }; display(_String, 10); printf("\n\n== bubble sort ==\n"); Bubble_Sort(_String, 10); display(_String, 10); return 0; } void display(char array[][10], int size) { int index = 0; for (index = 0; index < size; index++) { printf("%s\n", *(array + index)); } return; } void Bubble_Sort(char array[][10], int size) { int All_Element = 0, Partial_Element = 0; for (All_Element = 0; All_Element < size; All_Element++) { for (Partial_Element = 0; Partial_Element < size - 1; Partial_Element++) { if (String_Compare(*(array + Partial_Element + 0), *(array + Partial_Element + 1)) > 0) { swap(array, Partial_Element); } } Partial_Element++; } return; } void swap(char array[][10], int Partial_Element) { char* temporary = (char*)calloc(100, sizeof(char)); String_Copy(temporary, *(array + Partial_Element + 0)); String_Copy(*(array + Partial_Element + 0), *(array + Partial_Element + 1)); String_Copy(*(array + Partial_Element + 1), temporary); return; } char* String_Copy(char* destination, const char* source) { char* Return_Result = destination; while (*destination++ = *source++); return (char*)Return_Result; } int String_Compare(const char* _First_String_, const char* _Second_String_) { int result = 0; while (!(result = *(unsigned char*)_First_String_ - *(unsigned char*)_Second_String_) && *_Second_String_) ++_First_String_, ++_Second_String_; if (result < 0) return -1; else if (result > 0) return 1; return 0; } | cs |
0 개의 댓글:
댓글 쓰기