K
K
kate2018-08-19 23:04:38
C++ / C#
kate, 2018-08-19 23:04:38

What is wrong with selection sort?

I don’t see an error, it doesn’t write anything when compiling, I run it and it just hangs

#include <stdio.h>

int main()
{
  int array[n];
  int i, j;
  int temp;
  int min;

  printf("vvedite n: \n");
  scanf ("%d", &n);

  for(i = 0; i < n; i++)
  {
    printf("vvedite massiv: \n");
    scanf("%d", &array[i]);
  }
  for (i = 0; i < n - 1; i++)
  {
    min = i;
    for (int j = i + 1; j < n; j++) 
    {
      if (array[j] < array[min]) 
      {
        min = j;
      }
    }
    temp = array[i];
    array[i] = array[min];
    array[min] = temp;
  }
  for (i = 0; i < n; i++)
  {
    printf ("%d ", array[i]);
  }
}

and this gives me immediately.
5b79cd428d7fe087830264.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2018-08-19
@kkate4ka

when compiling it says nothing...
Here are some questions for you to think about:
- where is the variable n defined in this program?
- what does the construct do int array[n]if n is a variable?
- how is the allocation of memory for int array[n]and the input of the value n through scanf related?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question