-
Notifications
You must be signed in to change notification settings - Fork 0
/
nmSort.c
54 lines (44 loc) · 1.07 KB
/
nmSort.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void bubbleSort(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void usage()
{
printf("Usage: ./nmSort [-a|-d] <integers>\n");
}
int main(int argc, char const *argv[])
{
int argvOffset = 2;
int size = argc - argvOffset;
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 + argvOffset]);
bubbleSort(arr, size);
if (strcmp(argv[1], "-a") == 0)
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
if (strcmp(argv[1], "-d") == 0)
for (int i = size - 1; i >= 0; i--)
printf("%d ", arr[i]);
printf("\n");
return EXIT_SUCCESS;
}