Data Structure Using C++
Array Data Structure
Sorting
Selection Sort
void selectionSort(int arr[], int n);
int main(){
int arr[]={12,11,13,5,6,4,10,3,14,2};
int n=sizeof(arr)/sizeof(arr[0]);
selectionSort(arr,n);
for(int i=0;i<n;i++){
cout<<arr[i]<<"\t";
}
}
void selectionSort(int arr[], int n)
{
int i,j,min=0,temp;
for(i=0;i<n-1;i++){
min=i;
for(j=i+1;j<n;j++){
if(arr[j]<arr[min]){
min=j;
}
}
int temp;
temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
}
Let me know in the comment section if you have any question.
Previous Post:
Binary Search In Data Structure Using C++
Next Post:
Comments
Post a Comment