-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionSort.c
64 lines (52 loc) · 1 KB
/
SelectionSort.c
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
//Lab Program
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
clock_t start, end;
int arr[100000];
double cpu_time;
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void printArray(int arr[], int n)
{
for(int i = 0; i<n; i++)
printf("%d\t", arr[i]);
printf("\n");
}
void selectionSort(int arr[], int n)
{
int min, i, j;
for(i=0; i<n-1; i++)
{
min = i;
for(j=i+1; j<n; j++)
if(arr[min]>arr[j])
{
min = j;
}
swap(&arr[min], &arr[i]);
}
}
int main()
{
int n,i;
printf("Enter the size of array: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
arr[i] = rand()%9001+1000;
}
printf("The unsorted array is: ");
printArray(arr,n);
start = clock();
selectionSort(arr,n);
end = clock();
cpu_time = (double)(end-start)/CLOCKS_PER_SEC;
printf("The sorted array is: ");
printArray(arr,n);
printf("The time taken is: %lf\n", cpu_time);
}