-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectionsort.c
67 lines (53 loc) · 1.36 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
65
66
67
// Sort list of element using selection sort algorithm!
#include<stdio.h>
void main()// main function
{// define array & required variables
int array[10];
int i,j,n,temp;
// decalre function
int find_max(int b[10],int k);// function for find maximum value
// function required for exchange value
void Exchange(int b[10], int k);
// accept array element from user
printf("\n Enter how many number of array You Want!");
scanf("%d",&n);
printf("\n Enter Element one by one....");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
// selection sorting start..
Exchange(array,n); // call exchange funtion
printf("\n sorted Array..\n");
for(i=0;i<n;i++)
{// display value of array after sorting
printf("\n %d\n",array[i]);
}
}
// write function to find maximum value..
int find_max(int b[10],int k)
{// find maximum value from array
int max=0,j;// declare variables
for(j=1;j<=k;j++)
{// if given value is garter
if(b[j]>b[max])
{
max=j;// assign value to max
}
}
return (max);// return max value
}
//define exchange function...
void Exchange(int b[10],int k)
{// define variables
int temp,big,j;
for(j=k-1;j>=1;j--)
{
// assign max value to big
big=find_max(b,j);
temp=b[big];// strored into temp
b[big]=b[j];// swap two variables
b[j]=temp;
}
return;
}