-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathsort.cpp
More file actions
40 lines (40 loc) · 729 Bytes
/
sort.cpp
File metadata and controls
40 lines (40 loc) · 729 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
#include <iostream>
using namespace std;
int main()
{
int n, count = 0, swap = 0, compare = 0;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter elements:" << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int key, j;
count++; // for i=1
for (int i = 1; i < n; i++)
{
count++; //for i<n
key = arr[i];
count++; //for key=arr[i]
j = i;
count++; //for j=i
compare++;
while (j > 0 && arr[j - 1] > key)
{
count++; //for condition
swap++; //for swapping
arr[j] = arr[j - 1];
j--;
count++; //j--
}
count++; //false while condition
arr[j] = key
count++; // arr=key
count++; // i++
}
count++; //false for condition
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
}