forked from bishweashwarsukla/hactoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection sort.cpp
42 lines (40 loc) · 838 Bytes
/
selection sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
using namespace std;
void swapping(int &a ,int &b){
int temp;
temp =a;
a=b;
b= temp;
}
void display(int *array , int size){
for(int i =0;i<size;i++)
cout<<array[i]<<" ";
cout<<endl;
}
void selectionSort(int *array, int size){
int i ,j , min;
for(i=0;i<size-1;i++){
min = i;
for(j=i+1;j<size;j++)
if(array[j]<array[min]);
min = j;
swap(array[i],array[min]);
}
}
int main()
{
int n;
cout<<"enter the number of elements : ";
cin>>n;
int arr[n];
cout<<"enter elements : "<<endl;
for(int i =0;i<n;i++){
cin>>arr[i];
}
cout<<"Array before sorting :";
display(arr,n);
selectionSort(arr,n);
cout<<"array after sorting : ";
display(arr,n);
return 0;
}