FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
C/sorting/binary_Insertion_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
/
binary_Insertion_Sort.c
Copy path
More file actions
More file actions
Latest commit
History
History
History
71 lines (61 loc) · 1.72 KB
Breadcrumbs
C
/
sorting
/
binary_Insertion_Sort.c
Copy path
File metadata and controls
71 lines (61 loc) · 1.72 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
69
70
/* Sorting of array list using binary insertion sort
* Using binary search to find the proper location for
* inserting the selected item at each iteration. */
#include
<stdio.h>
/*Displays the array, passed to this method*/
void
display
(
int
arr
[],
int
n
) {
int
i
;
for
(
i
=
0
;
i
<
n
;
i
++
){
printf
(
"%d "
,
arr
[
i
]);
}
printf
(
"\n"
);
}
int
binarySearch
(
int
arr
[],
int
key
,
int
low
,
int
high
) {
if
(
low
>=
high
)
return
(
key
>
arr
[
low
]) ? (
low
+
1
):
low
;
int
mid
=
low
+
(
high
-
1
) /
2
;
if
(
arr
[
mid
]
==
key
)
return
mid
+
1
;
else
if
(
arr
[
mid
]
>
key
)
return
binarySearch
(
arr
,
key
,
low
,
mid
-
1
);
else
return
binarySearch
(
arr
,
key
,
mid
+
1
,
high
);
}
/*This is where the sorting of the array takes place
arr[] --- Array to be sorted
size --- Array Size
*/
void
insertionSort
(
int
arr
[],
int
size
) {
int
i
,
j
,
key
,
index
;
for
(
i
=
0
;
i
<
size
;
i
++
) {
j
=
i
-
1
;
key
=
arr
[
i
];
/* Use binrary search to find exact key's index */
index
=
binarySearch
(
arr
,
key
,
0
,
j
);
/* Move all elements greater than key from [index...j]
* to one position */
while
(
j
>=
index
) {
arr
[
j
+
1
]
=
arr
[
j
];
j
=
j
-
1
;
}
/* Insert key value in right place */
arr
[
j
+
1
]
=
key
;
}
}
int
main
(
int
argc
,
const
char
*
argv
[]) {
int
n
;
printf
(
"Enter size of array:\n"
);
scanf
(
"%d"
,
&
n
);
// E.g. 8
printf
(
"Enter the elements of the array\n"
);
int
i
;
int
arr
[
n
];
for
(
i
=
0
;
i
<
n
;
i
++
) {
scanf
(
"%d"
,
&
arr
[
i
] );
}
printf
(
"Original array: "
);
display
(
arr
,
n
);
insertionSort
(
arr
,
n
);
printf
(
"Sorted array: "
);
display
(
arr
,
n
);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL