FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
C/sorting/shell_Sort.c at master · SelfCodeLearning/C · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
SelfCodeLearning
/
C
Public
forked from
TheAlgorithms/C
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
C
/
sorting
/
shell_Sort.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
68 lines (54 loc) · 1.36 KB
Breadcrumbs
C
/
sorting
/
shell_Sort.c
Copy path
File metadata and controls
68 lines (54 loc) · 1.36 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#include
<stdio.h>
#include
<stdlib.h>
#include
<time.h>
#define
ELEMENT_NR
20
#define
ARRAY_LEN
(
x
) (sizeof(x) / sizeof((x)[0]))
const
char
*
notation
=
"Shell Sort Big O Notation:\
\n--> Best Case: O(n log(n)) \
\n--> Average Case: depends on gap sequence \
\n--> Worst Case: O(n)"
;
void
show_data
(
int
arr
[],
int
len
)
{
int
i
;
for
(
i
=
0
;
i
<
len
;
i
++
)
printf
(
"%3d "
,
arr
[
i
]);
printf
(
"\n"
);
}
void
swap
(
int
*
a
,
int
*
b
)
{
int
tmp
;
tmp
=
*
a
;
*
a
=
*
b
;
*
b
=
tmp
;
}
void
shellSort
(
int
array
[],
int
len
)
{
int
i
,
j
,
gap
;
for
(
gap
=
len
/
2
;
gap
>
0
;
gap
=
gap
/
2
)
for
(
i
=
gap
;
i
<
len
;
i
++
)
for
(
j
=
i
-
gap
;
j
>=
0
&&
array
[
j
]
>
array
[
j
+
gap
];
j
=
j
-
gap
)
swap
(
&
array
[
j
],
&
array
[
j
+
gap
]);
}
int
main
(
int
argc
,
char
*
argv
[])
{
int
i
;
int
array
[
ELEMENT_NR
];
int
range
=
500
;
int
size
;
clock_t
start
,
end
;
double
time_spent
;
srand
(
time
(
NULL
));
for
(
i
=
0
;
i
<
ELEMENT_NR
;
i
++
)
array
[
i
]
=
rand
() %
range
+
1
;
size
=
ARRAY_LEN
(
array
);
show_data
(
array
,
size
);
start
=
clock
();
shellSort
(
array
,
size
);
end
=
clock
();
time_spent
=
(
double
)(
end
-
start
) /
CLOCKS_PER_SEC
;
printf
(
"Data Sorted\n"
);
show_data
(
array
,
size
);
printf
(
"%s\n"
,
notation
);
printf
(
"Time spent sorting: %f\n"
,
time_spent
);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL