-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsSort.c
More file actions
103 lines (89 loc) · 1.87 KB
/
insSort.c
File metadata and controls
103 lines (89 loc) · 1.87 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <stdlib.h>
int swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
return 0;
}
int checkArray(int *pArray, int pLength)
{
for (int i = 0; i < pLength; i++)
{
if (*(pArray + i) > *(pArray + i + 1))
{
return 0;
}
}
return 1;
}
int selSort(int *pArray, int pLength)
{
if (checkArray(pArray, pLength) == 1)
{
printf("Already sorted! \n");
return 0;
}
for (int i = 1; i < pLength; i++)
{
for (int j = 0; j <= i; j++)
{
if (*(pArray + i) < *(pArray + j))
{
swap(pArray + i, pArray + j);
}
}
}
}
int fill(int *pArray, int pLength, int pMin, int pMax)
{
for (int i = 0; i < pLength; i++)
{
*(pArray + i) = rand() % ((pMax + 1) - pMin) + pMin;
}
return 0;
}
// prints first and last 3 fields of array
int printArr(int *pArray, int pLength)
{
printf("length %d ", pLength);
if (pLength > 7)
{
for (int i = 0; i < 3; i++)
{
printf("%d ", *(pArray + i));
}
printf(" ... ");
for (int j = pLength - 3; j < pLength; j++)
{
printf("%d ", *(pArray + j));
}
}
// for arrays shorter than 7 chars
else
{
for (int i = 0; i < pLength; i++)
{
printf("%d ", *(pArray + i));
}
}
printf("\n\n");
}
int main()
{
srand(time(NULL));
int nMax = 2000000;
int nMin = 1;
int thisLength = rand() % ((nMax + 1) - nMin) + nMin;
int arr[thisLength];
fill(arr, thisLength, -100000000, 100000000);
// after generating and filling random array arr[]
int length = sizeof(arr) / sizeof(arr[0]);
printArr(arr, length);
selSort(arr, length);
printArr(arr, length);
printf("\n");
return 0;
}