forked from tecky708/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting.cpp
53 lines (43 loc) · 969 Bytes
/
sorting.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
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
using namespace std;
void printArray(int arr[],int n){
for (int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
}
void selectionSort(int array[],int n){
int newArr[n];
for (int i =0; i<n;i++)
{
int smallest=array[i];
for (int j=i+1;j<n;j++)
{
if (array[j]>smallest){
swap(smallest,array[j]);
array[i]=smallest;
}
else array[i]=smallest;
}
newArr[i]=smallest;
}
printArray(newArr,n);
}
void bubbleSort(int arr[],int n){
for (int i=0;i<n-1;i++ )
{
for (int j=0;j<n-1-i;j++)
{
if (arr[j]>arr[j+1]){
swap(arr[j],arr[j+1]);
}
}
}
printArray(arr,n);
}
int main(){
int arr[10]={5,8,2,6,7,1,9,10,3,4};
selectionSort(arr,10);
bubbleSort(arr,10);
return 0;
}