forked from jYOTIHARODE/Hacktoberfest2021-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionSort.cpp
More file actions
52 lines (39 loc) · 872 Bytes
/
selectionSort.cpp
File metadata and controls
52 lines (39 loc) · 872 Bytes
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
43
44
45
46
47
48
49
50
51
52
#include<iostream>
using namespace std;
void selectionSort(int a[],int size);
void printArray(int a[],int size);
int main(){
int n;
cout<<"Enter the size of the array: ";
cin>>n;
int arr[n];
cout<<"Enter the array to be sorted : \n";
for(int i=0;i<n;i++){
cin>>arr[i];
}
cout<<"\nInitial Array : \n";
printArray(arr,n);
selectionSort(arr,n);
cout<<"\nFinal Sorted (ascending) Array : \n";
printArray(arr,n);
return 0;
}
void selectionSort(int a[],int size){
int min;
for(int i=0;i<size-1;i++){
min=i;
for(int j=i+1;j<size;j++){
if(a[min]>a[j]){
min=j;
}
}
int temp=a[min];
a[min]=a[i];
a[i]=temp;
}
}
void printArray(int a[],int size){
for(int i=0;i<size;i++){
cout<<a[i]<<" ";
}
}