forked from shivprime94/Data-Structure-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort.cpp
More file actions
47 lines (39 loc) · 844 Bytes
/
selection_sort.cpp
File metadata and controls
47 lines (39 loc) · 844 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
#include<iostream>
using namespace std;
void swapp(int *a,int *b){
int temp= *b;
*b=*a;
*a=temp;
}
void Bubble_sort(int arr[],int n){
int temp;
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
if (arr[j+1]<arr[j]){
swapp(&arr[j+1],&arr[j]);
}
}
}
}
void selection_sort(int arr[],int n){
int smallest;
for(int i=0;i<n-1;i++){
smallest=i;
for(int j=i+1;j<n;j++){
if(arr[j]<arr[smallest]){
smallest=j;
}
}swapp(&arr[i],&arr[smallest]);
}
}
void print(int arr[],int n){
for(int i=0;i<n;i++){
cout<<arr[i];
}}
int main(){
int arr1[6]={1,7,9,10,8,5};
int i;
cout<<"the original array is";
print(arr1,6); cout<<endl;
selection_sort(arr1,6);
print(arr1,6);}