DEV Community

Leo
Leo

Posted on

Bubble

#include <iostream>

int *arreglo (int n){
  int *a = new int[n];
  for (int i=0; i<n; i++) a[i] = rand() % (n*10) + 1;
  return a;
}

void print(int *a, int n){
  for (int i=0; i<n; i++)
    std::cout << a[i] << " ";
}

void swap(int &a, int &b){
  int c = a;
  a = b;
  b = c;
}

int max(int *a, int n){
  int m = 0;
  int i = 1;
  while (i < n){
    if (a[m] < a[i]) m = i;
  }
  return m;
}

void selection(int *a, int n){
  for (int k = n-1; k > 0; k--){
    swap(a[max(a,k)], a[k]);
  }
}

void bsort (int *a, int n){
  for (int k = n - 1; k > 0; k--)
    for(int i = 0; i < k; i++)
      if (a[i] > a[i+1]) swap(a[i], a[i+1]);
}

int main() {
  srand((unsigned)time(nullptr));

  int n = 100;
  int *a = arreglo(n);
  print(a,n);
  bsort(a,n);
  printf("\n\nArreglo Ordenado:\n");
  print(a,n);
  delete[]a;
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)