Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define N 5
- void InputArray(int *pa, int n);
- void OutputArray(int *pa, int n);
- void selectionSort(int arr[], int n);
- void swap(int *xp, int *yp);
- int main(void)
- {
- int a[N];
- printf("Input five numbers: ");
- InputArray(a,N);
- selectionSort(a, N);
- OutputArray(a,N);
- return 0;
- }
- void InputArray(int *pa, int n)
- {
- for(int i = 0; i < n; i++)
- {
- scanf("%d",pa);
- pa++;
- }
- }
- void OutputArray(int *pa, int n)
- {
- printf("output: ");
- for(int i = 0; i < n; i++)
- {
- printf("%4d",*pa);
- pa++;
- }
- printf("\n");
- }
- void swap(int *xp, int *yp)
- {
- int temp = *xp;
- *xp = *yp;
- *yp = temp;
- }
- void selectionSort(int arr[], int n)
- {
- int i, j, min_idx;
- for (i = 0; i < n-1; i++)
- {
- min_idx = i;
- for (j = i+1; j < n; j++)
- if (arr[j] < arr[min_idx])
- min_idx = j;
- swap(&arr[min_idx], &arr[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement