-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram-to-sort-an-array-using-insertion-sort.cpp
More file actions
159 lines (129 loc) · 2.67 KB
/
Program-to-sort-an-array-using-insertion-sort.cpp
File metadata and controls
159 lines (129 loc) · 2.67 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream.h>
#include <conio.h>
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void displayArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
clrscr();
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
displayArray(arr, n);
insertionSort(arr, n);
cout << "Sorted array: ";
displayArray(arr, n);
getch();
return 0;
}
//2nd Way
#include<iostream.h>
#include<conio.h>
class insertion
{
private:
int i, k;
public:
void insertion_sort(int *, int);
void display(int *, int);
};
void insertion::insertion_sort(int l[], int n)
{
l[0] = 0; // Note: Changed -0 to 0
for (int i = 1; i <= n; i++)
{
int pointer = i - 1;
int temp = l[i];
while (temp < l[pointer])
{
l[pointer + 1] = l[pointer];
pointer--;
}
l[pointer + 1] = temp;
cout << "step=" << i;
for (k = 1; k <= n; k++)
cout << " " << l[k];
cout << "\n";
}
}
void insertion::display(int l[], int n)
{
cout << "\nSorted list is as follows\n";
for (i = 1; i <= n; i++)
cout << " " << l[i];
}
void main()
{
clrscr();
insertion ins_sort;
int number;
int list[100];
cout << "\nInput the number of elements in the list: ";
cin >> number;
for (int i = 1; i <= number; i++)
cin >> list[i];
ins_sort.insertion_sort(list, number);
ins_sort.display(list, number);
getch();
}
//3rd way
#include<iostream.h>
#include<conio.h>
class insertion
{
private: int i, k ;
public: void insertion_sort(int *,int) ;
void display(int *,int) ;
} ;
void insertion:: insertion_sort(int l[], int n)
{
l[0]= -0 ;
for( int i=1 ; i<=n;i++)
{
int pointer=i-1 ;
int temp=l[i] ;
while(temp<l[pointer])
{
l[pointer+1]=l[pointer] ;
pointer-- ;
}
l[pointer+1]=temp ;
cout<<"step="<<i ;
for(k=1;k<=n;k++)
cout<<" "<<l[k] ;
cout<<"\n";
}
}
void insertion::display(int l[], int n)
{
cout<<"\n Sorted list is as follows \n" ;
for(i=1;i<=n;i++)
cout<<" "<<l[i] ;
}
void main()
{
clrscr() ;
insertion ins_sort ;
int number ;
int list[100] ;
cout<<"\n Input the number of elements in the list:" ;
cin>>number ;
for(int i=1;i<=number;i++)
cin>>list[i] ;
ins_sort.insertion_sort(list,number) ;
ins_sort.display(list,number) ;
getch() ;
}