-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharraySorting.c
More file actions
73 lines (65 loc) · 1.29 KB
/
arraySorting.c
File metadata and controls
73 lines (65 loc) · 1.29 KB
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
70
71
72
73
#include <stdio.h>
#include <stdlib.h>
//---- Function Declarations -----
void sortAscending(int* ptr, int size);
void printArray(int* ptr, int size);
int compare(void*, void*);
void sortDescending(int* ptr, int size)
{
printf("\nArray in descending order is\n");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (ptr[i] > ptr[j])
{
int temp = ptr[i];
ptr[i] = ptr[j];
ptr[j] = temp;
}
}
}
printArray(ptr, size);
}
void sortAscending(int* ptr, int size)
{
printf("Array in ascending order is\n");
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (ptr[i] < ptr[j])
{
int temp = ptr[j];
ptr[j] = ptr[i];
ptr[i] = temp;
}
}
}
printArray(ptr, size);
}
void printArray(int* ptr, int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", ptr[i]);
}
}
int compare(void*a, void*b)
{
if ((*(int*)a) == (*(int*)b)) return 0;
else if ((*(int*)a) > (*(int*)b)) return 1;
else return -1;
}
int main()
{
int (*op[2])(int*,int);
int choice;
int array[] = {2, 6, 7, 8, 9, 3, 1, 14, 15, 11, 16};
op[0] = sortAscending;
op[1] = sortDescending;
printf("\nEnter 0 for ascending and 1 for descending choice: \n");
scanf_s("%d", &choice);
op[choice](array, (sizeof(array) / sizeof(int)));
return 0;
}