Search code examples
c++arrayssortingselection-sort

I made a function for selection sort, and I can't figure out what's going wrong here


I made a function for selection sort, and I can't figure out what's going wrong here.

void selection_sort(int n,int *arr){
    for(int i=0;i<n-1;i++){
        for(int j=i+1;j<n;j++){
  
            if(arr[j]<arr[i]){
                int temp=i;
                arr[i]=arr[j];
                arr[j]=arr[temp];
                
            }
        }
    }
}

Solution

  • I always find a picture to help. Here’s a good graphic I created some time ago:

    Selection Sort Animation

    This is what your code should be doing.