Advertisement
SergioRP

Plotze - Bubblesort

Oct 25th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <conio.h>
  5. #define MAX 50
  6.  
  7. int i, j, a[MAX];
  8. char b;
  9.  
  10. void createArray(int *a) {
  11.  
  12.     srand(time(NULL));
  13.  
  14.     for (i = 0; i < MAX; i++)
  15.         a[i] = rand() % 100;
  16.  
  17. }
  18.  
  19. void printArray(int a[]) {
  20.     for (i = 0; i < MAX; i++)
  21.         printf("%i ", a[i]);
  22.     printf("\n");
  23. }
  24.  
  25. void bubble_sort(int *a) {
  26.     for (i = 0; i < MAX; i++) {
  27.         for (j = MAX - 1; j > i; j--) {
  28.             if (a[j-1] > a[j]) {
  29.                 int x = a[j-1];
  30.                 a[j-1] = a[j];
  31.                 a[j] = x;
  32.             }
  33.         }
  34.     }
  35. }
  36.  
  37. int main()
  38. {
  39.     do {
  40.         createArray(a);
  41.         printf("Random array: ");
  42.         printArray(a);
  43.  
  44.         bubble_sort(a);
  45.         printf("Sorted array: ");
  46.         printArray(a);
  47.  
  48.         b = getch();
  49.         printf("\n");
  50.     } while (b != 27);
  51.  
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement