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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs