Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define MAXLEN 1000 /* our limit on how many integers will go into an array */
- /* function declarations go here */
- void selection_sort(int A[], int n);
- void print_int_array(int A[], int n);
- void int_swap(int *p1, int *p2);
- int
- main() {
- int A[MAXLEN];
- int val; /* val is going to be the variable that scanf reads values into */
- int n; /* n is the number of elements read into the array */
- printf("Enter up to 1000 numbers: \n");
- n = 0;
- while (scanf("%d",&val) == 1) {
- A[n] = val;
- n++;
- }
- /* call function on the array here */
- printf("Sorting ...\n");
- selection_sort(A, n);
- print_int_array(A, n); /* this will print the sorted array :o */
- return 0;
- }
- void
- selection_sort(int A[], int n) {
- /* write selection sort here!
- 1. find the largest element in the array (i.e. find its index)
- 2. swap that element with the final element in the array
- 3. rinse & repeat on elements < n-1
- Challenge: use recursion.
- */
- }
- void
- int_swap(int *p1, int *p2) {
- int temp = *p1;
- *p1 = *p2;
- *p2 = temp;
- }
- void
- print_int_array(int A[], int n) {
- int i;
- for (i=0; i<n; i++) {
- printf("%d ", A[i]);
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement