top of page
Click here to go to the home page of AskTheCode.

Selection Sorting in C

Team ATC

Random Post

 

#include<stdio.h>

void swap(int *m,int *n){

int temp=*m;

*m=*n;

*n=temp;

}


void seletionSort(int arr[],int n){

int i, j, min;

for (i = 0; i < n-1; i++)

{

min=i;

for (j=i+1;j<n;j++){

if (arr[j]<arr[min]){

min=j;

swap(&arr[min],&arr[i]);

}

}

}

}


int main(){

int i,j,n,arr[n];

printf("Enter the number of elements: ");

scanf("%d",&n);

printf("\nEnter the elements:\n");

for(i=0;i<n;i++){

scanf("%d",&arr[i]);

}

printf("The ascending order of elements using Selection Sorting is:\n");

seletionSort(arr,n);

for (i=0;i<n;i++){

printf("%d ",arr[i]);

}

return 0;

}

Recent Posts

See All

Komentar


bottom of page