Advertisement
fahimkamal63

selectionSort

Jun 19th, 2021
914
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #define N 5
  3. void InputArray(int *pa, int n);
  4. void OutputArray(int *pa, int n);
  5. void selectionSort(int arr[], int n);
  6. void swap(int *xp, int *yp);
  7. int main(void)
  8. {
  9.     int a[N];
  10.     printf("Input five numbers: ");
  11.     InputArray(a,N);
  12.     selectionSort(a, N);
  13.     OutputArray(a,N);
  14.     return 0;
  15. }
  16. void InputArray(int *pa, int n)
  17. {
  18.     for(int i = 0; i < n; i++)
  19.     {
  20.         scanf("%d",pa);
  21.         pa++;
  22.     }
  23. }
  24. void OutputArray(int *pa, int n)
  25. {
  26.     printf("output: ");
  27.     for(int i = 0; i < n; i++)
  28.     {
  29.         printf("%4d",*pa);
  30.         pa++;
  31.     }
  32.     printf("\n");
  33. }
  34. void swap(int *xp, int *yp)
  35. {
  36.     int temp = *xp;
  37.     *xp = *yp;
  38.     *yp = temp;
  39. }
  40.  
  41. void selectionSort(int arr[], int n)
  42. {
  43.     int i, j, min_idx;
  44.     for (i = 0; i < n-1; i++)
  45.     {
  46.         min_idx = i;
  47.         for (j = i+1; j < n; j++)
  48.           if (arr[j] < arr[min_idx])
  49.             min_idx = j;
  50.         swap(&arr[min_idx], &arr[i]);
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement