DEV Community

Leo
Leo

Posted on

Sort

#include <iostream>

long count;

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

void print(int *a, int n) {
  for (int i = 0; i < n; i++) printf ("%i ", a[i]);
printf( "\n");
}

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;
  for(int i = 1; i <= n; i++) {
    count ++;
      if (a[m] < a[i]) m = i;
  }
//      i++;
  return m;
}

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

int main() {
srand (time(nullptr));
  int n = 100;
  int *a = arreglo(n);
  print(a, n);
  count  = 0;
  seleccion(a, n);
  printf ("\n\nArreglo ordenado: \n");
  print(a, n);
//  int m = max(a, n);
//  printf("\n la mayor variable esta en %i y es igual a %i\n", m, a[m]);
  printf ("\nEsfuerzo: %ld", count);
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)