fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <stddef.h>
  6.  
  7. #define MAX_SIZE 101
  8. #define SWAP(x,y,t) ((t)=(x), (x)=(y), (y)=(t))
  9.  
  10. void sort(int[], int);
  11.  
  12. int main(void)
  13. {
  14. printf("Enter the number of numbers to generate:");
  15. int n;
  16. scanf("%d", &n);
  17. if(n<1 || n>MAX_SIZE)
  18. {
  19. fprintf(stderr, "Improper value of n\n");
  20. return EXIT_FAILURE;
  21. }
  22.  
  23. int list[n];
  24. srand(time(NULL));
  25. for(int i=0; i<n; i++)
  26. {
  27. list[i] = rand() % 1000;
  28. printf("%d ", list[i]);
  29. }
  30.  
  31. sort(list,n);
  32.  
  33. printf("\n Sorted array: \n");
  34. for(int i=0; i<n; i++)
  35. printf("%d ", list[i]);
  36. printf("\n");
  37. }
  38.  
  39. void sort(int list[], int n)
  40. {
  41. int min, temp;
  42. for(int i=0; i<n-1; i++)
  43. {
  44. min=i;
  45. for(int j=i+1; j<n;j++)
  46. if(list[j] < list[min])
  47. min=j;
  48. SWAP(list[i], list[min], temp);
  49. }
  50. }
  51.  
Success #stdin #stdout 0.01s 1724KB
stdin
15
stdout
Enter the number of numbers to generate:306 413 393 401 520 994 657 867 933 203 541 797 578 20 853 
 Sorted array: 
20 203 306 393 401 413 520 541 578 657 797 853 867 933 994