Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions selection_sort
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
void selection_sort (int A[ ], int n) {
// temporary variable to store the position of minimum element

#include<stdio.h>
#define MAX 100
int main(void)
{
int arr[MAX],i,j,k,n;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d", &arr[i]);
}
int minimum;
// reduces the effective size of the array by one in each iteration.

for(i=1; i<n; i++)
{
k=arr[i]; /*k is to be inserted at proper place*/
for(j=i-1; j>=0 && k<arr[j]; j--)
arr[j+1]=arr[j];
arr[j+1]=k;
}
printf("Sorted list is :\n");
for(i=0; i<n; i++)
printf("%d ",arr[i]);
printf("\n");
return 0;
}/*End of main()*/
for(int i = 0; i < n-1 ; i++) {

// assuming the first element to be the minimum of the unsorted array .
minimum = i ;

// gives the effective size of the unsorted array .

for(int j = i+1; j < n ; j++ ) {
if(A[ j ] < A[ minimum ]) { //finds the minimum element
minimum = j ;
}
}
// putting minimum element on its proper position.
swap ( A[ minimum ], A[ i ]) ;
}
}