bubble 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 | #include <stdio.h> void bubble_sort (int* array, int count); void swapping (int* current, int* next); int main (void) { int numberArray[10] = { 8, 4, 2, 5, 3, 7, 10, 1, 6, 9 }; int loop = 0; bubble_sort (numberArray, sizeof (numberArray) / sizeof (int)); for (loop = 0; loop < sizeof (numberArray) / sizeof (int); loop++) { printf("%d ", numberArray[loop]); } printf("\n"); return 0; } void bubble_sort (int* array, int count) { int allElement = 0, partialElement = 0; for (allElement = 0; allElement < count; allElement++) { for (partialElement = 0; partialElement < count - 1; partialElement++) { if (array[partialElement] > array[partialElement + 1]) { swapping (&array[partialElement], &array[partialElement + 1]); } } partialElement++; } return; } void swapping (int* current, int* next) { int temporary = 0; temporary = *current; *current = *next; *next = temporary; return; } | cs |
0 개의 댓글:
댓글 쓰기