Advertisement
STANAANDREY

selection sort

Oct 17th, 2023
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.53 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define NMAX 101
  4.  
  5. void swap(int* a, int* b) {
  6.     int tmp = *a;
  7.     *a = *b;
  8.     *b = tmp;
  9. }
  10.  
  11. int main(void) {
  12.     static int arr[NMAX];
  13.     int n;
  14.     scanf_s("%d", &n);
  15.     for (int i = 0; i < n; i++) {
  16.         scanf_s("%d", &arr[i]);
  17.     }
  18.     for (int i = 0; i < n - 1; i++) {
  19.         int minId = i;
  20.         for (int j = i + 1; j < n; j++) {
  21.             if (arr[minId] > arr[j]) {
  22.                 minId = j;
  23.             }
  24.         }
  25.         swap(&arr[i], &arr[minId]);
  26.     }
  27.     for (int i = 0; i < n; i++) {
  28.         printf("%d ", arr[i]);
  29.     }
  30.     putchar('\n');
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement