please type a logical explaination of the code i have here. (1 paragraph) select
ID: 3699419 • Letter: P
Question
please type a logical explaination of the code i have here. (1 paragraph)
selection sort code
#include <stdio.h>
void swap(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void selection_sort(int a[], int n)
{
int i, j, mini;
//keeping sorting array one element at a time
for(i=0; i<n-1;i++)
{
mini = i;
//Finding minimum element in unsorted part
for(j = i+1; j<n;j++)
{
if(a[j] < a[mini])
mini = j;
}
swap(&a[mini],&a[i]);
}
}
int main()
{
int a[1000];
int n;
int i;
printf("Enter no of elements ");
scanf("%d",&n);
printf("Enter elements ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selection_sort(a,n);
printf("After sorting ");
for(i=0;i<n;i++)
printf("%d ",a[i] );
}
Explanation / Answer
In main function, taking the user input of number of elements user want to sort and then taking input of that many numbers. Printing the array after input. Calling selection sort function. and then printing after sorting the array. There is a function to swap two numbers in the array using reference. In selection sort function, calling this funciton to swap two numbers in array. Selection sorting is the technique to get the least number each time and sort.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.