Selection sort Algorithm (C DSA ) 🌟🌟🌟👍🎊🎊

 By: Himanshu Tiwari




Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the unsorted portion of the list and moving it to the sorted portion of the list.


code : 

#include<stdio.h>
void printarr(int arr[],int n){
    for(int i=0; i<n; i++){
        printf("%d ",arr[i]);
    }
    printf("\n");
}
void selectionsort(int arr[],int n){
    int minindex;
    for(int i=0; i<n-1; i++){
        minindex=i;
        for(int j=i+1; j<n; j++){
            if(arr[j]<arr[minindex]){
                minindex=j;
            }
        }
        int temp=arr[i];
        arr[i]=arr[minindex];
        arr[minindex]=temp;
    }
}
int main(){
int arr[]={7,1,2,5,18};
int n=5;
printarr(arr,n);
selectionsort(arr,n);
printarr(arr,n);

    return 0;
}


Comments

Popular Posts