C 14
Bubblesort Guest on 5th April 2021 12:47:58 AM
  1. #include <stdio.h>
  2.  
  3. void bubble_sort(int *, int);
  4. void bubble_sort_pointer(int *, int);
  5.  
  6. int main(){
  7.   int a[10] = {5, 9, 0, 8, 7, 4, 6, 3, 2, 1};
  8.   int i, *p;
  9.   p = a;
  10.   fflush(stdout);  
  11.   for (i = 0; i<10; i++)  
  12.   printf("%d ", *p++);
  13.   printf("\n");
  14.   p = a;
  15.  
  16.   printf("%d \n", *(a+1));
  17.   *(a+1) = 20;
  18.  
  19.   bubble_sort_pointer(p, 10);
  20.  
  21.   for (p = a; p < a+10; p++)
  22.     printf("%d ", *p);
  23.  
  24.   printf("\n");
  25.  
  26.   return 0;
  27. }
  28.  
  29. void bubble_sort(int x[], int n) {
  30.   int i, j, k, t;
  31.   for (i = 0; i<n-1; i++){
  32.     k = i;
  33.     for (j = i + 1; j < n; j++)
  34.       if (x[j] > x[k]) k = j;
  35.     if (k!=i){
  36.       t = x[i]; x[i]=x[k]; x[k] = t;
  37.     }
  38.   }
  39. }
  40.  
  41.  
  42. void bubble_sort_pointer(int *x, int n) {
  43.   int i, j, k, t;
  44.   for (i = 0; i<n-1; i++){
  45.     k = i;
  46.     for (j = i + 1; j < n; j++)
  47.       if (*(x+j) > *(x+k)) k = j;
  48.     if (k!=i){
  49.       t = *(x+i); *(x+i) = *(x+k); *(x+k) = t;
  50.     }
  51.   }
  52. }

Paste-bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.