-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertion-Sort.cpp
More file actions
47 lines (33 loc) · 803 Bytes
/
Copy pathInsertion-Sort.cpp
File metadata and controls
47 lines (33 loc) · 803 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
#include <iostream>
void Insertion_Sort(int arr[], int n);
void swap(int* a, int* b);
int main(){
int arr[50], n;
std::cout<< "Enter the size of your array : " ;
std::cin >> n;
std::cout<< "Enter the elements of your array : " ;
for(int i=0; i<n; ++i)
std::cin >>arr[i];
Insertion_Sort(arr,n);
std::cout<< "Sorted array : " << std::endl;
for(int i=0; i<n; ++i)
std::cout << arr[i] << ",";
return 0;
}
void Insertion_Sort(int arr[], int n){
for (int i=1; i<n; i++){
int temp = arr[i];
int j = i-1;
while(j>=0 && arr[j] > temp){
arr[j+1] = arr[j];
j --;
}
arr[j+1] = temp;
}
}
void swap(int* a, int* b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}