-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCPP-04-Insertion-sort.txt
More file actions
61 lines (52 loc) · 1.16 KB
/
Copy pathCPP-04-Insertion-sort.txt
File metadata and controls
61 lines (52 loc) · 1.16 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
/*
1. Define headers
2. Define class sorting with private variables and public functions
3. Define inputdata() function to get user input
4. Define sort() function and implement the insertion sort logic
5. Define outputdata() function to display output
6. Define main() function to invoke all class functions
*/
#include<iostream>
#include<iomanip>
using namespace std;
class sorting
{
private:
int a[50], n, i;
public:
void inputdata();
void sort();
void outputdata();
};
void sorting::inputdata(){
cout<<"Enter the size of the array";
cin>>n;
cout<<"Enter the array elements";
for(i=0;i<n;i++)
cin>>a[i];
}
void sorting::sort(){
int temp,j;
for(i=1;i<n;i++)
{
j=i;
while(j>=1 && a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
j--;
}
}
}
void sorting::outputdata(){
cout<<"Array elements after sorting"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<setw(4);
}
int main(){
sorting s;
s.inputdata();
s.sort();
s.outputdata();
}