-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfpSort.c
69 lines (58 loc) · 1.85 KB
/
fpSort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int ascending(int a, int b)
{
return a > b;
}
int descending(int a, int b)
{
return b > a;
}
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
/************************************************************************************
This tells bubbleSort to expect a parameter (compare) that’s a pointer to a function
that receives two integer parameters and returns an integer result. Parentheses are
needed around *compare to group the * with compare to indicate that compare is a
pointer. If we had not included the parentheses, the declaration would have been
int *compare( int a, int b )
which declares a function that receives two integers as parameters and returns a
pointer to an integer.
************************************************************************************/
int bubbleSort(int arr[], int size, int (*compare)(int a, int b))
{
for (int i = 0; i < size; i++)
for (int j = 0; j < size - 1; j++)
if ((*compare)(arr[j], arr[j + 1]))
swap(&arr[j], &arr[j + 1]);
}
void usage()
{
printf("Usage: ./fpSort [-a|-d] <integers>\n");
}
int main(int argc, char const *argv[])
{
int argv_offset = 2;
int size = argc - argv_offset;
if (argc <= 2 || (strcmp(argv[1], "-a") != 0 && strcmp(argv[1], "-d") != 0))
{
usage();
return EXIT_FAILURE;
}
int arr[size];
for (int i = 0; i < size; i++)
arr[i] = atoi(argv[i + argv_offset]);
if (strcmp(argv[1], "-a") == 0)
bubbleSort(arr, size, ascending);
if (strcmp(argv[1], "-d") == 0)
bubbleSort(arr, size, descending);
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
return EXIT_SUCCESS;
}