Skip to content

Instantly share code, notes, and snippets.

@rudrapal98
Created September 15, 2021 07:41
Show Gist options
  • Save rudrapal98/62bb1c1434588cd0099e693a2b5acb85 to your computer and use it in GitHub Desktop.
Save rudrapal98/62bb1c1434588cd0099e693a2b5acb85 to your computer and use it in GitHub Desktop.
/*
So we are going to implement selection sort.
In this we select smallest element and move it to the sorted subarray of the main array.
We repeat it several times until whole array is sorted.
*/
void selectionSort(int arr[], int n){
int i,j,min;
for(i=0;i<n-1i++){
min=i;
for(j=i+1;j<n;j++){
if(arr[j]<arr[min])
min=j;
}
swap(&arr[min],&arr[i]);
}
}
void swap(int *x, int *y){
int temp = *x;
*x=*y;
*y=temp;
}
void printarr(int arr[], int size){
int i;
for(i=0;i<size;i++)
cour<<arr[i]<<" ";
}
int main(){
int arr[]={7,4,56,45,76,32,46,98,87,61};
int n=sizeof(arr)/sizeof(arr[0]);
selectionSort(arr,n);
printarr(arr,n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment