Advertisement
Guest User

ex7-6_skeleton

a guest
Aug 22nd, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.26 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAXLEN 1000 /* our limit on how many integers will go into an array */
  5.  
  6. /* function declarations go here */
  7. void selection_sort(int A[], int n);
  8. void print_int_array(int A[], int n);
  9. void int_swap(int *p1, int *p2);
  10.  
  11. int
  12. main() {
  13.     int A[MAXLEN];
  14.     int val; /* val is going to be the variable that scanf reads values into */
  15.     int n; /* n is the number of elements read into the array */
  16.     printf("Enter up to 1000 numbers: \n");
  17.     n = 0;
  18.     while (scanf("%d",&val) == 1) {
  19.         A[n] = val;
  20.         n++;
  21.     }
  22.  
  23.     /* call function on the array here */
  24.     printf("Sorting ...\n");
  25.     selection_sort(A, n);
  26.     print_int_array(A, n); /* this will print the sorted array :o */
  27.  
  28.     return 0;
  29. }
  30.  
  31. void
  32. selection_sort(int A[], int n) {
  33.     /* write selection sort here!
  34.         1. find the largest element in the array (i.e. find its index)
  35.         2. swap that element with the final element in the array
  36.         3. rinse & repeat on elements < n-1
  37.         Challenge: use recursion.
  38.     */
  39. }
  40.  
  41. void
  42. int_swap(int *p1, int *p2) {
  43.     int temp = *p1;
  44.     *p1 = *p2;
  45.     *p2 = temp;
  46. }
  47.  
  48. void
  49. print_int_array(int A[], int n) {
  50.     int i;
  51.     for (i=0; i<n; i++) {
  52.         printf("%d ", A[i]);
  53.     }
  54.     printf("\n");
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement