selection_sort..c
#include //header file input output function
#define N 10//default value N=10
void selection_sort(int a[], int n);//function declaration
int *find_largest(int a[], int n);//function declaration
void swap(int *p, int *q);//function declaration
int main(void)
{//main function
int i;
int a[N];//variable declaration
int x,y;
printf("Enter %d numbers to be sorted: ", N);
for (i = 0; i < N; i++)
scanf("%d", &a[i]);//keyboard inputting
selection_sort(a, N);//calling method
printf("In sorted order:");
for (i = 0; i < N; i++)
printf(" %d", a[i]);
printf(" ");
printf("largest element in the array :");
*find_largest(a,N);//calling method
printf(" Enter two variables for swapping :");
scanf("%d",&x);
scanf("%d",&y);
swap(&x,&y);
return 0;
}
void selection_sort(int a[], int n)
{//function definition
int i, largest = 0, temp;
if (n == 1)
return;
for (i = 1; i < n; i++)
if (a[i] > a[largest])
largest = i;//selection sort logic
if (largest < n - 1) {
temp = a[n-1];
a[n-1] = a[largest];
a[largest] = temp;
}
selection_sort(a, n - 1);
}
int *find_largest(int *a, int n)
{//function definition
for(int i=1;i if you want to find smallest element*/
*a=*(a+i);
}
printf("%d",*a);
}
void swap(int *p, int *q)
{//function definition
*p = *p + *q; // x now becomes 15
*q = *p - *q; // y becomes 10
*p = *p - *q; // x becomes 5
printf("After swapping of values :");
printf("%d %d ",*p,*q);
}
output
Enter 10 numbers to be sorted: 562 1 344 6 4 3 2 4 67 8
In sorted order: 1 2 3 4 4 6 8 67 344 562
largest element in the array :562
Enter two variables for swapping :10 20
After swapping of
values :20 10
Solution
selection_sort..c
#include //header file input output function
#define N 10//default value N=10
void selection_sort(int a[], int n);//function declaration
int *find_largest(int a[], int n);//function declaration
void swap(int *p, int *q);//function declaration
int main(void)
{//main function
int i;
int a[N];//variable declaration
int x,y;
printf("Enter %d numbers to be sorted: ", N);
for (i = 0; i < N; i++)
scanf("%d", &a[i]);//keyboard inputting
selection_sort(a, N);//calling method
printf("In sorted order:");
for (i = 0; i < N; i++)
printf(" %d", a[i]);
printf(" ");
printf("largest element in the array :");
*find_largest(a,N);//calling method
printf(" Enter two variables for swapping :");
scanf("%d",&x);
scanf("%d",&y);
swap(&x,&y);
return 0;
}
void selection_sort(int a[], int n)
{//function definition
int i, largest = 0, temp;
if (n == 1)
return;
for (i = 1; i < n; i++)
if (a[i] > a[largest])
largest = i;//selection sort logic
if (largest < n - 1) {
temp = a[n-1];
a[n-1] = a[largest];
a[largest] = temp;
}
selection_sort(a, n - 1);
}
int *find_largest(int *a, int n)
{//function definition
for(int i=1;i if you want to find smallest element*/
*a=*(a+i);
}
printf("%d",*a);
}
void swap(int *p, int *q)
{//function definition
*p = *p + *q; // x now becomes 15
*q = *p - *q; // y becomes 10
*p = *p - *q; // x becomes 5
printf("After swapping of values :");
printf("%d %d ",*p,*q);
}
output
Enter 10 numbers to be sorted: 562 1 344 6 4 3 2 4 67 8
In sorted order: 1 2 3 4 4 6 8 67 344 562
largest element in the array :562
Enter two variables for swapping :10 20
After swapping of
values :20 10

selection_sort..c#include stdio.hheader file input output func.pdf

  • 1.
    selection_sort..c #include //header fileinput output function #define N 10//default value N=10 void selection_sort(int a[], int n);//function declaration int *find_largest(int a[], int n);//function declaration void swap(int *p, int *q);//function declaration int main(void) {//main function int i; int a[N];//variable declaration int x,y; printf("Enter %d numbers to be sorted: ", N); for (i = 0; i < N; i++) scanf("%d", &a[i]);//keyboard inputting selection_sort(a, N);//calling method printf("In sorted order:"); for (i = 0; i < N; i++) printf(" %d", a[i]); printf(" "); printf("largest element in the array :"); *find_largest(a,N);//calling method printf(" Enter two variables for swapping :"); scanf("%d",&x); scanf("%d",&y); swap(&x,&y); return 0; } void selection_sort(int a[], int n) {//function definition int i, largest = 0, temp; if (n == 1) return; for (i = 1; i < n; i++) if (a[i] > a[largest]) largest = i;//selection sort logic
  • 2.
    if (largest <n - 1) { temp = a[n-1]; a[n-1] = a[largest]; a[largest] = temp; } selection_sort(a, n - 1); } int *find_largest(int *a, int n) {//function definition for(int i=1;i if you want to find smallest element*/ *a=*(a+i); } printf("%d",*a); } void swap(int *p, int *q) {//function definition *p = *p + *q; // x now becomes 15 *q = *p - *q; // y becomes 10 *p = *p - *q; // x becomes 5 printf("After swapping of values :"); printf("%d %d ",*p,*q); } output Enter 10 numbers to be sorted: 562 1 344 6 4 3 2 4 67 8 In sorted order: 1 2 3 4 4 6 8 67 344 562 largest element in the array :562 Enter two variables for swapping :10 20 After swapping of values :20 10 Solution selection_sort..c
  • 3.
    #include //header fileinput output function #define N 10//default value N=10 void selection_sort(int a[], int n);//function declaration int *find_largest(int a[], int n);//function declaration void swap(int *p, int *q);//function declaration int main(void) {//main function int i; int a[N];//variable declaration int x,y; printf("Enter %d numbers to be sorted: ", N); for (i = 0; i < N; i++) scanf("%d", &a[i]);//keyboard inputting selection_sort(a, N);//calling method printf("In sorted order:"); for (i = 0; i < N; i++) printf(" %d", a[i]); printf(" "); printf("largest element in the array :"); *find_largest(a,N);//calling method printf(" Enter two variables for swapping :"); scanf("%d",&x); scanf("%d",&y); swap(&x,&y); return 0; } void selection_sort(int a[], int n) {//function definition int i, largest = 0, temp; if (n == 1) return; for (i = 1; i < n; i++) if (a[i] > a[largest]) largest = i;//selection sort logic if (largest < n - 1) { temp = a[n-1];
  • 4.
    a[n-1] = a[largest]; a[largest]= temp; } selection_sort(a, n - 1); } int *find_largest(int *a, int n) {//function definition for(int i=1;i if you want to find smallest element*/ *a=*(a+i); } printf("%d",*a); } void swap(int *p, int *q) {//function definition *p = *p + *q; // x now becomes 15 *q = *p - *q; // y becomes 10 *p = *p - *q; // x becomes 5 printf("After swapping of values :"); printf("%d %d ",*p,*q); } output Enter 10 numbers to be sorted: 562 1 344 6 4 3 2 4 67 8 In sorted order: 1 2 3 4 4 6 8 67 344 562 largest element in the array :562 Enter two variables for swapping :10 20 After swapping of values :20 10