Skip to content

Instantly share code, notes, and snippets.

@mahmmoudkinawy
Created November 11, 2020 18:12
Show Gist options
  • Save mahmmoudkinawy/fa4b4a5d702577fd79402fcea5371e56 to your computer and use it in GitHub Desktop.
Save mahmmoudkinawy/fa4b4a5d702577fd79402fcea5371e56 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
using namespace std;
void selectionSort(int arr[] , int n)
{
int minIdx;
for(int i = 0 ; i < n - 1 ; i++){
minIdx = i ;
//0 1 2 3 4 5
//60 40 50 30 10 20
for(int j = i + 1 ; j < n ; j++){
if(arr[j] > arr[minIdx]){
minIdx = j;
swap(arr[minIdx] , arr[i]);
}
}
}
}
void print(int arr[] , int size){
for(int i = size-1 ; i >= 0 ; i--)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main(){
int arr[] = {60,40,50,30,10,20};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr,n);
cout<<"Array After Selection Sort : ";
print(arr,n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment