-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndextoPositionSort.c
72 lines (53 loc) · 1.7 KB
/
IndextoPositionSort.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
//Create the sort array
int array[17] = {1, 10, 2, 3, 5, 7, 6, 154, 21312, 32, 123, 535, 34, 443, 65, 45, 87};
int sortedArray[17] = {0};
//Check if array is empty
if (sizeof(array) / sizeof(array[0]) == 0){
printf("%s", "Empty array");
return 0;
}
//Get largest and smallest value of array
int min = array[0];
int max = array[0];
for (int i = 0; i < (sizeof(array) / sizeof(array[0])); i++){
if (array[i] < min){
min = array[i];
}
if (array[i] > max){
max = array[i];
}
}
//Create and assign values to position array
int* positionArray = malloc(sizeof(array[0]) * (max - min + 2));
memset(positionArray, 0, sizeof(array[0]) * (max - min + 2));
for (int i = 0; i < (sizeof(array) / sizeof(array[0])); i++){
positionArray[array[i]] = array[i];
}
//Assign values to sorted position
int sortedIndex = 0;
for (int i = 0; i < (max - min + 2); i++){
if (positionArray[i] > 0){
if (sortedIndex < (sizeof(array) / sizeof(array[0]))){
sortedArray[sortedIndex] = positionArray[i];
sortedIndex += 1;
}
}
}
//Print sorted array
printf("%s", "Sorted array: [");
for (int i = 0; i < (sizeof(sortedArray) / sizeof(sortedArray[0])); i++){
if (i < (sizeof(sortedArray) / sizeof(sortedArray[0])) - 1){
printf("%d%s", sortedArray[i], ", ");
}else {
printf("%d", sortedArray[i]);
}
}
printf("%s", "]");
//Free allocated memory
free(positionArray);
return 0;
}