File tree 2 files changed +87
-0
lines changed
.vs/cs-algorithms/v16/ipch/AutoPCH/dc43ad4a5185248e
Searching and Sorting/Insertion Sort/C++
2 files changed +87
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ using namespace std ;
3
+ /*
4
+ Insertion Sort (Recursive)
5
+
6
+ Compiled in Visual Studio 2019
7
+ by Divakar Lakhera
8
+
9
+ */
10
+
11
+ /*
12
+
13
+ void doiSort(int buf[], int len, int maxl)
14
+
15
+ - buf: array to sort
16
+ - len: no of elements "not to sort" (set it to 1 or 0 for complete array).
17
+ Setting it to any another no will result in "len" no of elements of starting of array to not get sorted and will be added at the end of sorted array.
18
+ Eg for above:
19
+ Input array(arr): 10 9 8 7 6 5 4 3 2 1
20
+ param: arr,4,10
21
+ Output(arr): 1 2 3 4 5 6 10 9 8 7
22
+ - maxl: length of array to sort
23
+
24
+ */
25
+
26
+ void doiSort (int buf[], int len, int maxl)
27
+ {
28
+ if (len >= maxl)
29
+ {
30
+ return ;
31
+ }
32
+ if (len == 1 )
33
+ {
34
+ if (buf[0 ] > buf[1 ])
35
+ {
36
+ // cout << " Shift 1" << endl;
37
+ int r = buf[1 ];
38
+ buf[1 ] = buf[0 ];
39
+ buf[0 ] = r;
40
+ }
41
+ doiSort (buf, 2 , maxl);
42
+ }
43
+ else
44
+ {
45
+ int m = buf[len];
46
+ for (int i = 0 ; i < len; i++)
47
+ {
48
+ if (buf[i] > m)
49
+ {
50
+ // cout << "insert " << buf[i] << "->" << buf[len] << " " << endl;
51
+ for (int j = len; j > i; j--)
52
+ {
53
+ int t = buf[j];
54
+ buf[j] = buf[j - 1 ];
55
+ buf[j - 1 ] = t;
56
+ }
57
+ break ;
58
+ }
59
+ }
60
+
61
+ doiSort (buf, len + 1 , maxl);
62
+ }
63
+ return ;
64
+ }
65
+
66
+ /*
67
+
68
+ Some Test Driver code
69
+ int main()
70
+ {
71
+ int a[10];
72
+ for (int i = 0; i < 10; i++)
73
+ {
74
+ a[i] = 10 - i;
75
+ }
76
+
77
+ doiSort(a,1,10);
78
+
79
+ for (int i = 0; i < 10; i++)
80
+ {
81
+ cout << a[i] << " ";
82
+ }
83
+
84
+ return 0;
85
+ }
86
+
87
+ */
You can’t perform that action at this time.
0 commit comments