t1nman

lab2_2

May 31st, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #define maxSize 10
  3.  
  4. void selectSort(int arr[]);
  5.  
  6. main()
  7. {
  8.     int array[maxSize], i = 0;
  9.  
  10.     while ( !feof(stdin) )
  11.         fscanf(stdin, "%d", &array[++i]);
  12.    
  13.     array[0] = i;
  14.     printf("\n");
  15.  
  16.     selectSort(array);
  17.  
  18.     for (i = 1; i < array[0]; i++)
  19.         printf("%d ", array[i]);
  20.     printf("\n");
  21.    
  22.     return 0;
  23. }
  24.  
  25. void swap(int *x, int *y)
  26. {
  27.     int tmp;
  28.  
  29.     tmp = *x;
  30.     *x = *y;
  31.     *y = tmp;
  32.  
  33.     return;
  34. }
  35.  
  36. void selectSort(int arr[])
  37. {
  38.     int i, j, minIndex;
  39.  
  40.     for (j = 1; j < arr[0]; j++) {
  41.         minIndex = j;
  42.         for (i = j; i < arr[0]; i++) {
  43.             if (arr[i] < arr[minIndex])
  44.                 minIndex = i;
  45.         }
  46.         swap(&arr[j], &arr[minIndex]); 
  47.     }
  48.  
  49.     return;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment