-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1st_lab.cpp
More file actions
66 lines (62 loc) · 1.35 KB
/
Copy path1st_lab.cpp
File metadata and controls
66 lines (62 loc) · 1.35 KB
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
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>
using namespace std;
void insertion_sort(int arr[], int sz){
for(int i = 1; i<sz; i++){
int key = arr[i];
int j = i-1;
while(j >= 0 && arr[j] > key){
arr[j+1] = arr[j];
j = j-1;
}
arr[j+1] = key;
}
}
void selection_sort(int arr[], int sz){
for(int i = 0; i<sz-1; i++){
int min_index = i;
for(int j = i+1; j<sz; j++){
if(arr[j] < arr[min_index]){
min_index = j;
}
}
if(min_index != i){
swap(arr[min_index] , arr[i]);
}
}
}
void bubble_sort(int arr[], int sz){
int flag = 0;
for(int i = 1; i<sz; i++){
for(int j = 0; j<sz-1; j++){
if(arr[j] > arr[j+1]){
swap(arr[j+1], arr[j]);
flag = 1;
}
}
if(flag == 0){
break;
}
}
}
void print_array(int arr[], int sz){
for(int i = 0; i<sz; i++){
cout << arr[i] << " ";
}
cout << endl;
}
int main(){
int sz;
//cout << "Enter array size: ";
cin >> sz;
int arr[sz];
for(int i = 0; i<sz; i++){
cin >> arr[i];
}
insertion_sort(arr,sz);
print_array(arr,sz);
selection_sort(arr,sz);
print_array(arr,sz);
bubble_sort(arr,sz);
print_array(arr,sz);
return 0;
}