forked from suman-shah/c-program-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selection_Sort.c
65 lines (52 loc) · 1.54 KB
/
Selection_Sort.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
65
#include<stdio.h>
#include<stdlib.h>
void selectionSort(int a[], int n);
void print(int a[], int n);
int main()
{
int length, i;
printf("Welcome to the Selection Sort Algorithm!!\n");
printf("You can sort the data elements here, I will help you in that\n");
printf("So lets start with sorting integer datatypes\n");
printf("So let me know the how much integer datatypes you want me to sort:\n");
scanf("%d", &length);
// Creating an array using calloc i.e. array for the user defined length
int *array = (int *) calloc (length, sizeof(int));
// Accepting the data elements for the array
printf("Now you can enter the integers:\n");
for ( i = 0; i < length; i++)
{
scanf("%d", &array[i]);
}
// Implemention the Selection Sort (in Ascending Order)
selectionSort(array, length);
// Printing the Sorted elements
print(array, length);
}
void selectionSort(int a[], int n)
{
int i, j, minimunIndex, temp;
printf("Just started to sort Using the Selection Sort Algorithm\n");
for ( i = 0; i < n; i++)
{
minimunIndex = i;
for ( j = i + 1; j < n; j++)
{
if (a[minimunIndex] > a[j])
minimunIndex = j;
}
temp = a[i];
a[i] = a[minimunIndex];
a[minimunIndex] = temp;
}
printf("Ended Sorting using Selection Sort\n");
}
void print(int a[], int n)
{
int i;
printf("So now Printing the Array below:\n");
for ( i = 0; i < n; i++)
{
printf("%d ",a[i]);
}
}