-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.cpp
More file actions
48 lines (41 loc) · 826 Bytes
/
Copy pathinsertion_sort.cpp
File metadata and controls
48 lines (41 loc) · 826 Bytes
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
#include<iostream>
using namespace std;
void display1D(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
void insertionSort(int arr[], int size)
{
for (int i = 1; i < size; i++)
{
int temp = arr[i];
int j = i-1;
for (; j >=0 ; j--)
{
if (arr[j]>temp)
arr[j+1]=arr[j];
else
break;
}
arr[j+1]=temp;
}
}
int main()
{
int arr[5];
cout<<"Enter elements in the array:"<<endl;
for(int i=0; i<5; i++)
{
cin>>arr[i];
}
cout<<"Original Array:"<<endl;
display1D(arr,5);
cout<<"Sorted Array:"<<endl;
insertionSort(arr,5);
display1D(arr,5);
return 0;
}