-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionSort.cpp
More file actions
38 lines (37 loc) · 787 Bytes
/
insertionSort.cpp
File metadata and controls
38 lines (37 loc) · 787 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
#include<iostream>
using namespace std;
int main(){
int n,i,j,temp,swap=0;
cout << "Enter the size" <<endl;
cin >> n;
int arr[n];
cout<< "Enter the array" << endl;
for(i=0;i<n;i++){
cin >> arr[i];
}
for(j=1;j<n;j++){
temp = arr[j];
i = j-1;
while(i >=0 && arr[i] > temp){
arr[i+1] = arr[i];
i--;
swap++;
}
arr[i+1] = temp;
}
if(swap ==0){
cout<<"This is best case"<<endl;
}
else if(swap == n*(n-1)/2){
cout << "This is worst case" << endl;
}
else {
cout<< "This is average case" << endl;
}
cout <<"The sorted array is ";
for(i=0;i<n;i++){
cout << arr[i] << " ";
}
cout << endl;
return 0;
}