-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionsort.cpp
More file actions
103 lines (81 loc) · 2.58 KB
/
insertionsort.cpp
File metadata and controls
103 lines (81 loc) · 2.58 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
void insertionSort(vector <int>& arr, long long& passes, long long& comparisons, long long& shifts){
int n = arr.size();
for (int i = 1; i < n; i++){
passes++;
int curr = arr[i];
int prev = i-1;
while (prev >= 0){
comparisons++;
if (arr[prev] > curr){
arr[prev+1] = arr[prev];
prev--;
shifts++;
}else
break;
}
arr[prev+1] = curr;
}
}
void processArray(vector<int> arr, char type, int arrayNum, ofstream &fout, long long &sumPass, long long &sumCom, long long &sumShifts,int& batchCount){
long long passes = 0;
long long comparisons = 0;
long long shifts = 0;
insertionSort(arr, passes, comparisons, shifts); //array sorted
fout << arrayNum << "," << type << "," << arr.size() << ","
<<passes << "," << comparisons << "," << shifts << endl;
sumPass += passes;
sumCom += comparisons;
sumShifts += shifts;
batchCount++;
if (batchCount == 10){
fout << "AVG,AVG," <<arr.size()<<","
<<(double)sumPass/10 << ","
<<(double)sumCom/10 << "," << (double)sumShifts/10 << "\n";
batchCount = 0;
sumPass = 0;
sumCom = 0;
sumShifts = 0;
}
}
int main(){
ifstream fin("arrays.csv");
ofstream fout("insertion_results.csv");
if (!fin || !fout){
cout << "file error :/";
return 0;
}
fout << "Array_No,Type,Size,Passes,Comparisons,Shifts\n"; //headers add kiye hai
int arrayNum = 1;
int batchCount = 0;
long long sumPass = 0;
long long sumCom = 0;
long long sumShifts = 0;
while (true){
vector<int> arr;
char type; int size;
int number;
char comma;
if (!(fin >> type >> comma >> size >> comma)) break;
while (fin>>number){
arr.push_back(number);
if (fin.peek() == ','){
fin.ignore();
} else{
break;
}
}
char ch;
while (fin.get(ch) && ch != '\n');
processArray(arr, type, arrayNum, fout, sumPass, sumCom, sumShifts, batchCount);
arrayNum++;
}
fin.close();
fout.close();
cout<< "Insertion sort completed :)";
return 0;
}