-
Notifications
You must be signed in to change notification settings - Fork 45
/
ArraySort.c
84 lines (71 loc) · 1.8 KB
/
ArraySort.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/********************************
*
* ArraySort.c
*
* Created by Jean-Philippe Legault
*
* Your task is to implement in place sorting using the two available functions
* swapAdjacent, and compareAdjacent.
*
* Some bug might have been introduced... you will have to find out if there are any!
* if so, you will have to correct it
*
********************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void printArray(int *array, int size)
{
for(int i=0; i<size; i++)
{
if(i != 0)
{
printf(", ");
}
printf("%d", array[i]);
}
printf("\n");
}
void swapAdjacent(int *a, int index)
{
int *temp = a + index;
*(a + index) = *(a + index + 1);
*(a + index + 1) = *temp;
}
int compareAdjacent(int *a, int index)
{
return *a + index - *a + index + 1;
}
/**
* TODO: implement in place sorting on an array
* by using the two functions swapAdjacent and compareAdjacent
*/
void inPlaceSort(/* your input parameter */)
{
}
int main(void)
{
int array_size = 0;
printf("Enter the array size (>0) and the numbers to fill the array with: ");
if(!scanf("%d", &array_size))
{
printf("ERROR. Must enter an integer.\n");
return EXIT_FAILURE;
}
else if(array_size < 1)
{
printf("ERROR. array size must be at least 1.\n");
return EXIT_FAILURE;
}
int a[array_size];
/**********************
* TODO finish parsing the user input to fill the array
*
* it should parse user input with scanf to fill the array with values
**********************/
printf("=== Array before Sorting = ");
printArray(a, array_size);
inPlaceSort(/* your input parameter */);
printf("=== Array after Sorting = ");
printArray(a, array_size);
}