Advertisement
andidegn

BubbleSort

Jan 8th, 2013
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #define SIZE 7
  3. void sortInt();
  4.  
  5. int main(void) {
  6.     int numbers[SIZE] = {10, 7, -5, 6, 1, 8, 11};  
  7.    
  8.     printf("Before: ");
  9.     int i = 0;
  10.     int* n = numbers;
  11.     while (i++ < SIZE)
  12.         printf("%d, ", *n++);
  13.     sortInt(numbers, SIZE);
  14.     printf("\nAfter: ");
  15.     n = numbers;
  16.     i = 0;
  17.     while (i++ < SIZE)
  18.         printf("%d, ", *n++);
  19. }
  20.  
  21. void sortInt(int *numbers, int length) {
  22.     int top = 0;
  23.     int temp = 0;
  24.     while (top < length) {
  25.         while (top > 0 && *numbers < *(numbers - 1)) {
  26.             temp = *numbers;
  27.             *numbers = *(numbers - 1);
  28.             *(numbers - 1) = temp;
  29.             top--;
  30.             numbers--;
  31.         }
  32.         top++;
  33.         numbers++;
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement