-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_selec_sort.cpp
65 lines (59 loc) · 1.92 KB
/
1_selec_sort.cpp
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
#include<iostream>
#include<chrono>
#include<bits/stdc++.h>
using namespace std;
void selectionSort(vector<int>& arr , int n)
{
for(int i=0 ; i<n-1 ; i++) // n-1 number of passes
{
int min = i; // let this be the index of the min element
for(int j=i+1 ; j<n; j++) // compare with all the succeeding elements of the array
{
if(arr[j]<arr[min])
min = j;
}
swap(arr[min] , arr[i]);
}
}
void input_generator(vector<pair<int,int>> &store)
{
for(int i=10 ; i<=1e4 ; i+=1000)
{
vector<int> arr(i);
generate(arr.begin() , arr.end() , rand) ;
clock_t time_req;
int t=0;
for(int m=1 ; m<=10 ; m++)
{
time_req = clock();
selectionSort(arr,i);
time_req = clock() - time_req;
t+=(time_req) ;
}
t=(t/10);
store.push_back({t,i});
}
}
int main()
{
vector<pair<int,int>> store;
input_generator(store);
for(auto i : store)
{
cout << "Time " << "\t" << "Number of inputs " << endl;
cout << i.first << "\t" << i.second << endl;
}
return 0;
}
// Description about the code
/*
Analysing the time complexity of Selection Sort.
1. Taking varied inputs in the array (generating the inputs using generate function:- generate(arr.begin(),arr.end(),rand)).
Made an input generator function which reapeated generates the asked number of inputs.
2. Running Selection sort for the generated outputs for 10 times and taking average of that time.
3.Plotting the graph between number of inputs and the time taken.
As complexity of selection sort is of the order of n^2 we get a parabolic graph.
(Graph plotted in desmos)
*/
// scale up and scale down of time
//CLOCKS_PER_SEC macro for converting the time generated by clock into seconds