-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathPrac1_2.cpp
More file actions
50 lines (45 loc) · 1.03 KB
/
Copy pathPrac1_2.cpp
File metadata and controls
50 lines (45 loc) · 1.03 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
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void selection_sort(int unsorted[],int n){
int i,j,min;
for (i = 0; i < n-1; i++) {
min = i;
for(j=i+1;j<n;j++){
if(unsorted[min]>unsorted[j]){
min=j;
}
}
swap(unsorted[min],unsorted[i]);
}
}
int main()
{
int n,i,j;
cout<<"Enter Number of elements you want to insert: ";
cin>>n;
int *unsorted = (int*)malloc(n * sizeof(int));
for (i = 0; i < n;i++) {
unsorted[i] = rand()%100;
}
time_t start,end;
start=clock();
selection_sort(unsorted,n);
double tc;
end=clock();
tc=(difftime(end,start)/CLOCKS_PER_SEC);
printf("time efficiency is %lf",tc);
cout<<"\n\nSorted Array:";
for (i = 0; i < n; i++) {
cout<<unsorted[i]<<" ";
}
return 0;
}