-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay-039.cpp
58 lines (57 loc) · 1.22 KB
/
Day-039.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
54
55
56
57
58
#include<iostream>
#include<vector>
using namespace std;
void insertionSort(int arr[],int n){
for(int i = 1;i <n; i++){
int temp = arr[i];
int j = i - 1;
while (j>=0){
if (arr[j] > temp){
// shift
arr[j+1] = arr[j];
}
else{
break;
}
j--;
}
arr[j+1] = temp;
}
}
void bubbleSort(int arr[], int n)
{
for(int i=0 ; i<n;i++){
bool swapped = false;
for(int j=0; j<n-1;j++){
if (arr[j]>arr[j+1]){
swap(arr[j],arr[j+1]);
swapped = true;
}
}
if (swapped == false)
break;
}
}
void selectionSort(int arr[], int n)
{
for (int i = 0;i<n-1; i++){
int minIndex = i;
for(int j= i+1; j<n;j++){
if(arr[j] < arr[minIndex])
minIndex = j;
}
swap(arr[minIndex],arr[i]);
}
}
void printArray(int arr[],int n){
for (int i=0;i <n;i++)
cout << arr[i] << " ";
}
int main(){
int arr[6] = {4,3,7,6,9,2};
selectionSort(arr,6);
bubbleSort(arr,6);
insertionSort(arr,6);
printArray(arr,6);
return 0;
}