forked from ddhira123/Algorithms-HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.c
More file actions
executable file
·94 lines (84 loc) · 2.2 KB
/
SelectionSort.c
File metadata and controls
executable file
·94 lines (84 loc) · 2.2 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
/*
* @Author: Rohit Nagraj
* @Date: 2019-02-05 18:19:22
* @Last Modified by: Rohit Nagraj
* @Last Modified time: 2019-02-13 19:19:59
*/
/* Question:-
* Sort a given set of elements using bubble and selection sort and determine the time
* required to sort the elements. Plot a graph of no. of elements vs time taken.
* Specify the time efficiency class of this algorithm.
*/
/*
* Logic: Take the first element, find the smallest element in the rest of the array,
* swap it with the first element. Repeat with 2nd element and so on.
*
* Algorithm: Selection Sort
* Time complexity: O(n^2)
* Space complexity: O(1)
*/
/* Psuedo Code:-
*
* for i from 0 to n-1
* small = i
*
* for j from i+1 to n
* if array[j] < array[small]
* small = j
*
* swap array[small] and array[i]
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
double SelectionSort(int *array, int n)
{
int i, j, smallest, temp;
clock_t start = clock();
for (i = 0; i < n - 1; i++)
{
smallest = i;
for (j = i + 1; j < n; j++) // Loop finds the smallest element from i+1 to n
{
if (array[j] < array[smallest])
smallest = j;
}
// Swap the smallest element with the current ith element
temp = array[i];
array[i] = array[smallest];
array[smallest] = temp;
}
return ((double)(clock() - start)) / CLOCKS_PER_SEC;
// Returns the time taken to run the sorting algorithm
}
int main()
{
int i, n;
double t;
printf("Enter the size of the array: ");
scanf("%d", &n);
int a[n];
for (i = 0; i < n; i++) // Generate an array with random values
{
a[i] = rand() % 1000;
}
t = SelectionSort(a, n);
printf("Time: %fs\n", t);
return 0;
}
/*
* Observation:-
* X(size of array) y(time in s)
* 10,000 0.1708
* 20,000 0.677
* 30,000 1.521
* 40,000 2.702
* 50,000 4.222
* 60,000 6.073
* 70,000 8.274
* 80,000 10.795
* 90,000 13.664
* 100,000 16.878
*
* -> The code runs on a single thread
*/